@@ -177,70 +177,74 @@ 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 )
197
+ for ( i = 0 ; i < f . length - 1 ; i ++ ) {
198
+ if ( f . charCodeAt ( i ) === 37 ) { // '%'
199
+ const nextChar = f . charCodeAt ( ++ i ) ;
200
+ if ( a !== arguments . length ) {
201
+ switch ( nextChar ) {
202
+ case 115 : // 's'
203
+ tempStr = String ( arguments [ a ++ ] ) ;
204
+ break ;
205
+ case 106 : // 'j'
206
+ tempStr = tryStringify ( arguments [ a ++ ] ) ;
207
+ break ;
208
+ case 100 : // 'd'
209
+ tempStr = `${ Number ( arguments [ a ++ ] ) } ` ;
210
+ break ;
211
+ case 79 : // 'O'
212
+ tempStr = inspect ( arguments [ a ++ ] ) ;
213
+ break ;
214
+ case 111 : // 'o'
215
+ tempStr = inspect ( arguments [ a ++ ] ,
216
+ { showHidden : true , depth : 4 , showProxy : true } ) ;
217
+ break ;
218
+ case 105 : // 'i'
219
+ tempStr = `${ parseInt ( arguments [ a ++ ] ) } ` ;
220
+ break ;
221
+ case 102 : // 'f'
222
+ tempStr = `${ parseFloat ( arguments [ a ++ ] ) } ` ;
223
+ break ;
224
+ case 37 : // '%'
225
+ str += f . slice ( lastPos , i ) ;
226
+ lastPos = i + 1 ;
227
+ continue ;
228
+ default : // any other character is not a correct placeholder
229
+ continue ;
230
+ }
231
+ if ( lastPos !== i - 1 )
232
+ str += f . slice ( lastPos , i - 1 ) ;
233
+ str += tempStr ;
234
+ lastPos = i + 1 ;
235
+ } else if ( nextChar === 37 ) {
200
236
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 ;
237
+ lastPos = i + 1 ;
231
238
}
232
- lastPos = i = i + 2 ;
233
- continue ;
234
239
}
235
- ++ i ;
236
240
}
237
241
if ( lastPos === 0 )
238
242
str = f ;
239
243
else if ( lastPos < f . length )
240
244
str += f . slice ( lastPos ) ;
241
245
while ( a < arguments . length ) {
242
246
const x = arguments [ a ++ ] ;
243
- if ( x === null || ( typeof x !== 'object' && typeof x !== 'symbol' ) ) {
247
+ if ( ( typeof x !== 'object' && typeof x !== 'symbol' ) || x === null ) {
244
248
str += ` ${ x } ` ;
245
249
} else {
246
250
str += ` ${ inspect ( x ) } ` ;
0 commit comments