Skip to content

Commit 724ad8f

Browse files
committed
refactor(sprintf): replace abbreviations with words
This refactors the internal parameters used in the printer with full words, based a bit on Pino: <https://github.com/pinojs/pino/blob/main/docs/api.md#trace>. This is useful because we document these parameters and abbreviations are jargon not always understood by everyone. Compared to Pino, this is a smaller utility doing interpolation, so I went with `values` instead of a longer `interpolationValues` or so. And, in some cases, the constant `length` was stored in a variable (`valuesLength`). That felt a bit too much. Related-to: GH-4853.
1 parent c1021e9 commit 724ad8f

File tree

1 file changed

+69
-65
lines changed

1 file changed

+69
-65
lines changed

sprintf/index.ts

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -51,137 +51,141 @@ const LOWERCASE_S_CODE = 115; /* s */
5151
/**
5252
* Format a string with placeholders using the provided arguments.
5353
*
54-
* @param str
54+
* @param template
5555
* Template.
56-
* @param args
56+
* @param values
5757
* Values to interpolate.
5858
* @returns
5959
* Formatted string.
6060
*/
61-
export default function sprintf(str: string, ...args: unknown[]): string {
62-
if (typeof str !== "string") {
61+
export default function sprintf(
62+
template: string,
63+
...values: unknown[]
64+
): string {
65+
if (typeof template !== "string") {
6366
throw new TypeError("First argument must be a string");
6467
}
6568

66-
const argsLength = args.length;
67-
if (argsLength === 0) {
68-
return str;
69+
if (values.length === 0) {
70+
return template;
6971
}
7072

7173
let output = "";
72-
let argIdx = 0;
74+
let valueIndex = 0;
7375
let lastPosition = -1;
74-
const strLength = str.length;
75-
for (let i = 0; i < strLength; ) {
76-
if (str.charCodeAt(i) === PERCENT_CODE && i + 1 < strLength) {
76+
for (let index = 0; index < template.length; ) {
77+
if (
78+
template.charCodeAt(index) === PERCENT_CODE &&
79+
index + 1 < template.length
80+
) {
7781
lastPosition = lastPosition > -1 ? lastPosition : 0;
78-
switch (str.charCodeAt(i + 1)) {
82+
switch (template.charCodeAt(index + 1)) {
7983
case LOWERCASE_D_CODE:
8084
case LOWERCASE_F_CODE: {
81-
if (argIdx >= argsLength) {
85+
if (valueIndex >= values.length) {
8286
break;
8387
}
84-
const arg = args[argIdx];
85-
if (typeof arg !== "number") {
88+
const value = values[valueIndex];
89+
if (typeof value !== "number") {
8690
break;
8791
}
88-
if (lastPosition < i) {
89-
output += str.slice(lastPosition, i);
92+
if (lastPosition < index) {
93+
output += template.slice(lastPosition, index);
9094
}
91-
output += arg;
92-
lastPosition = i + 2;
93-
i++;
95+
output += value;
96+
lastPosition = index + 2;
97+
index++;
9498
break;
9599
}
96100
case LOWERCASE_I_CODE: {
97-
if (argIdx >= argsLength) {
101+
if (valueIndex >= values.length) {
98102
break;
99103
}
100-
const arg = args[argIdx];
101-
if (typeof arg !== "number") {
104+
const value = values[valueIndex];
105+
if (typeof value !== "number") {
102106
break;
103107
}
104-
if (lastPosition < i) {
105-
output += str.slice(lastPosition, i);
108+
if (lastPosition < index) {
109+
output += template.slice(lastPosition, index);
106110
}
107-
output += Math.floor(arg);
108-
lastPosition = i + 2;
109-
i++;
111+
output += Math.floor(value);
112+
lastPosition = index + 2;
113+
index++;
110114
break;
111115
}
112116
case UPPERCASE_O_CODE:
113117
case LOWERCASE_O_CODE:
114118
case LOWERCASE_J_CODE: {
115-
if (argIdx >= argsLength) {
119+
if (valueIndex >= values.length) {
116120
break;
117121
}
118-
const arg = args[argIdx];
119-
if (arg === undefined) {
122+
const value = values[valueIndex];
123+
if (value === undefined) {
120124
break;
121125
}
122-
if (lastPosition < i) {
123-
output += str.slice(lastPosition, i);
126+
if (lastPosition < index) {
127+
output += template.slice(lastPosition, index);
124128
}
125-
if (typeof arg === "string") {
126-
output += `'${arg}'`;
127-
lastPosition = i + 2;
128-
i++;
129+
if (typeof value === "string") {
130+
output += `'${value}'`;
131+
lastPosition = index + 2;
132+
index++;
129133
break;
130134
}
131-
if (typeof arg === "bigint") {
135+
if (typeof value === "bigint") {
132136
output += `"[BigInt]"`;
133-
lastPosition = i + 2;
134-
i++;
137+
lastPosition = index + 2;
138+
index++;
135139
break;
136140
}
137-
if (typeof arg === "function") {
138-
output += arg.name || "<anonymous>";
139-
lastPosition = i + 2;
140-
i++;
141+
if (typeof value === "function") {
142+
output += value.name || "<anonymous>";
143+
lastPosition = index + 2;
144+
index++;
141145
break;
142146
}
143-
output += tryStringify(arg);
144-
lastPosition = i + 2;
145-
i++;
147+
output += tryStringify(value);
148+
lastPosition = index + 2;
149+
index++;
146150
break;
147151
}
148152
case LOWERCASE_S_CODE: {
149-
if (argIdx >= argsLength) {
153+
if (valueIndex >= values.length) {
150154
break;
151155
}
152-
const arg = args[argIdx];
153-
if (typeof arg !== "string") {
156+
const value = values[valueIndex];
157+
if (typeof value !== "string") {
154158
break;
155159
}
156-
if (lastPosition < i) {
157-
output += str.slice(lastPosition, i);
160+
if (lastPosition < index) {
161+
output += template.slice(lastPosition, index);
158162
}
159-
output += arg;
160-
lastPosition = i + 2;
161-
i++;
163+
output += value;
164+
lastPosition = index + 2;
165+
index++;
162166
break;
163167
}
164168
case PERCENT_CODE: {
165-
if (lastPosition < i) {
166-
output += str.slice(lastPosition, i);
169+
if (lastPosition < index) {
170+
output += template.slice(lastPosition, index);
167171
}
168172
output += "%";
169-
lastPosition = i + 2;
170-
i++;
171-
argIdx--;
173+
lastPosition = index + 2;
174+
index++;
175+
valueIndex--;
172176
break;
173177
}
174178
}
175-
++argIdx;
179+
++valueIndex;
176180
}
177-
++i;
181+
++index;
178182
}
179183
if (lastPosition === -1) {
180-
return str;
184+
return template;
181185
}
182186

183-
if (lastPosition < strLength) {
184-
output += str.slice(lastPosition);
187+
if (lastPosition < template.length) {
188+
output += template.slice(lastPosition);
185189
}
186190

187191
return output;

0 commit comments

Comments
 (0)