@@ -177,70 +177,79 @@ function tryStringify(arg) {
177
177
}
178
178
179
179
function format ( f ) {
180
+ var i , tempStr ;
180
181
if ( typeof f !== 'string' ) {
181
- const objects = new Array ( arguments . length ) ;
182
- for ( var index = 0 ; index < arguments . length ; index ++ ) {
183
- objects [ index ] = inspect ( arguments [ index ] ) ;
182
+ if ( arguments . length === 0 ) return '' ;
183
+ var res = '' ;
184
+ for ( i = 0 ; i < arguments . length - 1 ; i ++ ) {
185
+ res += inspect ( arguments [ i ] ) ;
186
+ res += ' ' ;
184
187
}
185
- return objects . join ( ' ' ) ;
188
+ res += inspect ( arguments [ i ] ) ;
189
+ return res ;
186
190
}
187
191
188
192
if ( arguments . length === 1 ) return f ;
189
193
190
194
var str = '' ;
191
195
var a = 1 ;
192
196
var lastPos = 0 ;
193
- for ( var i = 0 ; i < f . length ; ) {
194
- if ( f . charCodeAt ( i ) === 37 /*'%'*/ && i + 1 < f . length ) {
195
- if ( f . charCodeAt ( i + 1 ) !== 37 /*'%'*/ && a >= arguments . length ) {
196
- ++ i ;
197
- continue ;
198
- }
199
- if ( lastPos < i )
200
- str += f . slice ( lastPos , i ) ;
201
- switch ( f . charCodeAt ( i + 1 ) ) {
202
- case 100 : // 'd'
203
- str += Number ( arguments [ a ++ ] ) ;
204
- break ;
205
- case 105 : // 'i'
206
- str += parseInt ( arguments [ a ++ ] ) ;
207
- break ;
208
- case 102 : // 'f'
209
- str += parseFloat ( arguments [ a ++ ] ) ;
210
- break ;
211
- case 106 : // 'j'
212
- str += tryStringify ( arguments [ a ++ ] ) ;
213
- break ;
214
- case 115 : // 's'
215
- str += String ( arguments [ a ++ ] ) ;
216
- break ;
217
- case 79 : // 'O'
218
- str += inspect ( arguments [ a ++ ] ) ;
219
- break ;
220
- case 111 : // 'o'
221
- str += inspect ( arguments [ a ++ ] ,
222
- { showHidden : true , depth : 4 , showProxy : true } ) ;
223
- break ;
224
- case 37 : // '%'
225
- str += '%' ;
226
- break ;
227
- default : // any other character is not a correct placeholder
228
- str += '%' ;
229
- lastPos = i = i + 1 ;
230
- continue ;
197
+ for ( i = 0 ; i < f . length - 1 ; i ++ ) {
198
+ if ( f . charCodeAt ( i ) === 37 ) { // '%'
199
+ if ( a !== arguments . length ) {
200
+ switch ( f . charCodeAt ( i + 1 ) ) {
201
+ case 115 : // 's'
202
+ tempStr = String ( arguments [ a ++ ] ) ;
203
+ break ;
204
+ case 106 : // 'j'
205
+ tempStr = tryStringify ( arguments [ a ++ ] ) ;
206
+ break ;
207
+ case 100 : // 'd'
208
+ tempStr = `${ Number ( arguments [ a ++ ] ) } ` ;
209
+ break ;
210
+ case 79 : // 'O'
211
+ tempStr = inspect ( arguments [ a ++ ] ) ;
212
+ break ;
213
+ case 111 : // 'o'
214
+ tempStr = inspect ( arguments [ a ++ ] ,
215
+ { showHidden : true , depth : 4 , showProxy : true } ) ;
216
+ break ;
217
+ case 105 : // 'i'
218
+ tempStr = `${ parseInt ( arguments [ a ++ ] ) } ` ;
219
+ break ;
220
+ case 102 : // 'f'
221
+ tempStr = `${ parseFloat ( arguments [ a ++ ] ) } ` ;
222
+ break ;
223
+ case 37 : // '%'
224
+ i ++ ;
225
+ str += f . slice ( lastPos , i ) ;
226
+ lastPos = i + 1 ;
227
+ continue ;
228
+ default : // any other character is not a correct placeholder
229
+ i ++ ;
230
+ continue ;
231
+ }
232
+ if ( lastPos !== i )
233
+ str += f . slice ( lastPos , i ) ;
234
+ str += tempStr ;
235
+ i ++ ;
236
+ lastPos = i + 1 ;
237
+ } else {
238
+ i ++ ;
239
+ if ( f . charCodeAt ( i ) === 37 ) {
240
+ str += f . slice ( lastPos , i ) ;
241
+ lastPos = i + 1 ;
242
+ }
231
243
}
232
- lastPos = i = i + 2 ;
233
- continue ;
234
244
}
235
- ++ i ;
236
245
}
237
246
if ( lastPos === 0 )
238
247
str = f ;
239
248
else if ( lastPos < f . length )
240
249
str += f . slice ( lastPos ) ;
241
250
while ( a < arguments . length ) {
242
251
const x = arguments [ a ++ ] ;
243
- if ( x === null || ( typeof x !== 'object' && typeof x !== 'symbol' ) ) {
252
+ if ( ( typeof x !== 'object' && typeof x !== 'symbol' ) || x === null ) {
244
253
str += ` ${ x } ` ;
245
254
} else {
246
255
str += ` ${ inspect ( x ) } ` ;
0 commit comments