qs 6.2.0 → 6.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +45 -0
- package/.eslintrc +35 -15
- package/.github/FUNDING.yml +12 -0
- package/.nycrc +13 -0
- package/CHANGELOG.md +36 -0
- package/LICENSE.md +29 -0
- package/README.md +400 -0
- package/bower.json +21 -0
- package/component.json +15 -0
- package/dist/qs.js +145 -64
- package/lib/index.js +0 -0
- package/lib/parse.js +34 -33
- package/lib/stringify.js +52 -10
- package/lib/utils.js +57 -19
- package/package.json +52 -46
- package/test/index.js +2 -0
- package/test/parse.js +195 -41
- package/test/stringify.js +11 -13
- package/test/utils.js +20 -0
- package/.eslintignore +0 -1
- package/.jscs.json +0 -176
- package/LICENSE +0 -28
package/dist/qs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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
|
|
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');
|
|
@@ -14,6 +14,8 @@ module.exports = {
|
|
|
14
14
|
|
|
15
15
|
var Utils = require('./utils');
|
|
16
16
|
|
|
17
|
+
var has = Object.prototype.hasOwnProperty;
|
|
18
|
+
|
|
17
19
|
var defaults = {
|
|
18
20
|
delimiter: '&',
|
|
19
21
|
depth: 5,
|
|
@@ -34,21 +36,18 @@ var parseValues = function parseValues(str, options) {
|
|
|
34
36
|
var part = parts[i];
|
|
35
37
|
var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
|
|
36
38
|
|
|
39
|
+
var key, val;
|
|
37
40
|
if (pos === -1) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (options.strictNullHandling) {
|
|
41
|
-
obj[options.decoder(part)] = null;
|
|
42
|
-
}
|
|
41
|
+
key = options.decoder(part);
|
|
42
|
+
val = options.strictNullHandling ? null : '';
|
|
43
43
|
} else {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
44
|
+
key = options.decoder(part.slice(0, pos));
|
|
45
|
+
val = options.decoder(part.slice(pos + 1));
|
|
46
|
+
}
|
|
47
|
+
if (has.call(obj, key)) {
|
|
48
|
+
obj[key] = [].concat(obj[key]).concat(val);
|
|
49
|
+
} else {
|
|
50
|
+
obj[key] = val;
|
|
52
51
|
}
|
|
53
52
|
}
|
|
54
53
|
|
|
@@ -63,23 +62,25 @@ var parseObject = function parseObject(chain, val, options) {
|
|
|
63
62
|
var root = chain.shift();
|
|
64
63
|
|
|
65
64
|
var obj;
|
|
66
|
-
if (root === '[]') {
|
|
65
|
+
if (root === '[]' && options.parseArrays) {
|
|
67
66
|
obj = [];
|
|
68
67
|
obj = obj.concat(parseObject(chain, val, options));
|
|
69
68
|
} else {
|
|
70
69
|
obj = options.plainObjects ? Object.create(null) : {};
|
|
71
|
-
var cleanRoot = root
|
|
70
|
+
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
|
|
72
71
|
var index = parseInt(cleanRoot, 10);
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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)
|
|
79
80
|
) {
|
|
80
81
|
obj = [];
|
|
81
82
|
obj[index] = parseObject(chain, val, options);
|
|
82
|
-
} else {
|
|
83
|
+
} else if (cleanRoot !== '__proto__') {
|
|
83
84
|
obj[cleanRoot] = parseObject(chain, val, options);
|
|
84
85
|
}
|
|
85
86
|
}
|
|
@@ -93,30 +94,30 @@ var parseKeys = function parseKeys(givenKey, val, options) {
|
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
// Transform dot notation to bracket notation
|
|
96
|
-
var key = options.allowDots ? givenKey.replace(/\.([
|
|
97
|
+
var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
|
|
97
98
|
|
|
98
99
|
// The regex chunks
|
|
99
100
|
|
|
100
|
-
var
|
|
101
|
-
var child = /(\[[
|
|
101
|
+
var brackets = /(\[[^[\]]*])/;
|
|
102
|
+
var child = /(\[[^[\]]*])/g;
|
|
102
103
|
|
|
103
104
|
// Get the parent
|
|
104
105
|
|
|
105
|
-
var segment =
|
|
106
|
+
var segment = brackets.exec(key);
|
|
107
|
+
var parent = segment ? key.slice(0, segment.index) : key;
|
|
106
108
|
|
|
107
109
|
// Stash the parent if it exists
|
|
108
110
|
|
|
109
111
|
var keys = [];
|
|
110
|
-
if (
|
|
111
|
-
// If we aren't using plain objects, optionally prefix keys
|
|
112
|
-
|
|
113
|
-
if (!options.plainObjects && Object.prototype.hasOwnProperty(segment[1])) {
|
|
112
|
+
if (parent) {
|
|
113
|
+
// If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
|
|
114
|
+
if (!options.plainObjects && has.call(Object.prototype, parent)) {
|
|
114
115
|
if (!options.allowPrototypes) {
|
|
115
116
|
return;
|
|
116
117
|
}
|
|
117
118
|
}
|
|
118
119
|
|
|
119
|
-
keys.push(
|
|
120
|
+
keys.push(parent);
|
|
120
121
|
}
|
|
121
122
|
|
|
122
123
|
// Loop through children appending to the array until we hit depth
|
|
@@ -124,9 +125,9 @@ var parseKeys = function parseKeys(givenKey, val, options) {
|
|
|
124
125
|
var i = 0;
|
|
125
126
|
while ((segment = child.exec(key)) !== null && i < options.depth) {
|
|
126
127
|
i += 1;
|
|
127
|
-
if (!options.plainObjects && Object.prototype
|
|
128
|
+
if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
|
|
128
129
|
if (!options.allowPrototypes) {
|
|
129
|
-
|
|
130
|
+
return;
|
|
130
131
|
}
|
|
131
132
|
}
|
|
132
133
|
keys.push(segment[1]);
|
|
@@ -203,7 +204,18 @@ var defaults = {
|
|
|
203
204
|
encoder: Utils.encode
|
|
204
205
|
};
|
|
205
206
|
|
|
206
|
-
var
|
|
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 (
|
|
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 (
|
|
249
|
-
values = values.concat(stringify(
|
|
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(
|
|
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 ?
|
|
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 (
|
|
279
|
-
objKeys =
|
|
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(
|
|
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);
|
|
@@ -329,8 +372,10 @@ var hexTable = (function () {
|
|
|
329
372
|
return array;
|
|
330
373
|
}());
|
|
331
374
|
|
|
375
|
+
var has = Object.prototype.hasOwnProperty;
|
|
376
|
+
|
|
332
377
|
exports.arrayToObject = function (source, options) {
|
|
333
|
-
var obj = options.plainObjects ? Object.create(null) : {};
|
|
378
|
+
var obj = options && options.plainObjects ? Object.create(null) : {};
|
|
334
379
|
for (var i = 0; i < source.length; ++i) {
|
|
335
380
|
if (typeof source[i] !== 'undefined') {
|
|
336
381
|
obj[i] = source[i];
|
|
@@ -340,16 +385,32 @@ exports.arrayToObject = function (source, options) {
|
|
|
340
385
|
return obj;
|
|
341
386
|
};
|
|
342
387
|
|
|
343
|
-
|
|
388
|
+
var isArray = Array.isArray;
|
|
389
|
+
|
|
390
|
+
var arrayToObject = function arrayToObject(source, options) {
|
|
391
|
+
var obj = options && options.plainObjects ? Object.create(null) : {};
|
|
392
|
+
for (var i = 0; i < source.length; ++i) {
|
|
393
|
+
if (typeof source[i] !== 'undefined') {
|
|
394
|
+
obj[i] = source[i];
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return obj;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
exports.merge = function merge(target, source, options) {
|
|
402
|
+
/* eslint no-param-reassign: 0 */
|
|
344
403
|
if (!source) {
|
|
345
404
|
return target;
|
|
346
405
|
}
|
|
347
406
|
|
|
348
407
|
if (typeof source !== 'object') {
|
|
349
|
-
if (
|
|
408
|
+
if (isArray(target)) {
|
|
350
409
|
target.push(source);
|
|
351
|
-
} else if (typeof target === 'object') {
|
|
352
|
-
|
|
410
|
+
} else if (target && typeof target === 'object') {
|
|
411
|
+
if ((options && (options.plainObjects || options.allowPrototypes)) || !has.call(Object.prototype, source)) {
|
|
412
|
+
target[source] = true;
|
|
413
|
+
}
|
|
353
414
|
} else {
|
|
354
415
|
return [target, source];
|
|
355
416
|
}
|
|
@@ -357,20 +418,36 @@ exports.merge = function (target, source, options) {
|
|
|
357
418
|
return target;
|
|
358
419
|
}
|
|
359
420
|
|
|
360
|
-
if (typeof target !== 'object') {
|
|
421
|
+
if (!target || typeof target !== 'object') {
|
|
361
422
|
return [target].concat(source);
|
|
362
423
|
}
|
|
363
424
|
|
|
364
425
|
var mergeTarget = target;
|
|
365
|
-
if (
|
|
366
|
-
mergeTarget =
|
|
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;
|
|
367
444
|
}
|
|
368
445
|
|
|
369
446
|
return Object.keys(source).reduce(function (acc, key) {
|
|
370
447
|
var value = source[key];
|
|
371
448
|
|
|
372
|
-
if (
|
|
373
|
-
acc[key] =
|
|
449
|
+
if (has.call(acc, key)) {
|
|
450
|
+
acc[key] = merge(acc[key], value, options);
|
|
374
451
|
} else {
|
|
375
452
|
acc[key] = value;
|
|
376
453
|
}
|
|
@@ -400,13 +477,13 @@ exports.encode = function (str) {
|
|
|
400
477
|
var c = string.charCodeAt(i);
|
|
401
478
|
|
|
402
479
|
if (
|
|
403
|
-
c === 0x2D
|
|
404
|
-
c === 0x2E
|
|
405
|
-
c === 0x5F
|
|
406
|
-
c === 0x7E
|
|
407
|
-
(c >= 0x30 && c <= 0x39)
|
|
408
|
-
(c >= 0x41 && c <= 0x5A)
|
|
409
|
-
(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
|
|
410
487
|
) {
|
|
411
488
|
out += string.charAt(i);
|
|
412
489
|
continue;
|
|
@@ -429,7 +506,11 @@ exports.encode = function (str) {
|
|
|
429
506
|
|
|
430
507
|
i += 1;
|
|
431
508
|
c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
|
|
432
|
-
|
|
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)];
|
|
433
514
|
}
|
|
434
515
|
|
|
435
516
|
return out;
|
|
@@ -448,7 +529,7 @@ exports.compact = function (obj, references) {
|
|
|
448
529
|
|
|
449
530
|
refs.push(obj);
|
|
450
531
|
|
|
451
|
-
if (
|
|
532
|
+
if (isArray(obj)) {
|
|
452
533
|
var compacted = [];
|
|
453
534
|
|
|
454
535
|
for (var i = 0; i < obj.length; ++i) {
|
|
@@ -484,4 +565,4 @@ exports.isBuffer = function (obj) {
|
|
|
484
565
|
};
|
|
485
566
|
|
|
486
567
|
},{}]},{},[1])(1)
|
|
487
|
-
});
|
|
568
|
+
});
|
package/lib/index.js
CHANGED
|
File without changes
|
package/lib/parse.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var Utils = require('./utils');
|
|
4
4
|
|
|
5
|
+
var has = Object.prototype.hasOwnProperty;
|
|
6
|
+
|
|
5
7
|
var defaults = {
|
|
6
8
|
delimiter: '&',
|
|
7
9
|
depth: 5,
|
|
@@ -22,21 +24,18 @@ var parseValues = function parseValues(str, options) {
|
|
|
22
24
|
var part = parts[i];
|
|
23
25
|
var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
|
|
24
26
|
|
|
27
|
+
var key, val;
|
|
25
28
|
if (pos === -1) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (options.strictNullHandling) {
|
|
29
|
-
obj[options.decoder(part)] = null;
|
|
30
|
-
}
|
|
29
|
+
key = options.decoder(part);
|
|
30
|
+
val = options.strictNullHandling ? null : '';
|
|
31
31
|
} else {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
32
|
+
key = options.decoder(part.slice(0, pos));
|
|
33
|
+
val = options.decoder(part.slice(pos + 1));
|
|
34
|
+
}
|
|
35
|
+
if (has.call(obj, key)) {
|
|
36
|
+
obj[key] = [].concat(obj[key]).concat(val);
|
|
37
|
+
} else {
|
|
38
|
+
obj[key] = val;
|
|
40
39
|
}
|
|
41
40
|
}
|
|
42
41
|
|
|
@@ -51,23 +50,25 @@ var parseObject = function parseObject(chain, val, options) {
|
|
|
51
50
|
var root = chain.shift();
|
|
52
51
|
|
|
53
52
|
var obj;
|
|
54
|
-
if (root === '[]') {
|
|
53
|
+
if (root === '[]' && options.parseArrays) {
|
|
55
54
|
obj = [];
|
|
56
55
|
obj = obj.concat(parseObject(chain, val, options));
|
|
57
56
|
} else {
|
|
58
57
|
obj = options.plainObjects ? Object.create(null) : {};
|
|
59
|
-
var cleanRoot = root
|
|
58
|
+
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
|
|
60
59
|
var index = parseInt(cleanRoot, 10);
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
if (!options.parseArrays && cleanRoot === '') {
|
|
61
|
+
obj = { 0: val };
|
|
62
|
+
} else if (
|
|
63
|
+
!isNaN(index)
|
|
64
|
+
&& root !== cleanRoot
|
|
65
|
+
&& String(index) === cleanRoot
|
|
66
|
+
&& index >= 0
|
|
67
|
+
&& (options.parseArrays && index <= options.arrayLimit)
|
|
67
68
|
) {
|
|
68
69
|
obj = [];
|
|
69
70
|
obj[index] = parseObject(chain, val, options);
|
|
70
|
-
} else {
|
|
71
|
+
} else if (cleanRoot !== '__proto__') {
|
|
71
72
|
obj[cleanRoot] = parseObject(chain, val, options);
|
|
72
73
|
}
|
|
73
74
|
}
|
|
@@ -81,30 +82,30 @@ var parseKeys = function parseKeys(givenKey, val, options) {
|
|
|
81
82
|
}
|
|
82
83
|
|
|
83
84
|
// Transform dot notation to bracket notation
|
|
84
|
-
var key = options.allowDots ? givenKey.replace(/\.([
|
|
85
|
+
var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
|
|
85
86
|
|
|
86
87
|
// The regex chunks
|
|
87
88
|
|
|
88
|
-
var
|
|
89
|
-
var child = /(\[[
|
|
89
|
+
var brackets = /(\[[^[\]]*])/;
|
|
90
|
+
var child = /(\[[^[\]]*])/g;
|
|
90
91
|
|
|
91
92
|
// Get the parent
|
|
92
93
|
|
|
93
|
-
var segment =
|
|
94
|
+
var segment = brackets.exec(key);
|
|
95
|
+
var parent = segment ? key.slice(0, segment.index) : key;
|
|
94
96
|
|
|
95
97
|
// Stash the parent if it exists
|
|
96
98
|
|
|
97
99
|
var keys = [];
|
|
98
|
-
if (
|
|
99
|
-
// If we aren't using plain objects, optionally prefix keys
|
|
100
|
-
|
|
101
|
-
if (!options.plainObjects && Object.prototype.hasOwnProperty(segment[1])) {
|
|
100
|
+
if (parent) {
|
|
101
|
+
// If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
|
|
102
|
+
if (!options.plainObjects && has.call(Object.prototype, parent)) {
|
|
102
103
|
if (!options.allowPrototypes) {
|
|
103
104
|
return;
|
|
104
105
|
}
|
|
105
106
|
}
|
|
106
107
|
|
|
107
|
-
keys.push(
|
|
108
|
+
keys.push(parent);
|
|
108
109
|
}
|
|
109
110
|
|
|
110
111
|
// Loop through children appending to the array until we hit depth
|
|
@@ -112,9 +113,9 @@ var parseKeys = function parseKeys(givenKey, val, options) {
|
|
|
112
113
|
var i = 0;
|
|
113
114
|
while ((segment = child.exec(key)) !== null && i < options.depth) {
|
|
114
115
|
i += 1;
|
|
115
|
-
if (!options.plainObjects && Object.prototype
|
|
116
|
+
if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
|
|
116
117
|
if (!options.allowPrototypes) {
|
|
117
|
-
|
|
118
|
+
return;
|
|
118
119
|
}
|
|
119
120
|
}
|
|
120
121
|
keys.push(segment[1]);
|
package/lib/stringify.js
CHANGED
|
@@ -22,7 +22,18 @@ var defaults = {
|
|
|
22
22
|
encoder: Utils.encode
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
var
|
|
25
|
+
var isArray = Array.isArray;
|
|
26
|
+
var stringify = function stringify(
|
|
27
|
+
object,
|
|
28
|
+
prefix,
|
|
29
|
+
generateArrayPrefix,
|
|
30
|
+
strictNullHandling,
|
|
31
|
+
skipNulls,
|
|
32
|
+
encoder,
|
|
33
|
+
filter,
|
|
34
|
+
sort,
|
|
35
|
+
allowDots
|
|
36
|
+
) {
|
|
26
37
|
var obj = object;
|
|
27
38
|
if (typeof filter === 'function') {
|
|
28
39
|
obj = filter(prefix, obj);
|
|
@@ -50,7 +61,7 @@ var stringify = function stringify(object, prefix, generateArrayPrefix, strictNu
|
|
|
50
61
|
}
|
|
51
62
|
|
|
52
63
|
var objKeys;
|
|
53
|
-
if (
|
|
64
|
+
if (isArray(filter)) {
|
|
54
65
|
objKeys = filter;
|
|
55
66
|
} else {
|
|
56
67
|
var keys = Object.keys(obj);
|
|
@@ -64,10 +75,30 @@ var stringify = function stringify(object, prefix, generateArrayPrefix, strictNu
|
|
|
64
75
|
continue;
|
|
65
76
|
}
|
|
66
77
|
|
|
67
|
-
if (
|
|
68
|
-
values = values.concat(stringify(
|
|
78
|
+
if (isArray(obj)) {
|
|
79
|
+
values = values.concat(stringify(
|
|
80
|
+
obj[key],
|
|
81
|
+
generateArrayPrefix(prefix, key),
|
|
82
|
+
generateArrayPrefix,
|
|
83
|
+
strictNullHandling,
|
|
84
|
+
skipNulls,
|
|
85
|
+
encoder,
|
|
86
|
+
filter,
|
|
87
|
+
sort,
|
|
88
|
+
allowDots
|
|
89
|
+
));
|
|
69
90
|
} else {
|
|
70
|
-
values = values.concat(stringify(
|
|
91
|
+
values = values.concat(stringify(
|
|
92
|
+
obj[key],
|
|
93
|
+
prefix + (allowDots ? '.' + key : '[' + key + ']'),
|
|
94
|
+
generateArrayPrefix,
|
|
95
|
+
strictNullHandling,
|
|
96
|
+
skipNulls,
|
|
97
|
+
encoder,
|
|
98
|
+
filter,
|
|
99
|
+
sort,
|
|
100
|
+
allowDots
|
|
101
|
+
));
|
|
71
102
|
}
|
|
72
103
|
}
|
|
73
104
|
|
|
@@ -81,21 +112,22 @@ module.exports = function (object, opts) {
|
|
|
81
112
|
var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
|
|
82
113
|
var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
|
|
83
114
|
var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
|
|
84
|
-
var encoder = encode ?
|
|
115
|
+
var encoder = encode ? typeof options.encoder === 'function' ? options.encoder : defaults.encoder : null;
|
|
85
116
|
var sort = typeof options.sort === 'function' ? options.sort : null;
|
|
86
117
|
var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
|
|
87
118
|
var objKeys;
|
|
88
119
|
var filter;
|
|
89
120
|
|
|
90
|
-
if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
|
|
121
|
+
if (options.encoder !== null && typeof options.encoder !== 'undefined' && typeof options.encoder !== 'function') {
|
|
91
122
|
throw new TypeError('Encoder has to be a function.');
|
|
92
123
|
}
|
|
93
124
|
|
|
94
125
|
if (typeof options.filter === 'function') {
|
|
95
126
|
filter = options.filter;
|
|
96
127
|
obj = filter('', obj);
|
|
97
|
-
} else if (
|
|
98
|
-
objKeys =
|
|
128
|
+
} else if (isArray(options.filter)) {
|
|
129
|
+
objKeys = options.filter;
|
|
130
|
+
filter = options.filter;
|
|
99
131
|
}
|
|
100
132
|
|
|
101
133
|
var keys = [];
|
|
@@ -130,7 +162,17 @@ module.exports = function (object, opts) {
|
|
|
130
162
|
continue;
|
|
131
163
|
}
|
|
132
164
|
|
|
133
|
-
keys = keys.concat(stringify(
|
|
165
|
+
keys = keys.concat(stringify(
|
|
166
|
+
obj[key],
|
|
167
|
+
key,
|
|
168
|
+
generateArrayPrefix,
|
|
169
|
+
strictNullHandling,
|
|
170
|
+
skipNulls,
|
|
171
|
+
encoder,
|
|
172
|
+
filter,
|
|
173
|
+
sort,
|
|
174
|
+
allowDots
|
|
175
|
+
));
|
|
134
176
|
}
|
|
135
177
|
|
|
136
178
|
return keys.join(delimiter);
|