@@ -174,19 +174,22 @@ function stringify(obj, sep, eq, options) {
174
174
for ( var i = 0 ; i < len ; ++ i ) {
175
175
var k = keys [ i ] ;
176
176
var v = obj [ k ] ;
177
- var ks = encode ( stringifyPrimitive ( k ) ) + eq ;
177
+ var ks = encode ( stringifyPrimitive ( k ) ) ;
178
+ ks += eq ;
178
179
179
180
if ( Array . isArray ( v ) ) {
180
181
var vlen = v . length ;
181
182
if ( vlen === 0 ) continue ;
182
183
var vlast = vlen - 1 ;
183
184
for ( var j = 0 ; j < vlen ; ++ j ) {
184
- fields += ks + encode ( stringifyPrimitive ( v [ j ] ) ) ;
185
+ fields += ks ;
186
+ fields += encode ( stringifyPrimitive ( v [ j ] ) ) ;
185
187
if ( j < vlast )
186
188
fields += sep ;
187
189
}
188
190
} else {
189
- fields += ks + encode ( stringifyPrimitive ( v ) ) ;
191
+ fields += ks ;
192
+ fields += encode ( stringifyPrimitive ( v ) ) ;
190
193
}
191
194
192
195
if ( i < flast )
@@ -200,14 +203,34 @@ function stringify(obj, sep, eq, options) {
200
203
function charCodes ( str ) {
201
204
if ( str . length === 0 ) return [ ] ;
202
205
if ( str . length === 1 ) return [ str . charCodeAt ( 0 ) ] ;
203
- const ret = [ ] ;
206
+ const ret = new Array ( str . length ) ;
204
207
for ( var i = 0 ; i < str . length ; ++ i )
205
- ret [ ret . length ] = str . charCodeAt ( i ) ;
208
+ ret [ i ] = str . charCodeAt ( i ) ;
206
209
return ret ;
207
210
}
208
211
const defSepCodes = [ 38 ] ; // &
209
212
const defEqCodes = [ 61 ] ; // =
210
213
214
+ function addKeyVal ( obj , key , value , keyEncoded , valEncoded , decode ) {
215
+ if ( key . length > 0 && keyEncoded )
216
+ key = decodeStr ( key , decode ) ;
217
+ if ( value . length > 0 && valEncoded )
218
+ value = decodeStr ( value , decode ) ;
219
+
220
+ if ( obj [ key ] === undefined ) {
221
+ obj [ key ] = value ;
222
+ } else {
223
+ const curValue = obj [ key ] ;
224
+ // A simple Array-specific property check is enough here to
225
+ // distinguish from a string value and is faster and still safe
226
+ // since we are generating all of the values being assigned.
227
+ if ( curValue . pop )
228
+ curValue [ curValue . length ] = value ;
229
+ else
230
+ obj [ key ] = [ curValue , value ] ;
231
+ }
232
+ }
233
+
211
234
// Parse a key/val string.
212
235
function parse ( qs , sep , eq , options ) {
213
236
const obj = Object . create ( null ) ;
@@ -272,23 +295,8 @@ function parse(qs, sep, eq, options) {
272
295
value += qs . slice ( lastPos , end ) ;
273
296
}
274
297
275
- if ( key . length > 0 && keyEncoded )
276
- key = decodeStr ( key , decode ) ;
277
- if ( value . length > 0 && valEncoded )
278
- value = decodeStr ( value , decode ) ;
298
+ addKeyVal ( obj , key , value , keyEncoded , valEncoded , decode ) ;
279
299
280
- if ( obj [ key ] === undefined ) {
281
- obj [ key ] = value ;
282
- } else {
283
- const curValue = obj [ key ] ;
284
- // A simple Array-specific property check is enough here to
285
- // distinguish from a string value and is faster and still safe
286
- // since we are generating all of the values being assigned.
287
- if ( curValue . pop )
288
- curValue [ curValue . length ] = value ;
289
- else
290
- obj [ key ] = [ curValue , value ] ;
291
- }
292
300
if ( -- pairs === 0 )
293
301
return obj ;
294
302
keyEncoded = valEncoded = customDecode ;
@@ -370,22 +378,8 @@ function parse(qs, sep, eq, options) {
370
378
// We ended on an empty substring
371
379
return obj ;
372
380
}
373
- if ( key . length > 0 && keyEncoded )
374
- key = decodeStr ( key , decode ) ;
375
- if ( value . length > 0 && valEncoded )
376
- value = decodeStr ( value , decode ) ;
377
- if ( obj [ key ] === undefined ) {
378
- obj [ key ] = value ;
379
- } else {
380
- const curValue = obj [ key ] ;
381
- // A simple Array-specific property check is enough here to
382
- // distinguish from a string value and is faster and still safe since
383
- // we are generating all of the values being assigned.
384
- if ( curValue . pop )
385
- curValue [ curValue . length ] = value ;
386
- else
387
- obj [ key ] = [ curValue , value ] ;
388
- }
381
+
382
+ addKeyVal ( obj , key , value , keyEncoded , valEncoded , decode ) ;
389
383
390
384
return obj ;
391
385
}
0 commit comments