@@ -78,45 +78,32 @@ function format(...args) {
78
78
return formatWithOptions ( emptyOptions , ...args ) ;
79
79
}
80
80
81
- function formatValue ( val , inspectOptions ) {
82
- const inspectTypes = [ 'object' , 'symbol' , 'function' , 'number' ] ;
83
-
84
- if ( inspectTypes . includes ( typeof val ) ) {
85
- return inspect ( val , inspectOptions ) ;
86
- } else {
87
- return String ( val ) ;
88
- }
89
- }
90
-
91
81
function formatWithOptions ( inspectOptions , ...args ) {
92
82
const first = args [ 0 ] ;
93
- const parts = [ ] ;
83
+ let a = 0 ;
84
+ let str = '' ;
85
+ let join = '' ;
94
86
95
- const firstIsString = typeof first === 'string' ;
96
-
97
- if ( firstIsString && args . length === 1 ) {
98
- return first ;
99
- }
100
-
101
- if ( firstIsString && / % [ s j d O o i f % ] / . test ( first ) ) {
102
- let i , tempStr ;
103
- let str = '' ;
104
- let a = 1 ;
87
+ if ( typeof first === 'string' ) {
88
+ if ( args . length === 1 ) {
89
+ return first ;
90
+ }
91
+ let tempStr ;
105
92
let lastPos = 0 ;
106
93
107
- for ( i = 0 ; i < first . length - 1 ; i ++ ) {
94
+ for ( var i = 0 ; i < first . length - 1 ; i ++ ) {
108
95
if ( first . charCodeAt ( i ) === 37 ) { // '%'
109
96
const nextChar = first . charCodeAt ( ++ i ) ;
110
- if ( a !== args . length ) {
97
+ if ( a + 1 !== args . length ) {
111
98
switch ( nextChar ) {
112
99
case 115 : // 's'
113
- tempStr = String ( args [ a ++ ] ) ;
100
+ tempStr = String ( args [ ++ a ] ) ;
114
101
break ;
115
102
case 106 : // 'j'
116
- tempStr = tryStringify ( args [ a ++ ] ) ;
103
+ tempStr = tryStringify ( args [ ++ a ] ) ;
117
104
break ;
118
105
case 100 : // 'd'
119
- const tempNum = args [ a ++ ] ;
106
+ const tempNum = args [ ++ a ] ;
120
107
// eslint-disable-next-line valid-typeof
121
108
if ( typeof tempNum === 'bigint' ) {
122
109
tempStr = `${ tempNum } n` ;
@@ -127,20 +114,20 @@ function formatWithOptions(inspectOptions, ...args) {
127
114
}
128
115
break ;
129
116
case 79 : // 'O'
130
- tempStr = inspect ( args [ a ++ ] , inspectOptions ) ;
117
+ tempStr = inspect ( args [ ++ a ] , inspectOptions ) ;
131
118
break ;
132
119
case 111 : // 'o'
133
120
{
134
- const opts = Object . assign ( { } , inspectOptions , {
121
+ tempStr = inspect ( args [ ++ a ] , {
122
+ ...inspectOptions ,
135
123
showHidden : true ,
136
124
showProxy : true ,
137
125
depth : 4
138
126
} ) ;
139
- tempStr = inspect ( args [ a ++ ] , opts ) ;
140
127
break ;
141
128
}
142
129
case 105 : // 'i'
143
- const tempInteger = args [ a ++ ] ;
130
+ const tempInteger = args [ ++ a ] ;
144
131
// eslint-disable-next-line valid-typeof
145
132
if ( typeof tempInteger === 'bigint' ) {
146
133
tempStr = `${ tempInteger } n` ;
@@ -151,7 +138,7 @@ function formatWithOptions(inspectOptions, ...args) {
151
138
}
152
139
break ;
153
140
case 102 : // 'f'
154
- const tempFloat = args [ a ++ ] ;
141
+ const tempFloat = args [ ++ a ] ;
155
142
if ( typeof tempFloat === 'symbol' ) {
156
143
tempStr = 'NaN' ;
157
144
} else {
@@ -176,24 +163,32 @@ function formatWithOptions(inspectOptions, ...args) {
176
163
}
177
164
}
178
165
}
179
- if ( lastPos === 0 ) {
180
- str = first ;
181
- } else if ( lastPos < first . length ) {
182
- str += first . slice ( lastPos ) ;
183
- }
184
-
185
- parts . push ( str ) ;
186
- while ( a < args . length ) {
187
- parts . push ( formatValue ( args [ a ] , inspectOptions ) ) ;
166
+ if ( lastPos !== 0 ) {
188
167
a ++ ;
189
- }
190
- } else {
191
- for ( const arg of args ) {
192
- parts . push ( formatValue ( arg , inspectOptions ) ) ;
168
+ join = ' ' ;
169
+ if ( lastPos < first . length ) {
170
+ str += first . slice ( lastPos ) ;
171
+ }
193
172
}
194
173
}
195
174
196
- return parts . join ( ' ' ) ;
175
+ while ( a < args . length ) {
176
+ const value = args [ a ] ;
177
+ // TODO(BridgeAR): This should apply for all besides strings. Especially
178
+ // BigInt should be properly inspected.
179
+ str += join ;
180
+ if ( typeof value !== 'string' &&
181
+ typeof value !== 'boolean' &&
182
+ // eslint-disable-next-line valid-typeof
183
+ typeof value !== 'bigint' ) {
184
+ str += inspect ( value , inspectOptions ) ;
185
+ } else {
186
+ str += value ;
187
+ }
188
+ join = ' ' ;
189
+ a ++ ;
190
+ }
191
+ return str ;
197
192
}
198
193
199
194
const debugs = { } ;
0 commit comments