1
- ( function ( f ) { if ( typeof exports === "object" && typeof module !== "undefined" ) { module . exports = f ( ) } else if ( typeof define === "function" && define . amd ) { define ( [ ] , f ) } else { var g ; if ( typeof window !== "undefined" ) { g = window } else if ( typeof global !== "undefined" ) { g = global } else if ( typeof self !== "undefined" ) { g = self } else { g = this } g . Qs = f ( ) } } ) ( function ( ) { var define , module , exports ; return ( function e ( t , n , r ) { function s ( o , u ) { if ( ! n [ o ] ) { if ( ! t [ o ] ) { var a = typeof require == "function" && require ; if ( ! u && a ) return a ( o , ! 0 ) ; if ( i ) return i ( o , ! 0 ) ; var f = new Error ( "Cannot find module '" + o + "'" ) ; throw f . code = "MODULE_NOT_FOUND" , f } var l = n [ o ] = { exports :{ } } ; t [ o ] [ 0 ] . call ( l . exports , function ( e ) { var n = t [ o ] [ 1 ] [ e ] ; return s ( n ? n : e ) } , l , l . exports , e , t , n , r ) } return n [ o ] . exports } var i = typeof require == "function" && require ; for ( var o = 0 ; o < r . length ; o ++ ) s ( r [ o ] ) ; return s } ) ( { 1 :[ function ( require , module , exports ) {
1
+ ( function ( f ) { if ( typeof exports === "object" && typeof module !== "undefined" ) { module . exports = f ( ) } else if ( typeof define === "function" && define . amd ) { define ( [ ] , f ) } else { var g ; if ( typeof window !== "undefined" ) { g = window } else if ( typeof global !== "undefined" ) { g = global } else if ( typeof self !== "undefined" ) { g = self } else { g = this } g . Qs = f ( ) } } ) ( function ( ) { var define , module , exports ; return ( function ( ) { function r ( e , n , t ) { function o ( i , f ) { if ( ! n [ i ] ) { if ( ! e [ i ] ) { var c = "function" == typeof require && require ; if ( ! f && c ) return c ( i , ! 0 ) ; if ( u ) return u ( i , ! 0 ) ; var a = new Error ( "Cannot find module '" + i + "'" ) ; throw a . code = "MODULE_NOT_FOUND" , a } var p = n [ i ] = { exports :{ } } ; e [ i ] [ 0 ] . call ( p . exports , function ( r ) { var n = e [ i ] [ 1 ] [ r ] ; return o ( n || r ) } , p , p . exports , r , e , n , t ) } return n [ i ] . exports } for ( var u = "function" == typeof require && require , i = 0 ; i < t . length ; i ++ ) o ( t [ i ] ) ; return o } return r } ) ( ) ( { 1 :[ function ( require , module , exports ) {
2
2
'use strict' ;
3
3
4
4
var Stringify = require ( './stringify' ) ;
@@ -62,23 +62,25 @@ var parseObject = function parseObject(chain, val, options) {
62
62
var root = chain . shift ( ) ;
63
63
64
64
var obj ;
65
- if ( root === '[]' ) {
65
+ if ( root === '[]' && options . parseArrays ) {
66
66
obj = [ ] ;
67
67
obj = obj . concat ( parseObject ( chain , val , options ) ) ;
68
68
} else {
69
69
obj = options . plainObjects ? Object . create ( null ) : { } ;
70
70
var cleanRoot = root . charAt ( 0 ) === '[' && root . charAt ( root . length - 1 ) === ']' ? root . slice ( 1 , - 1 ) : root ;
71
71
var index = parseInt ( cleanRoot , 10 ) ;
72
- if (
73
- ! isNaN ( index ) &&
74
- root !== cleanRoot &&
75
- String ( index ) === cleanRoot &&
76
- index >= 0 &&
77
- ( options . parseArrays && index <= options . arrayLimit )
72
+ if ( ! options . parseArrays && cleanRoot === '' ) {
73
+ obj = { 0 : val } ;
74
+ } else if (
75
+ ! isNaN ( index )
76
+ && root !== cleanRoot
77
+ && String ( index ) === cleanRoot
78
+ && index >= 0
79
+ && ( options . parseArrays && index <= options . arrayLimit )
78
80
) {
79
81
obj = [ ] ;
80
82
obj [ index ] = parseObject ( chain , val , options ) ;
81
- } else {
83
+ } else if ( cleanRoot !== '__proto__' ) {
82
84
obj [ cleanRoot ] = parseObject ( chain , val , options ) ;
83
85
}
84
86
}
@@ -108,8 +110,7 @@ var parseKeys = function parseKeys(givenKey, val, options) {
108
110
109
111
var keys = [ ] ;
110
112
if ( parent ) {
111
- // If we aren't using plain objects, optionally prefix keys
112
- // that would overwrite object prototype properties
113
+ // If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
113
114
if ( ! options . plainObjects && has . call ( Object . prototype , parent ) ) {
114
115
if ( ! options . allowPrototypes ) {
115
116
return ;
@@ -203,7 +204,18 @@ var defaults = {
203
204
encoder : Utils . encode
204
205
} ;
205
206
206
- var stringify = function stringify ( object , prefix , generateArrayPrefix , strictNullHandling , skipNulls , encoder , filter , sort , allowDots ) {
207
+ var isArray = Array . isArray ;
208
+ var stringify = function stringify (
209
+ object ,
210
+ prefix ,
211
+ generateArrayPrefix ,
212
+ strictNullHandling ,
213
+ skipNulls ,
214
+ encoder ,
215
+ filter ,
216
+ sort ,
217
+ allowDots
218
+ ) {
207
219
var obj = object ;
208
220
if ( typeof filter === 'function' ) {
209
221
obj = filter ( prefix , obj ) ;
@@ -231,7 +243,7 @@ var stringify = function stringify(object, prefix, generateArrayPrefix, strictNu
231
243
}
232
244
233
245
var objKeys ;
234
- if ( Array . isArray ( filter ) ) {
246
+ if ( isArray ( filter ) ) {
235
247
objKeys = filter ;
236
248
} else {
237
249
var keys = Object . keys ( obj ) ;
@@ -245,10 +257,30 @@ var stringify = function stringify(object, prefix, generateArrayPrefix, strictNu
245
257
continue ;
246
258
}
247
259
248
- if ( Array . isArray ( obj ) ) {
249
- values = values . concat ( stringify ( obj [ key ] , generateArrayPrefix ( prefix , key ) , generateArrayPrefix , strictNullHandling , skipNulls , encoder , filter , sort , allowDots ) ) ;
260
+ if ( isArray ( obj ) ) {
261
+ values = values . concat ( stringify (
262
+ obj [ key ] ,
263
+ generateArrayPrefix ( prefix , key ) ,
264
+ generateArrayPrefix ,
265
+ strictNullHandling ,
266
+ skipNulls ,
267
+ encoder ,
268
+ filter ,
269
+ sort ,
270
+ allowDots
271
+ ) ) ;
250
272
} else {
251
- values = values . concat ( stringify ( obj [ key ] , prefix + ( allowDots ? '.' + key : '[' + key + ']' ) , generateArrayPrefix , strictNullHandling , skipNulls , encoder , filter , sort , allowDots ) ) ;
273
+ values = values . concat ( stringify (
274
+ obj [ key ] ,
275
+ prefix + ( allowDots ? '.' + key : '[' + key + ']' ) ,
276
+ generateArrayPrefix ,
277
+ strictNullHandling ,
278
+ skipNulls ,
279
+ encoder ,
280
+ filter ,
281
+ sort ,
282
+ allowDots
283
+ ) ) ;
252
284
}
253
285
}
254
286
@@ -262,21 +294,22 @@ module.exports = function (object, opts) {
262
294
var strictNullHandling = typeof options . strictNullHandling === 'boolean' ? options . strictNullHandling : defaults . strictNullHandling ;
263
295
var skipNulls = typeof options . skipNulls === 'boolean' ? options . skipNulls : defaults . skipNulls ;
264
296
var encode = typeof options . encode === 'boolean' ? options . encode : defaults . encode ;
265
- var encoder = encode ? ( typeof options . encoder === 'function' ? options . encoder : defaults . encoder ) : null ;
297
+ var encoder = encode ? typeof options . encoder === 'function' ? options . encoder : defaults . encoder : null ;
266
298
var sort = typeof options . sort === 'function' ? options . sort : null ;
267
299
var allowDots = typeof options . allowDots === 'undefined' ? false : options . allowDots ;
268
300
var objKeys ;
269
301
var filter ;
270
302
271
- if ( options . encoder !== null && options . encoder !== undefined && typeof options . encoder !== 'function' ) {
303
+ if ( options . encoder !== null && typeof options . encoder !== ' undefined' && typeof options . encoder !== 'function' ) {
272
304
throw new TypeError ( 'Encoder has to be a function.' ) ;
273
305
}
274
306
275
307
if ( typeof options . filter === 'function' ) {
276
308
filter = options . filter ;
277
309
obj = filter ( '' , obj ) ;
278
- } else if ( Array . isArray ( options . filter ) ) {
279
- objKeys = filter = options . filter ;
310
+ } else if ( isArray ( options . filter ) ) {
311
+ objKeys = options . filter ;
312
+ filter = options . filter ;
280
313
}
281
314
282
315
var keys = [ ] ;
@@ -311,7 +344,17 @@ module.exports = function (object, opts) {
311
344
continue ;
312
345
}
313
346
314
- keys = keys . concat ( stringify ( obj [ key ] , key , generateArrayPrefix , strictNullHandling , skipNulls , encoder , filter , sort , allowDots ) ) ;
347
+ keys = keys . concat ( stringify (
348
+ obj [ key ] ,
349
+ key ,
350
+ generateArrayPrefix ,
351
+ strictNullHandling ,
352
+ skipNulls ,
353
+ encoder ,
354
+ filter ,
355
+ sort ,
356
+ allowDots
357
+ ) ) ;
315
358
}
316
359
317
360
return keys . join ( delimiter ) ;
@@ -332,7 +375,20 @@ var hexTable = (function () {
332
375
var has = Object . prototype . hasOwnProperty ;
333
376
334
377
exports . arrayToObject = function ( source , options ) {
335
- var obj = options . plainObjects ? Object . create ( null ) : { } ;
378
+ var obj = options && options . plainObjects ? Object . create ( null ) : { } ;
379
+ for ( var i = 0 ; i < source . length ; ++ i ) {
380
+ if ( typeof source [ i ] !== 'undefined' ) {
381
+ obj [ i ] = source [ i ] ;
382
+ }
383
+ }
384
+
385
+ return obj ;
386
+ } ;
387
+
388
+ var isArray = Array . isArray ;
389
+
390
+ var arrayToObject = function arrayToObject ( source , options ) {
391
+ var obj = options && options . plainObjects ? Object . create ( null ) : { } ;
336
392
for ( var i = 0 ; i < source . length ; ++ i ) {
337
393
if ( typeof source [ i ] !== 'undefined' ) {
338
394
obj [ i ] = source [ i ] ;
@@ -342,16 +398,17 @@ exports.arrayToObject = function (source, options) {
342
398
return obj ;
343
399
} ;
344
400
345
- exports . merge = function ( target , source , options ) {
401
+ exports . merge = function merge ( target , source , options ) {
402
+ /* eslint no-param-reassign: 0 */
346
403
if ( ! source ) {
347
404
return target ;
348
405
}
349
406
350
407
if ( typeof source !== 'object' ) {
351
- if ( Array . isArray ( target ) ) {
408
+ if ( isArray ( target ) ) {
352
409
target . push ( source ) ;
353
- } else if ( typeof target === 'object' ) {
354
- if ( options . plainObjects || options . allowPrototypes || ! has . call ( Object . prototype , source ) ) {
410
+ } else if ( target && typeof target === 'object' ) {
411
+ if ( ( options && ( options . plainObjects || options . allowPrototypes ) ) || ! has . call ( Object . prototype , source ) ) {
355
412
target [ source ] = true ;
356
413
}
357
414
} else {
@@ -361,20 +418,36 @@ exports.merge = function (target, source, options) {
361
418
return target ;
362
419
}
363
420
364
- if ( typeof target !== 'object' ) {
421
+ if ( ! target || typeof target !== 'object' ) {
365
422
return [ target ] . concat ( source ) ;
366
423
}
367
424
368
425
var mergeTarget = target ;
369
- if ( Array . isArray ( target ) && ! Array . isArray ( source ) ) {
370
- mergeTarget = exports . arrayToObject ( target , options ) ;
426
+ if ( isArray ( target ) && ! isArray ( source ) ) {
427
+ mergeTarget = arrayToObject ( target , options ) ;
428
+ }
429
+
430
+ if ( isArray ( target ) && isArray ( source ) ) {
431
+ source . forEach ( function ( item , i ) {
432
+ if ( has . call ( target , i ) ) {
433
+ var targetItem = target [ i ] ;
434
+ if ( targetItem && typeof targetItem === 'object' && item && typeof item === 'object' ) {
435
+ target [ i ] = merge ( targetItem , item , options ) ;
436
+ } else {
437
+ target . push ( item ) ;
438
+ }
439
+ } else {
440
+ target [ i ] = item ;
441
+ }
442
+ } ) ;
443
+ return target ;
371
444
}
372
445
373
446
return Object . keys ( source ) . reduce ( function ( acc , key ) {
374
447
var value = source [ key ] ;
375
448
376
449
if ( has . call ( acc , key ) ) {
377
- acc [ key ] = exports . merge ( acc [ key ] , value , options ) ;
450
+ acc [ key ] = merge ( acc [ key ] , value , options ) ;
378
451
} else {
379
452
acc [ key ] = value ;
380
453
}
@@ -404,13 +477,13 @@ exports.encode = function (str) {
404
477
var c = string . charCodeAt ( i ) ;
405
478
406
479
if (
407
- c === 0x2D || // -
408
- c === 0x2E || // .
409
- c === 0x5F || // _
410
- c === 0x7E || // ~
411
- ( c >= 0x30 && c <= 0x39 ) || // 0-9
412
- ( c >= 0x41 && c <= 0x5A ) || // a-z
413
- ( c >= 0x61 && c <= 0x7A ) // A-Z
480
+ c === 0x2D // -
481
+ || c === 0x2E // .
482
+ || c === 0x5F // _
483
+ || c === 0x7E // ~
484
+ || ( c >= 0x30 && c <= 0x39 ) // 0-9
485
+ || ( c >= 0x41 && c <= 0x5A ) // a-z
486
+ || ( c >= 0x61 && c <= 0x7A ) // A-Z
414
487
) {
415
488
out += string . charAt ( i ) ;
416
489
continue ;
@@ -433,7 +506,11 @@ exports.encode = function (str) {
433
506
434
507
i += 1 ;
435
508
c = 0x10000 + ( ( ( c & 0x3FF ) << 10 ) | ( string . charCodeAt ( i ) & 0x3FF ) ) ;
436
- out += hexTable [ 0xF0 | ( c >> 18 ) ] + hexTable [ 0x80 | ( ( c >> 12 ) & 0x3F ) ] + hexTable [ 0x80 | ( ( c >> 6 ) & 0x3F ) ] + hexTable [ 0x80 | ( c & 0x3F ) ] ;
509
+ /* eslint operator-linebreak: [2, "before"] */
510
+ out += hexTable [ 0xF0 | ( c >> 18 ) ]
511
+ + hexTable [ 0x80 | ( ( c >> 12 ) & 0x3F ) ]
512
+ + hexTable [ 0x80 | ( ( c >> 6 ) & 0x3F ) ]
513
+ + hexTable [ 0x80 | ( c & 0x3F ) ] ;
437
514
}
438
515
439
516
return out ;
@@ -452,7 +529,7 @@ exports.compact = function (obj, references) {
452
529
453
530
refs . push ( obj ) ;
454
531
455
- if ( Array . isArray ( obj ) ) {
532
+ if ( isArray ( obj ) ) {
456
533
var compacted = [ ] ;
457
534
458
535
for ( var i = 0 ; i < obj . length ; ++ i ) {
@@ -488,4 +565,4 @@ exports.isBuffer = function (obj) {
488
565
} ;
489
566
490
567
} , { } ] } , { } , [ 1 ] ) ( 1 )
491
- } ) ;
568
+ } ) ;
0 commit comments