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/.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,36 @@
|
|
|
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
|
+
|
|
18
|
+
## **6.2.3**
|
|
19
|
+
- [Fix] follow `allowPrototypes` option during merge (#201, #200)
|
|
20
|
+
- [Fix] chmod a-x
|
|
21
|
+
- [Fix] support keys starting with brackets (#202, #200)
|
|
22
|
+
- [Tests] up to `node` `v7.7`, `v6.10`,` v4.8`; disable osx builds since they block linux builds
|
|
23
|
+
|
|
24
|
+
## **6.2.2**
|
|
25
|
+
- [Fix] ensure that `allowPrototypes: false` does not ever shadow Object.prototype properties
|
|
26
|
+
|
|
27
|
+
## **6.2.1**
|
|
28
|
+
- [Fix] ensure `key[]=x&key[]&key[]=y` results in 3, not 2, values
|
|
29
|
+
- [Refactor] Be explicit and use `Object.prototype.hasOwnProperty.call`
|
|
30
|
+
- [Tests] remove `parallelshell` since it does not reliably report failures
|
|
31
|
+
- [Tests] up to `node` `v6.3`, `v5.12`
|
|
32
|
+
- [Dev Deps] update `tape`, `eslint`, `@ljharb/eslint-config`, `qs-iconv`
|
|
33
|
+
|
|
1
34
|
## [**6.2.0**](https://github.com/ljharb/qs/issues?milestone=36&state=closed)
|
|
2
35
|
- [New] pass Buffers to the encoder/decoder directly (#161)
|
|
3
36
|
- [New] add "encoder" and "decoder" options, for custom param encoding/decoding (#160)
|
|
@@ -17,6 +50,9 @@
|
|
|
17
50
|
## [**6.0.0**](https://github.com/ljharb/qs/issues?milestone=31&state=closed)
|
|
18
51
|
- [**#124**](https://github.com/ljharb/qs/issues/124) Use ES6 and drop support for node < v4
|
|
19
52
|
|
|
53
|
+
## **5.2.1**
|
|
54
|
+
- [Fix] ensure `key[]=x&key[]&key[]=y` results in 3, not 2, values
|
|
55
|
+
|
|
20
56
|
## [**5.2.0**](https://github.com/ljharb/qs/issues?milestone=30&state=closed)
|
|
21
57
|
- [**#64**](https://github.com/ljharb/qs/issues/64) Add option to sort object keys in the query string
|
|
22
58
|
|
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
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
# qs
|
|
2
|
+
|
|
3
|
+
A querystring parsing and stringifying library with some added security.
|
|
4
|
+
|
|
5
|
+
[](http://travis-ci.org/ljharb/qs)
|
|
6
|
+
|
|
7
|
+
Lead Maintainer: [Jordan Harband](https://github.com/ljharb)
|
|
8
|
+
|
|
9
|
+
The **qs** module was originally created and maintained by [TJ Holowaychuk](https://github.com/visionmedia/node-querystring).
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
var qs = require('qs');
|
|
15
|
+
var assert = require('assert');
|
|
16
|
+
|
|
17
|
+
var obj = qs.parse('a=c');
|
|
18
|
+
assert.deepEqual(obj, { a: 'c' });
|
|
19
|
+
|
|
20
|
+
var str = qs.stringify(obj);
|
|
21
|
+
assert.equal(str, 'a=c');
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Parsing Objects
|
|
25
|
+
|
|
26
|
+
[](#preventEval)
|
|
27
|
+
```javascript
|
|
28
|
+
qs.parse(string, [options]);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`.
|
|
32
|
+
For example, the string `'foo[bar]=baz'` converts to:
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
assert.deepEqual(qs.parse('foo[bar]=baz'), {
|
|
36
|
+
foo: {
|
|
37
|
+
bar: 'baz'
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
When using the `plainObjects` option the parsed value is returned as a plain object, created via `Object.create(null)` and as such you should be aware that prototype methods will not exist on it and a user may set those names to whatever value they like:
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
var plainObject = qs.parse('a[hasOwnProperty]=b', { plainObjects: true });
|
|
46
|
+
assert.deepEqual(plainObject, { a: { hasOwnProperty: 'b' } });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
By default parameters that would overwrite properties on the object prototype are ignored, if you wish to keep the data from those fields either use `plainObjects` as mentioned above, or set `allowPrototypes` to `true` which will allow user input to overwrite those properties. *WARNING* It is generally a bad idea to enable this option as it can cause problems when attempting to use the properties that have been overwritten. Always be careful with this option.
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
var protoObject = qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true });
|
|
53
|
+
assert.deepEqual(protoObject, { a: { hasOwnProperty: 'b' } });
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
URI encoded strings work too:
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
assert.deepEqual(qs.parse('a%5Bb%5D=c'), {
|
|
60
|
+
a: { b: 'c' }
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
You can also nest your objects, like `'foo[bar][baz]=foobarbaz'`:
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
assert.deepEqual(qs.parse('foo[bar][baz]=foobarbaz'), {
|
|
68
|
+
foo: {
|
|
69
|
+
bar: {
|
|
70
|
+
baz: 'foobarbaz'
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
By default, when nesting objects **qs** will only parse up to 5 children deep. This means if you attempt to parse a string like
|
|
77
|
+
`'a[b][c][d][e][f][g][h][i]=j'` your resulting object will be:
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
var expected = {
|
|
81
|
+
a: {
|
|
82
|
+
b: {
|
|
83
|
+
c: {
|
|
84
|
+
d: {
|
|
85
|
+
e: {
|
|
86
|
+
f: {
|
|
87
|
+
'[g][h][i]': 'j'
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
var string = 'a[b][c][d][e][f][g][h][i]=j';
|
|
96
|
+
assert.deepEqual(qs.parse(string), expected);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
This depth can be overridden by passing a `depth` option to `qs.parse(string, [options])`:
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
var deep = qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });
|
|
103
|
+
assert.deepEqual(deep, { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } });
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The depth limit helps mitigate abuse when **qs** is used to parse user input, and it is recommended to keep it a reasonably small number.
|
|
107
|
+
|
|
108
|
+
For similar reasons, by default **qs** will only parse up to 1000 parameters. This can be overridden by passing a `parameterLimit` option:
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
var limited = qs.parse('a=b&c=d', { parameterLimit: 1 });
|
|
112
|
+
assert.deepEqual(limited, { a: 'b' });
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
An optional delimiter can also be passed:
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
var delimited = qs.parse('a=b;c=d', { delimiter: ';' });
|
|
119
|
+
assert.deepEqual(delimited, { a: 'b', c: 'd' });
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Delimiters can be a regular expression too:
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
var regexed = qs.parse('a=b;c=d,e=f', { delimiter: /[;,]/ });
|
|
126
|
+
assert.deepEqual(regexed, { a: 'b', c: 'd', e: 'f' });
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Option `allowDots` can be used to enable dot notation:
|
|
130
|
+
|
|
131
|
+
```javascript
|
|
132
|
+
var withDots = qs.parse('a.b=c', { allowDots: true });
|
|
133
|
+
assert.deepEqual(withDots, { a: { b: 'c' } });
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Parsing Arrays
|
|
137
|
+
|
|
138
|
+
**qs** can also parse arrays using a similar `[]` notation:
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
var withArray = qs.parse('a[]=b&a[]=c');
|
|
142
|
+
assert.deepEqual(withArray, { a: ['b', 'c'] });
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
You may specify an index as well:
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
var withIndexes = qs.parse('a[1]=c&a[0]=b');
|
|
149
|
+
assert.deepEqual(withIndexes, { a: ['b', 'c'] });
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Note that the only difference between an index in an array and a key in an object is that the value between the brackets must be a number
|
|
153
|
+
to create an array. When creating arrays with specific indices, **qs** will compact a sparse array to only the existing values preserving
|
|
154
|
+
their order:
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
var noSparse = qs.parse('a[1]=b&a[15]=c');
|
|
158
|
+
assert.deepEqual(noSparse, { a: ['b', 'c'] });
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Note that an empty string is also a value, and will be preserved:
|
|
162
|
+
|
|
163
|
+
```javascript
|
|
164
|
+
var withEmptyString = qs.parse('a[]=&a[]=b');
|
|
165
|
+
assert.deepEqual(withEmptyString, { a: ['', 'b'] });
|
|
166
|
+
|
|
167
|
+
var withIndexedEmptyString = qs.parse('a[0]=b&a[1]=&a[2]=c');
|
|
168
|
+
assert.deepEqual(withIndexedEmptyString, { a: ['b', '', 'c'] });
|
|
169
|
+
```
|
|
170
|
+
|
|
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. 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
|
+
|
|
174
|
+
```javascript
|
|
175
|
+
var withMaxIndex = qs.parse('a[100]=b');
|
|
176
|
+
assert.deepEqual(withMaxIndex, { a: { '100': 'b' } });
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
This limit can be overridden by passing an `arrayLimit` option:
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
var withArrayLimit = qs.parse('a[1]=b', { arrayLimit: 0 });
|
|
183
|
+
assert.deepEqual(withArrayLimit, { a: { '1': 'b' } });
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
To disable array parsing entirely, set `parseArrays` to `false`.
|
|
187
|
+
|
|
188
|
+
```javascript
|
|
189
|
+
var noParsingArrays = qs.parse('a[]=b', { parseArrays: false });
|
|
190
|
+
assert.deepEqual(noParsingArrays, { a: { '0': 'b' } });
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
If you mix notations, **qs** will merge the two items into an object:
|
|
194
|
+
|
|
195
|
+
```javascript
|
|
196
|
+
var mixedNotation = qs.parse('a[0]=b&a[b]=c');
|
|
197
|
+
assert.deepEqual(mixedNotation, { a: { '0': 'b', b: 'c' } });
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
You can also create arrays of objects:
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
var arraysOfObjects = qs.parse('a[][b]=c');
|
|
204
|
+
assert.deepEqual(arraysOfObjects, { a: [{ b: 'c' }] });
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Stringifying
|
|
208
|
+
|
|
209
|
+
[](#preventEval)
|
|
210
|
+
```javascript
|
|
211
|
+
qs.stringify(object, [options]);
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
When stringifying, **qs** by default URI encodes output. Objects are stringified as you would expect:
|
|
215
|
+
|
|
216
|
+
```javascript
|
|
217
|
+
assert.equal(qs.stringify({ a: 'b' }), 'a=b');
|
|
218
|
+
assert.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
This encoding can be disabled by setting the `encode` option to `false`:
|
|
222
|
+
|
|
223
|
+
```javascript
|
|
224
|
+
var unencoded = qs.stringify({ a: { b: 'c' } }, { encode: false });
|
|
225
|
+
assert.equal(unencoded, 'a[b]=c');
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
This encoding can also be replaced by a custom encoding method set as `encoder` option:
|
|
229
|
+
|
|
230
|
+
```javascript
|
|
231
|
+
var encoded = qs.stringify({ a: { b: 'c' } }, { encoder: function (str) {
|
|
232
|
+
// Passed in values `a`, `b`, `c`
|
|
233
|
+
return // Return encoded string
|
|
234
|
+
}})
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
_(Note: the `encoder` option does not apply if `encode` is `false`)_
|
|
238
|
+
|
|
239
|
+
Analogue to the `encoder` there is a `decoder` option for `parse` to override decoding of properties and values:
|
|
240
|
+
|
|
241
|
+
```javascript
|
|
242
|
+
var decoded = qs.parse('x=z', { decoder: function (str) {
|
|
243
|
+
// Passed in values `x`, `z`
|
|
244
|
+
return // Return decoded string
|
|
245
|
+
}})
|
|
246
|
+
```
|
|
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
|
+
|
|
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.
|
|
273
|
+
|
|
274
|
+
When arrays are stringified, by default they are given explicit indices:
|
|
275
|
+
|
|
276
|
+
```javascript
|
|
277
|
+
qs.stringify({ a: ['b', 'c', 'd'] });
|
|
278
|
+
// 'a[0]=b&a[1]=c&a[2]=d'
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
You may override this by setting the `indices` option to `false`:
|
|
282
|
+
|
|
283
|
+
```javascript
|
|
284
|
+
qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false });
|
|
285
|
+
// 'a=b&a=c&a=d'
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
You may use the `arrayFormat` option to specify the format of the output array
|
|
289
|
+
|
|
290
|
+
```javascript
|
|
291
|
+
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })
|
|
292
|
+
// 'a[0]=b&a[1]=c'
|
|
293
|
+
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })
|
|
294
|
+
// 'a[]=b&a[]=c'
|
|
295
|
+
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })
|
|
296
|
+
// 'a=b&a=c'
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Empty strings and null values will omit the value, but the equals sign (=) remains in place:
|
|
300
|
+
|
|
301
|
+
```javascript
|
|
302
|
+
assert.equal(qs.stringify({ a: '' }), 'a=');
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
Properties that are set to `undefined` will be omitted entirely:
|
|
306
|
+
|
|
307
|
+
```javascript
|
|
308
|
+
assert.equal(qs.stringify({ a: null, b: undefined }), 'a=');
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
The delimiter may be overridden with stringify as well:
|
|
312
|
+
|
|
313
|
+
```javascript
|
|
314
|
+
assert.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d');
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Finally, you can use the `filter` option to restrict which keys will be included in the stringified output.
|
|
318
|
+
If you pass a function, it will be called for each key to obtain the replacement value. Otherwise, if you
|
|
319
|
+
pass an array, it will be used to select properties and array indices for stringification:
|
|
320
|
+
|
|
321
|
+
```javascript
|
|
322
|
+
function filterFunc(prefix, value) {
|
|
323
|
+
if (prefix == 'b') {
|
|
324
|
+
// Return an `undefined` value to omit a property.
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (prefix == 'e[f]') {
|
|
328
|
+
return value.getTime();
|
|
329
|
+
}
|
|
330
|
+
if (prefix == 'e[g][0]') {
|
|
331
|
+
return value * 2;
|
|
332
|
+
}
|
|
333
|
+
return value;
|
|
334
|
+
}
|
|
335
|
+
qs.stringify({ a: 'b', c: 'd', e: { f: new Date(123), g: [2] } }, { filter: filterFunc });
|
|
336
|
+
// 'a=b&c=d&e[f]=123&e[g][0]=4'
|
|
337
|
+
qs.stringify({ a: 'b', c: 'd', e: 'f' }, { filter: ['a', 'e'] });
|
|
338
|
+
// 'a=b&e=f'
|
|
339
|
+
qs.stringify({ a: ['b', 'c', 'd'], e: 'f' }, { filter: ['a', 0, 2] });
|
|
340
|
+
// 'a[0]=b&a[2]=d'
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Handling of `null` values
|
|
344
|
+
|
|
345
|
+
By default, `null` values are treated like empty strings:
|
|
346
|
+
|
|
347
|
+
```javascript
|
|
348
|
+
var withNull = qs.stringify({ a: null, b: '' });
|
|
349
|
+
assert.equal(withNull, 'a=&b=');
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
Parsing does not distinguish between parameters with and without equal signs. Both are converted to empty strings.
|
|
353
|
+
|
|
354
|
+
```javascript
|
|
355
|
+
var equalsInsensitive = qs.parse('a&b=');
|
|
356
|
+
assert.deepEqual(equalsInsensitive, { a: '', b: '' });
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
To distinguish between `null` values and empty strings use the `strictNullHandling` flag. In the result string the `null`
|
|
360
|
+
values have no `=` sign:
|
|
361
|
+
|
|
362
|
+
```javascript
|
|
363
|
+
var strictNull = qs.stringify({ a: null, b: '' }, { strictNullHandling: true });
|
|
364
|
+
assert.equal(strictNull, 'a&b=');
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
To parse values without `=` back to `null` use the `strictNullHandling` flag:
|
|
368
|
+
|
|
369
|
+
```javascript
|
|
370
|
+
var parsedStrictNull = qs.parse('a&b=', { strictNullHandling: true });
|
|
371
|
+
assert.deepEqual(parsedStrictNull, { a: null, b: '' });
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
To completely skip rendering keys with `null` values, use the `skipNulls` flag:
|
|
375
|
+
|
|
376
|
+
```javascript
|
|
377
|
+
var nullsSkipped = qs.stringify({ a: 'b', c: null}, { skipNulls: true });
|
|
378
|
+
assert.equal(nullsSkipped, 'a=b');
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
### Dealing with special character sets
|
|
382
|
+
|
|
383
|
+
By default the encoding and decoding of characters is done in `utf-8`. If you
|
|
384
|
+
wish to encode querystrings to a different character set (i.e.
|
|
385
|
+
[Shift JIS](https://en.wikipedia.org/wiki/Shift_JIS)) you can use the
|
|
386
|
+
[`qs-iconv`](https://github.com/martinheidegger/qs-iconv) library:
|
|
387
|
+
|
|
388
|
+
```javascript
|
|
389
|
+
var encoder = require('qs-iconv/encoder')('shift_jis');
|
|
390
|
+
var shiftJISEncoded = qs.stringify({ a: 'こんにちは!' }, { encoder: encoder });
|
|
391
|
+
assert.equal(shiftJISEncoded, 'a=%82%B1%82%F1%82%C9%82%BF%82%CD%81I');
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
This also works for decoding of query strings:
|
|
395
|
+
|
|
396
|
+
```javascript
|
|
397
|
+
var decoder = require('qs-iconv/decoder')('shift_jis');
|
|
398
|
+
var obj = qs.parse('a=%82%B1%82%F1%82%C9%82%BF%82%CD%81I', { decoder: decoder });
|
|
399
|
+
assert.deepEqual(obj, { a: 'こんにちは!' });
|
|
400
|
+
```
|
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
|
+
}
|