@@ -72,98 +72,113 @@ function format(...args) {
7272 return formatWithOptions ( emptyOptions , ...args ) ;
7373}
7474
75- function formatWithOptions ( inspectOptions , f ) {
76- let i , tempStr ;
77- if ( typeof f !== 'string' ) {
78- if ( arguments . length === 1 ) return '' ;
79- let res = '' ;
80- for ( i = 1 ; i < arguments . length - 1 ; i ++ ) {
81- res += inspect ( arguments [ i ] , inspectOptions ) ;
82- res += ' ' ;
83- }
84- res += inspect ( arguments [ i ] , inspectOptions ) ;
85- return res ;
75+ function formatValue ( val , inspectOptions ) {
76+ const inspectTypes = [ 'object' , 'symbol' , 'function' , 'number' ] ;
77+
78+ if ( inspectTypes . includes ( typeof val ) ) {
79+ return inspect ( val , inspectOptions ) ;
80+ } else {
81+ return String ( val ) ;
82+ }
83+ }
84+
85+ function formatWithOptions ( inspectOptions , ...args ) {
86+ const first = args [ 0 ] ;
87+ const parts = [ ] ;
88+
89+ const firstIsString = typeof first === 'string' ;
90+
91+ if ( firstIsString && args . length === 1 ) {
92+ return first ;
8693 }
8794
88- if ( arguments . length === 2 ) return f ;
89-
90- let str = '' ;
91- let a = 2 ;
92- let lastPos = 0 ;
93- for ( i = 0 ; i < f . length - 1 ; i ++ ) {
94- if ( f . charCodeAt ( i ) === 37 ) { // '%'
95- const nextChar = f . charCodeAt ( ++ i ) ;
96- if ( a !== arguments . length ) {
97- switch ( nextChar ) {
98- case 115 : // 's'
99- tempStr = String ( arguments [ a ++ ] ) ;
100- break ;
101- case 106 : // 'j'
102- tempStr = tryStringify ( arguments [ a ++ ] ) ;
103- break ;
104- case 100 : // 'd'
105- const tempNum = arguments [ a ++ ] ;
106- // eslint-disable-next-line valid-typeof
107- if ( typeof tempNum === 'bigint' ) {
108- tempStr = `${ tempNum } n` ;
109- } else {
110- tempStr = `${ Number ( tempNum ) } ` ;
95+ if ( firstIsString && / % [ s j d O o i f % ] / . test ( first ) ) {
96+ let i , tempStr ;
97+ let str = '' ;
98+ let a = 1 ;
99+ let lastPos = 0 ;
100+
101+ for ( i = 0 ; i < first . length - 1 ; i ++ ) {
102+ if ( first . charCodeAt ( i ) === 37 ) { // '%'
103+ const nextChar = first . charCodeAt ( ++ i ) ;
104+ if ( a !== args . length ) {
105+ switch ( nextChar ) {
106+ case 115 : // 's'
107+ tempStr = String ( args [ a ++ ] ) ;
108+ break ;
109+ case 106 : // 'j'
110+ tempStr = tryStringify ( args [ a ++ ] ) ;
111+ break ;
112+ case 100 : // 'd'
113+ const tempNum = args [ a ++ ] ;
114+ // eslint-disable-next-line valid-typeof
115+ if ( typeof tempNum === 'bigint' ) {
116+ tempStr = `${ tempNum } n` ;
117+ } else {
118+ tempStr = `${ Number ( tempNum ) } ` ;
119+ }
120+ break ;
121+ case 79 : // 'O'
122+ tempStr = inspect ( args [ a ++ ] , inspectOptions ) ;
123+ break ;
124+ case 111 : // 'o'
125+ {
126+ const opts = Object . assign ( { } , inspectOptions , {
127+ showHidden : true ,
128+ showProxy : true ,
129+ depth : 4
130+ } ) ;
131+ tempStr = inspect ( args [ a ++ ] , opts ) ;
132+ break ;
111133 }
112- break ;
113- case 79 : // 'O'
114- tempStr = inspect ( arguments [ a ++ ] , inspectOptions ) ;
115- break ;
116- case 111 : // 'o'
117- {
118- const opts = Object . assign ( { } , inspectOptions , {
119- showHidden : true ,
120- showProxy : true
121- } ) ;
122- tempStr = inspect ( arguments [ a ++ ] , opts ) ;
123- break ;
134+ case 105 : // 'i'
135+ const tempInteger = args [ a ++ ] ;
136+ // eslint-disable-next-line valid-typeof
137+ if ( typeof tempInteger === 'bigint' ) {
138+ tempStr = `${ tempInteger } n` ;
139+ } else {
140+ tempStr = `${ parseInt ( tempInteger ) } ` ;
141+ }
142+ break ;
143+ case 102 : // 'f'
144+ tempStr = `${ parseFloat ( args [ a ++ ] ) } ` ;
145+ break ;
146+ case 37 : // '%'
147+ str += first . slice ( lastPos , i ) ;
148+ lastPos = i + 1 ;
149+ continue ;
150+ default : // any other character is not a correct placeholder
151+ continue ;
124152 }
125- case 105 : // 'i'
126- const tempInteger = arguments [ a ++ ] ;
127- // eslint-disable-next-line valid-typeof
128- if ( typeof tempInteger === 'bigint' ) {
129- tempStr = `${ tempInteger } n` ;
130- } else {
131- tempStr = `${ parseInt ( tempInteger ) } ` ;
132- }
133- break ;
134- case 102 : // 'f'
135- tempStr = `${ parseFloat ( arguments [ a ++ ] ) } ` ;
136- break ;
137- case 37 : // '%'
138- str += f . slice ( lastPos , i ) ;
139- lastPos = i + 1 ;
140- continue ;
141- default : // any other character is not a correct placeholder
142- continue ;
153+ if ( lastPos !== i - 1 ) {
154+ str += first . slice ( lastPos , i - 1 ) ;
155+ }
156+ str += tempStr ;
157+ lastPos = i + 1 ;
158+ } else if ( nextChar === 37 ) {
159+ str += first . slice ( lastPos , i ) ;
160+ lastPos = i + 1 ;
143161 }
144- if ( lastPos !== i - 1 )
145- str += f . slice ( lastPos , i - 1 ) ;
146- str += tempStr ;
147- lastPos = i + 1 ;
148- } else if ( nextChar === 37 ) {
149- str += f . slice ( lastPos , i ) ;
150- lastPos = i + 1 ;
151162 }
152163 }
153- }
154- if ( lastPos === 0 )
155- str = f ;
156- else if ( lastPos < f . length )
157- str += f . slice ( lastPos ) ;
158- while ( a < arguments . length ) {
159- const x = arguments [ a ++ ] ;
160- if ( ( typeof x !== 'object' && typeof x !== 'symbol' ) || x === null ) {
161- str += ` ${ x } ` ;
162- } else {
163- str += ` ${ inspect ( x , inspectOptions ) } ` ;
164+ if ( lastPos === 0 ) {
165+ str = first ;
166+ } else if ( lastPos < first . length ) {
167+ str += first . slice ( lastPos ) ;
168+ }
169+
170+ parts . push ( str ) ;
171+ while ( a < args . length ) {
172+ parts . push ( formatValue ( args [ a ] , inspectOptions ) ) ;
173+ a ++ ;
174+ }
175+ } else {
176+ for ( const arg of args ) {
177+ parts . push ( formatValue ( arg , inspectOptions ) ) ;
164178 }
165179 }
166- return str ;
180+
181+ return parts . join ( ' ' ) ;
167182}
168183
169184const debugs = { } ;
0 commit comments