qs 6.2.3 → 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 +17 -0
- package/LICENSE.md +29 -0
- package/README.md +26 -2
- package/bower.json +21 -0
- package/component.json +15 -0
- package/dist/qs.js +117 -40
- package/lib/parse.js +11 -10
- package/lib/stringify.js +52 -10
- package/lib/utils.js +52 -18
- package/package.json +52 -47
- package/test/index.js +2 -0
- package/test/parse.js +98 -32
- package/test/stringify.js +11 -13
- package/test/utils.js +20 -0
- package/.eslintignore +0 -1
- package/LICENSE +0 -28
package/.editorconfig
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
root = true
|
|
2
|
+
|
|
3
|
+
[*]
|
|
4
|
+
indent_style = space
|
|
5
|
+
indent_size = 4
|
|
6
|
+
end_of_line = lf
|
|
7
|
+
charset = utf-8
|
|
8
|
+
trim_trailing_whitespace = true
|
|
9
|
+
insert_final_newline = true
|
|
10
|
+
max_line_length = 160
|
|
11
|
+
quote_type = single
|
|
12
|
+
|
|
13
|
+
[test/*]
|
|
14
|
+
max_line_length = off
|
|
15
|
+
|
|
16
|
+
[*.md]
|
|
17
|
+
indent_size = off
|
|
18
|
+
max_line_length = off
|
|
19
|
+
|
|
20
|
+
[*.json]
|
|
21
|
+
max_line_length = off
|
|
22
|
+
|
|
23
|
+
[Makefile]
|
|
24
|
+
max_line_length = off
|
|
25
|
+
|
|
26
|
+
[CHANGELOG.md]
|
|
27
|
+
indent_style = space
|
|
28
|
+
indent_size = 2
|
|
29
|
+
|
|
30
|
+
[LICENSE]
|
|
31
|
+
indent_size = 2
|
|
32
|
+
max_line_length = off
|
|
33
|
+
|
|
34
|
+
[coverage/**/*]
|
|
35
|
+
indent_size = off
|
|
36
|
+
indent_style = off
|
|
37
|
+
indent = off
|
|
38
|
+
max_line_length = off
|
|
39
|
+
|
|
40
|
+
[dist/*]
|
|
41
|
+
max_line_length = off
|
|
42
|
+
insert_final_newline = off
|
|
43
|
+
|
|
44
|
+
[.nycrc]
|
|
45
|
+
indent_style = off
|
package/.eslintrc
CHANGED
|
@@ -1,19 +1,39 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
2
|
+
"root": true,
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
"extends": "@ljharb",
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
"ignorePatterns": [
|
|
7
|
+
"dist/",
|
|
8
|
+
],
|
|
9
|
+
|
|
10
|
+
"rules": {
|
|
11
|
+
"complexity": [2, 29],
|
|
12
|
+
"consistent-return": 1,
|
|
13
|
+
"func-name-matching": 0,
|
|
14
|
+
"id-length": [2, { "min": 1, "max": 25, "properties": "never" }],
|
|
15
|
+
"indent": [2, 4],
|
|
16
|
+
"max-lines-per-function": 0,
|
|
17
|
+
"max-lines": 0,
|
|
18
|
+
"max-params": [2, 12],
|
|
19
|
+
"max-statements": [2, 45],
|
|
20
|
+
"multiline-comment-style": 0,
|
|
21
|
+
"no-continue": 1,
|
|
22
|
+
"no-magic-numbers": 0,
|
|
23
|
+
"no-param-reassign": 1,
|
|
24
|
+
"no-restricted-syntax": [2, "BreakStatement", "DebuggerStatement", "ForInStatement", "LabeledStatement", "WithStatement"],
|
|
25
|
+
"sort-keys": 0,
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
"overrides": [
|
|
29
|
+
{
|
|
30
|
+
"files": "test/**",
|
|
31
|
+
"rules": {
|
|
32
|
+
"max-lines-per-function": 0,
|
|
33
|
+
"max-statements": 0,
|
|
34
|
+
"no-extend-native": 0,
|
|
35
|
+
"function-paren-newline": 0,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
],
|
|
19
39
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# These are supported funding model platforms
|
|
2
|
+
|
|
3
|
+
github: [ljharb]
|
|
4
|
+
patreon: # Replace with a single Patreon username
|
|
5
|
+
open_collective: # Replace with a single Open Collective username
|
|
6
|
+
ko_fi: # Replace with a single Ko-fi username
|
|
7
|
+
tidelift: npm/qs
|
|
8
|
+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
9
|
+
liberapay: # Replace with a single Liberapay username
|
|
10
|
+
issuehunt: # Replace with a single IssueHunt username
|
|
11
|
+
otechie: # Replace with a single Otechie username
|
|
12
|
+
custom: # Replace with a single custom sponsorship URL
|
package/.nycrc
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
## **6.2.4**
|
|
2
|
+
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
3
|
+
- [Fix] `utils.merge`: avoid a crash with a null target and an array source
|
|
4
|
+
- [Fix] `utils.merge`: avoid a crash with a null target and a truthy non-array source
|
|
5
|
+
- [Fix] `utils`: `merge`: fix crash when `source` is a truthy primitive & no options are provided
|
|
6
|
+
- [Fix] when `parseArrays` is false, properly handle keys ending in `[]`
|
|
7
|
+
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
8
|
+
- [Refactor] use cached `Array.isArray`
|
|
9
|
+
- [Docs] Clarify the need for "arrayLimit" option
|
|
10
|
+
- [meta] fix README.md (#399)
|
|
11
|
+
- [meta] Clean up license text so it’s properly detected as BSD-3-Clause
|
|
12
|
+
- [meta] add FUNDING.yml
|
|
13
|
+
- [actions] backport actions from main
|
|
14
|
+
- [Tests] use `safer-buffer` instead of `Buffer` constructor
|
|
15
|
+
- [Tests] remove nonexistent tape option
|
|
16
|
+
- [Dev Deps] backport from main
|
|
17
|
+
|
|
1
18
|
## **6.2.3**
|
|
2
19
|
- [Fix] follow `allowPrototypes` option during merge (#201, #200)
|
|
3
20
|
- [Fix] chmod a-x
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014, Nathan LaFreniere and other [contributors](https://github.com/ljharb/qs/graphs/contributors)
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -169,7 +169,7 @@ assert.deepEqual(withIndexedEmptyString, { a: ['b', '', 'c'] });
|
|
|
169
169
|
```
|
|
170
170
|
|
|
171
171
|
**qs** will also limit specifying indices in an array to a maximum index of `20`. Any array members with an index of greater than `20` will
|
|
172
|
-
instead be converted to an object with the index as the key
|
|
172
|
+
instead be converted to an object with the index as the key. This is needed to handle cases when someone sent, for example, `a[999999999]` and it will take significant time to iterate over this huge array.
|
|
173
173
|
|
|
174
174
|
```javascript
|
|
175
175
|
var withMaxIndex = qs.parse('a[100]=b');
|
|
@@ -245,6 +245,30 @@ var decoded = qs.parse('x=z', { decoder: function (str) {
|
|
|
245
245
|
}})
|
|
246
246
|
```
|
|
247
247
|
|
|
248
|
+
You can encode keys and values using different logic by using the type argument provided to the encoder:
|
|
249
|
+
|
|
250
|
+
```javascript
|
|
251
|
+
var encoded = qs.stringify({ a: { b: 'c' } }, { encoder: function (str, defaultEncoder, charset, type) {
|
|
252
|
+
if (type === 'key') {
|
|
253
|
+
return // Encoded key
|
|
254
|
+
} else if (type === 'value') {
|
|
255
|
+
return // Encoded value
|
|
256
|
+
}
|
|
257
|
+
}})
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
The type argument is also provided to the decoder:
|
|
261
|
+
|
|
262
|
+
```javascript
|
|
263
|
+
var decoded = qs.parse('x=z', { decoder: function (str, defaultDecoder, charset, type) {
|
|
264
|
+
if (type === 'key') {
|
|
265
|
+
return // Decoded key
|
|
266
|
+
} else if (type === 'value') {
|
|
267
|
+
return // Decoded value
|
|
268
|
+
}
|
|
269
|
+
}})
|
|
270
|
+
```
|
|
271
|
+
|
|
248
272
|
Examples beyond this point will be shown as though the output is not URI encoded for clarity. Please note that the return values in these cases *will* be URI encoded during real usage.
|
|
249
273
|
|
|
250
274
|
When arrays are stringified, by default they are given explicit indices:
|
|
@@ -356,7 +380,7 @@ assert.equal(nullsSkipped, 'a=b');
|
|
|
356
380
|
|
|
357
381
|
### Dealing with special character sets
|
|
358
382
|
|
|
359
|
-
By default the encoding and decoding of characters is done in `utf-8`. If you
|
|
383
|
+
By default the encoding and decoding of characters is done in `utf-8`. If you
|
|
360
384
|
wish to encode querystrings to a different character set (i.e.
|
|
361
385
|
[Shift JIS](https://en.wikipedia.org/wiki/Shift_JIS)) you can use the
|
|
362
386
|
[`qs-iconv`](https://github.com/martinheidegger/qs-iconv) library:
|
package/bower.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "qs",
|
|
3
|
+
"main": "dist/qs.js",
|
|
4
|
+
"homepage": "https://github.com/hapijs/qs",
|
|
5
|
+
"authors": [
|
|
6
|
+
"Nathan LaFreniere <quitlahok@gmail.com>"
|
|
7
|
+
],
|
|
8
|
+
"description": "A querystring parser that supports nesting and arrays, with a depth limit",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"querystring",
|
|
11
|
+
"qs"
|
|
12
|
+
],
|
|
13
|
+
"license": "BSD-3-Clause",
|
|
14
|
+
"ignore": [
|
|
15
|
+
"**/.*",
|
|
16
|
+
"node_modules",
|
|
17
|
+
"bower_components",
|
|
18
|
+
"test",
|
|
19
|
+
"tests"
|
|
20
|
+
]
|
|
21
|
+
}
|
package/component.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "qs",
|
|
3
|
+
"repository": "hapijs/qs",
|
|
4
|
+
"description": "query-string parser / stringifier with nesting support",
|
|
5
|
+
"version": "6.2.4",
|
|
6
|
+
"keywords": ["querystring", "query", "parser"],
|
|
7
|
+
"main": "lib/index.js",
|
|
8
|
+
"scripts": [
|
|
9
|
+
"lib/index.js",
|
|
10
|
+
"lib/parse.js",
|
|
11
|
+
"lib/stringify.js",
|
|
12
|
+
"lib/utils.js"
|
|
13
|
+
],
|
|
14
|
+
"license": "BSD-3-Clause"
|
|
15
|
+
}
|
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');
|
|
@@ -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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
|
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);
|
|
@@ -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 (
|
|
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 (
|
|
370
|
-
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;
|
|
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] =
|
|
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)
|
|
412
|
-
(c >= 0x41 && c <= 0x5A)
|
|
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
|
-
|
|
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 (
|
|
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
|
+
});
|
package/lib/parse.js
CHANGED
|
@@ -50,23 +50,25 @@ var parseObject = function parseObject(chain, val, options) {
|
|
|
50
50
|
var root = chain.shift();
|
|
51
51
|
|
|
52
52
|
var obj;
|
|
53
|
-
if (root === '[]') {
|
|
53
|
+
if (root === '[]' && options.parseArrays) {
|
|
54
54
|
obj = [];
|
|
55
55
|
obj = obj.concat(parseObject(chain, val, options));
|
|
56
56
|
} else {
|
|
57
57
|
obj = options.plainObjects ? Object.create(null) : {};
|
|
58
58
|
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
|
|
59
59
|
var index = parseInt(cleanRoot, 10);
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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)
|
|
66
68
|
) {
|
|
67
69
|
obj = [];
|
|
68
70
|
obj[index] = parseObject(chain, val, options);
|
|
69
|
-
} else {
|
|
71
|
+
} else if (cleanRoot !== '__proto__') {
|
|
70
72
|
obj[cleanRoot] = parseObject(chain, val, options);
|
|
71
73
|
}
|
|
72
74
|
}
|
|
@@ -96,8 +98,7 @@ var parseKeys = function parseKeys(givenKey, val, options) {
|
|
|
96
98
|
|
|
97
99
|
var keys = [];
|
|
98
100
|
if (parent) {
|
|
99
|
-
// If we aren't using plain objects, optionally prefix keys
|
|
100
|
-
// that would overwrite object prototype properties
|
|
101
|
+
// If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
|
|
101
102
|
if (!options.plainObjects && has.call(Object.prototype, parent)) {
|
|
102
103
|
if (!options.allowPrototypes) {
|
|
103
104
|
return;
|