@@ -51,137 +51,141 @@ const LOWERCASE_S_CODE = 115; /* s */
51
51
/**
52
52
* Format a string with placeholders using the provided arguments.
53
53
*
54
- * @param str
54
+ * @param template
55
55
* Template.
56
- * @param args
56
+ * @param values
57
57
* Values to interpolate.
58
58
* @returns
59
59
* Formatted string.
60
60
*/
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" ) {
63
66
throw new TypeError ( "First argument must be a string" ) ;
64
67
}
65
68
66
- const argsLength = args . length ;
67
- if ( argsLength === 0 ) {
68
- return str ;
69
+ if ( values . length === 0 ) {
70
+ return template ;
69
71
}
70
72
71
73
let output = "" ;
72
- let argIdx = 0 ;
74
+ let valueIndex = 0 ;
73
75
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
+ ) {
77
81
lastPosition = lastPosition > - 1 ? lastPosition : 0 ;
78
- switch ( str . charCodeAt ( i + 1 ) ) {
82
+ switch ( template . charCodeAt ( index + 1 ) ) {
79
83
case LOWERCASE_D_CODE :
80
84
case LOWERCASE_F_CODE : {
81
- if ( argIdx >= argsLength ) {
85
+ if ( valueIndex >= values . length ) {
82
86
break ;
83
87
}
84
- const arg = args [ argIdx ] ;
85
- if ( typeof arg !== "number" ) {
88
+ const value = values [ valueIndex ] ;
89
+ if ( typeof value !== "number" ) {
86
90
break ;
87
91
}
88
- if ( lastPosition < i ) {
89
- output += str . slice ( lastPosition , i ) ;
92
+ if ( lastPosition < index ) {
93
+ output += template . slice ( lastPosition , index ) ;
90
94
}
91
- output += arg ;
92
- lastPosition = i + 2 ;
93
- i ++ ;
95
+ output += value ;
96
+ lastPosition = index + 2 ;
97
+ index ++ ;
94
98
break ;
95
99
}
96
100
case LOWERCASE_I_CODE : {
97
- if ( argIdx >= argsLength ) {
101
+ if ( valueIndex >= values . length ) {
98
102
break ;
99
103
}
100
- const arg = args [ argIdx ] ;
101
- if ( typeof arg !== "number" ) {
104
+ const value = values [ valueIndex ] ;
105
+ if ( typeof value !== "number" ) {
102
106
break ;
103
107
}
104
- if ( lastPosition < i ) {
105
- output += str . slice ( lastPosition , i ) ;
108
+ if ( lastPosition < index ) {
109
+ output += template . slice ( lastPosition , index ) ;
106
110
}
107
- output += Math . floor ( arg ) ;
108
- lastPosition = i + 2 ;
109
- i ++ ;
111
+ output += Math . floor ( value ) ;
112
+ lastPosition = index + 2 ;
113
+ index ++ ;
110
114
break ;
111
115
}
112
116
case UPPERCASE_O_CODE :
113
117
case LOWERCASE_O_CODE :
114
118
case LOWERCASE_J_CODE : {
115
- if ( argIdx >= argsLength ) {
119
+ if ( valueIndex >= values . length ) {
116
120
break ;
117
121
}
118
- const arg = args [ argIdx ] ;
119
- if ( arg === undefined ) {
122
+ const value = values [ valueIndex ] ;
123
+ if ( value === undefined ) {
120
124
break ;
121
125
}
122
- if ( lastPosition < i ) {
123
- output += str . slice ( lastPosition , i ) ;
126
+ if ( lastPosition < index ) {
127
+ output += template . slice ( lastPosition , index ) ;
124
128
}
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 ++ ;
129
133
break ;
130
134
}
131
- if ( typeof arg === "bigint" ) {
135
+ if ( typeof value === "bigint" ) {
132
136
output += `"[BigInt]"` ;
133
- lastPosition = i + 2 ;
134
- i ++ ;
137
+ lastPosition = index + 2 ;
138
+ index ++ ;
135
139
break ;
136
140
}
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 ++ ;
141
145
break ;
142
146
}
143
- output += tryStringify ( arg ) ;
144
- lastPosition = i + 2 ;
145
- i ++ ;
147
+ output += tryStringify ( value ) ;
148
+ lastPosition = index + 2 ;
149
+ index ++ ;
146
150
break ;
147
151
}
148
152
case LOWERCASE_S_CODE : {
149
- if ( argIdx >= argsLength ) {
153
+ if ( valueIndex >= values . length ) {
150
154
break ;
151
155
}
152
- const arg = args [ argIdx ] ;
153
- if ( typeof arg !== "string" ) {
156
+ const value = values [ valueIndex ] ;
157
+ if ( typeof value !== "string" ) {
154
158
break ;
155
159
}
156
- if ( lastPosition < i ) {
157
- output += str . slice ( lastPosition , i ) ;
160
+ if ( lastPosition < index ) {
161
+ output += template . slice ( lastPosition , index ) ;
158
162
}
159
- output += arg ;
160
- lastPosition = i + 2 ;
161
- i ++ ;
163
+ output += value ;
164
+ lastPosition = index + 2 ;
165
+ index ++ ;
162
166
break ;
163
167
}
164
168
case PERCENT_CODE : {
165
- if ( lastPosition < i ) {
166
- output += str . slice ( lastPosition , i ) ;
169
+ if ( lastPosition < index ) {
170
+ output += template . slice ( lastPosition , index ) ;
167
171
}
168
172
output += "%" ;
169
- lastPosition = i + 2 ;
170
- i ++ ;
171
- argIdx -- ;
173
+ lastPosition = index + 2 ;
174
+ index ++ ;
175
+ valueIndex -- ;
172
176
break ;
173
177
}
174
178
}
175
- ++ argIdx ;
179
+ ++ valueIndex ;
176
180
}
177
- ++ i ;
181
+ ++ index ;
178
182
}
179
183
if ( lastPosition === - 1 ) {
180
- return str ;
184
+ return template ;
181
185
}
182
186
183
- if ( lastPosition < strLength ) {
184
- output += str . slice ( lastPosition ) ;
187
+ if ( lastPosition < template . length ) {
188
+ output += template . slice ( lastPosition ) ;
185
189
}
186
190
187
191
return output ;
0 commit comments