query-string 6.8.2 → 6.10.1
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/index.d.ts +85 -5
- package/index.js +61 -13
- package/package.json +2 -1
- package/readme.md +135 -23
package/index.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface ParseOptions {
|
|
|
12
12
|
- `bracket`: Parse arrays with bracket representation:
|
|
13
13
|
|
|
14
14
|
```
|
|
15
|
+
import queryString = require('query-string');
|
|
16
|
+
|
|
15
17
|
queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
|
|
16
18
|
//=> {foo: ['1', '2', '3']}
|
|
17
19
|
```
|
|
@@ -19,6 +21,8 @@ export interface ParseOptions {
|
|
|
19
21
|
- `index`: Parse arrays with index representation:
|
|
20
22
|
|
|
21
23
|
```
|
|
24
|
+
import queryString = require('query-string');
|
|
25
|
+
|
|
22
26
|
queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
|
|
23
27
|
//=> {foo: ['1', '2', '3']}
|
|
24
28
|
```
|
|
@@ -26,6 +30,8 @@ export interface ParseOptions {
|
|
|
26
30
|
- `comma`: Parse arrays with elements separated by comma:
|
|
27
31
|
|
|
28
32
|
```
|
|
33
|
+
import queryString = require('query-string');
|
|
34
|
+
|
|
29
35
|
queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
|
|
30
36
|
//=> {foo: ['1', '2', '3']}
|
|
31
37
|
```
|
|
@@ -33,6 +39,8 @@ export interface ParseOptions {
|
|
|
33
39
|
- `none`: Parse arrays with elements using duplicate keys:
|
|
34
40
|
|
|
35
41
|
```
|
|
42
|
+
import queryString = require('query-string');
|
|
43
|
+
|
|
36
44
|
queryString.parse('foo=1&foo=2&foo=3');
|
|
37
45
|
//=> {foo: ['1', '2', '3']}
|
|
38
46
|
```
|
|
@@ -48,16 +56,22 @@ export interface ParseOptions {
|
|
|
48
56
|
|
|
49
57
|
@example
|
|
50
58
|
```
|
|
59
|
+
import queryString = require('query-string');
|
|
60
|
+
|
|
51
61
|
const order = ['c', 'a', 'b'];
|
|
52
62
|
|
|
53
63
|
queryString.parse('?a=one&b=two&c=three', {
|
|
54
64
|
sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
|
|
55
65
|
});
|
|
56
|
-
|
|
66
|
+
//=> {c: 'three', a: 'one', b: 'two'}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
@example
|
|
57
70
|
```
|
|
71
|
+
import queryString = require('query-string');
|
|
58
72
|
|
|
59
73
|
queryString.parse('?a=one&c=three&b=two', {sort: false});
|
|
60
|
-
|
|
74
|
+
//=> {a: 'one', c: 'three', b: 'two'}
|
|
61
75
|
```
|
|
62
76
|
*/
|
|
63
77
|
readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
|
|
@@ -68,7 +82,9 @@ export interface ParseOptions {
|
|
|
68
82
|
@default false
|
|
69
83
|
|
|
70
84
|
@example
|
|
71
|
-
```
|
|
85
|
+
```
|
|
86
|
+
import queryString = require('query-string');
|
|
87
|
+
|
|
72
88
|
queryString.parse('foo=1', {parseNumbers: true});
|
|
73
89
|
//=> {foo: 1}
|
|
74
90
|
```
|
|
@@ -82,6 +98,8 @@ export interface ParseOptions {
|
|
|
82
98
|
|
|
83
99
|
@example
|
|
84
100
|
```
|
|
101
|
+
import queryString = require('query-string');
|
|
102
|
+
|
|
85
103
|
queryString.parse('foo=true', {parseBooleans: true});
|
|
86
104
|
//=> {foo: true}
|
|
87
105
|
```
|
|
@@ -117,6 +135,8 @@ Extract the URL and the query string as an object.
|
|
|
117
135
|
|
|
118
136
|
@example
|
|
119
137
|
```
|
|
138
|
+
import queryString = require('query-string');
|
|
139
|
+
|
|
120
140
|
queryString.parseUrl('https://foo.bar?foo=bar');
|
|
121
141
|
//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
|
|
122
142
|
```
|
|
@@ -144,6 +164,8 @@ export interface StringifyOptions {
|
|
|
144
164
|
- `bracket`: Serialize arrays using bracket representation:
|
|
145
165
|
|
|
146
166
|
```
|
|
167
|
+
import queryString = require('query-string');
|
|
168
|
+
|
|
147
169
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
|
|
148
170
|
//=> 'foo[]=1&foo[]=2&foo[]=3'
|
|
149
171
|
```
|
|
@@ -151,6 +173,8 @@ export interface StringifyOptions {
|
|
|
151
173
|
- `index`: Serialize arrays using index representation:
|
|
152
174
|
|
|
153
175
|
```
|
|
176
|
+
import queryString = require('query-string');
|
|
177
|
+
|
|
154
178
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
|
|
155
179
|
//=> 'foo[0]=1&foo[1]=2&foo[2]=3'
|
|
156
180
|
```
|
|
@@ -158,6 +182,8 @@ export interface StringifyOptions {
|
|
|
158
182
|
- `comma`: Serialize arrays by separating elements with comma:
|
|
159
183
|
|
|
160
184
|
```
|
|
185
|
+
import queryString = require('query-string');
|
|
186
|
+
|
|
161
187
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
|
|
162
188
|
//=> 'foo=1,2,3'
|
|
163
189
|
```
|
|
@@ -165,6 +191,8 @@ export interface StringifyOptions {
|
|
|
165
191
|
- `none`: Serialize arrays by using duplicate keys:
|
|
166
192
|
|
|
167
193
|
```
|
|
194
|
+
import queryString = require('query-string');
|
|
195
|
+
|
|
168
196
|
queryString.stringify({foo: [1, 2, 3]});
|
|
169
197
|
//=> 'foo=1&foo=2&foo=3'
|
|
170
198
|
```
|
|
@@ -180,18 +208,49 @@ export interface StringifyOptions {
|
|
|
180
208
|
|
|
181
209
|
@example
|
|
182
210
|
```
|
|
211
|
+
import queryString = require('query-string');
|
|
212
|
+
|
|
183
213
|
const order = ['c', 'a', 'b'];
|
|
184
214
|
|
|
185
215
|
queryString.stringify({a: 1, b: 2, c: 3}, {
|
|
186
216
|
sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
|
|
187
217
|
});
|
|
188
|
-
|
|
218
|
+
//=> 'c=3&a=1&b=2'
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
@example
|
|
222
|
+
```
|
|
223
|
+
import queryString = require('query-string');
|
|
189
224
|
|
|
190
225
|
queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
|
|
191
|
-
|
|
226
|
+
//=> 'b=1&c=2&a=3'
|
|
192
227
|
```
|
|
193
228
|
*/
|
|
194
229
|
readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
Skip keys with `null` as the value.
|
|
233
|
+
|
|
234
|
+
Note that keys with `undefined` as the value are always skipped.
|
|
235
|
+
|
|
236
|
+
@default false
|
|
237
|
+
|
|
238
|
+
@example
|
|
239
|
+
```
|
|
240
|
+
import queryString = require('query-string');
|
|
241
|
+
|
|
242
|
+
queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
|
|
243
|
+
skipNull: true
|
|
244
|
+
});
|
|
245
|
+
//=> 'a=1&d=4'
|
|
246
|
+
|
|
247
|
+
queryString.stringify({a: undefined, b: null}, {
|
|
248
|
+
skipNull: true
|
|
249
|
+
});
|
|
250
|
+
//=> ''
|
|
251
|
+
```
|
|
252
|
+
*/
|
|
253
|
+
readonly skipNull?: boolean;
|
|
195
254
|
}
|
|
196
255
|
|
|
197
256
|
/**
|
|
@@ -204,5 +263,26 @@ export function stringify(
|
|
|
204
263
|
|
|
205
264
|
/**
|
|
206
265
|
Extract a query string from a URL that can be passed into `.parse()`.
|
|
266
|
+
|
|
267
|
+
Note: This behaviour can be changed with the `skipNull` option.
|
|
207
268
|
*/
|
|
208
269
|
export function extract(url: string): string;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
|
|
273
|
+
|
|
274
|
+
Query items in the `query` property overrides queries in the `url` property.
|
|
275
|
+
|
|
276
|
+
@example
|
|
277
|
+
```
|
|
278
|
+
queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
|
|
279
|
+
//=> 'https://foo.bar?foo=bar'
|
|
280
|
+
|
|
281
|
+
queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
|
|
282
|
+
//=> 'https://foo.bar?foo=bar'
|
|
283
|
+
```
|
|
284
|
+
*/
|
|
285
|
+
export function stringifyUrl(
|
|
286
|
+
object: ParsedUrl,
|
|
287
|
+
options?: StringifyOptions
|
|
288
|
+
): string;
|
package/index.js
CHANGED
|
@@ -8,7 +8,7 @@ function encoderForArrayFormat(options) {
|
|
|
8
8
|
case 'index':
|
|
9
9
|
return key => (result, value) => {
|
|
10
10
|
const index = result.length;
|
|
11
|
-
if (value === undefined) {
|
|
11
|
+
if (value === undefined || (options.skipNull && value === null)) {
|
|
12
12
|
return result;
|
|
13
13
|
}
|
|
14
14
|
|
|
@@ -24,7 +24,7 @@ function encoderForArrayFormat(options) {
|
|
|
24
24
|
|
|
25
25
|
case 'bracket':
|
|
26
26
|
return key => (result, value) => {
|
|
27
|
-
if (value === undefined) {
|
|
27
|
+
if (value === undefined || (options.skipNull && value === null)) {
|
|
28
28
|
return result;
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -36,12 +36,12 @@ function encoderForArrayFormat(options) {
|
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
case 'comma':
|
|
39
|
-
return key => (result, value
|
|
39
|
+
return key => (result, value) => {
|
|
40
40
|
if (value === null || value === undefined || value.length === 0) {
|
|
41
41
|
return result;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
if (
|
|
44
|
+
if (result.length === 0) {
|
|
45
45
|
return [[encode(key, options), '=', encode(value, options)].join('')];
|
|
46
46
|
}
|
|
47
47
|
|
|
@@ -50,7 +50,7 @@ function encoderForArrayFormat(options) {
|
|
|
50
50
|
|
|
51
51
|
default:
|
|
52
52
|
return key => (result, value) => {
|
|
53
|
-
if (value === undefined) {
|
|
53
|
+
if (value === undefined || (options.skipNull && value === null)) {
|
|
54
54
|
return result;
|
|
55
55
|
}
|
|
56
56
|
|
|
@@ -161,6 +161,16 @@ function removeHash(input) {
|
|
|
161
161
|
return input;
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
+
function getHash(url) {
|
|
165
|
+
let hash = '';
|
|
166
|
+
const hashStart = url.indexOf('#');
|
|
167
|
+
if (hashStart !== -1) {
|
|
168
|
+
hash = url.slice(hashStart);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return hash;
|
|
172
|
+
}
|
|
173
|
+
|
|
164
174
|
function extract(input) {
|
|
165
175
|
input = removeHash(input);
|
|
166
176
|
const queryStart = input.indexOf('?');
|
|
@@ -171,6 +181,16 @@ function extract(input) {
|
|
|
171
181
|
return input.slice(queryStart + 1);
|
|
172
182
|
}
|
|
173
183
|
|
|
184
|
+
function parseValue(value, options) {
|
|
185
|
+
if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
|
|
186
|
+
value = Number(value);
|
|
187
|
+
} else if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
|
|
188
|
+
value = value.toLowerCase() === 'true';
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return value;
|
|
192
|
+
}
|
|
193
|
+
|
|
174
194
|
function parse(input, options) {
|
|
175
195
|
options = Object.assign({
|
|
176
196
|
decode: true,
|
|
@@ -196,19 +216,23 @@ function parse(input, options) {
|
|
|
196
216
|
}
|
|
197
217
|
|
|
198
218
|
for (const param of input.split('&')) {
|
|
199
|
-
let [key, value] = splitOnFirst(param.replace(/\+/g, ' '), '=');
|
|
219
|
+
let [key, value] = splitOnFirst(options.decode ? param.replace(/\+/g, ' ') : param, '=');
|
|
200
220
|
|
|
201
221
|
// Missing `=` should be `null`:
|
|
202
222
|
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
|
|
203
223
|
value = value === undefined ? null : decode(value, options);
|
|
224
|
+
formatter(decode(key, options), value, ret);
|
|
225
|
+
}
|
|
204
226
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
227
|
+
for (const key of Object.keys(ret)) {
|
|
228
|
+
const value = ret[key];
|
|
229
|
+
if (typeof value === 'object' && value !== null) {
|
|
230
|
+
for (const k of Object.keys(value)) {
|
|
231
|
+
value[k] = parseValue(value[k], options);
|
|
232
|
+
}
|
|
233
|
+
} else {
|
|
234
|
+
ret[key] = parseValue(value, options);
|
|
209
235
|
}
|
|
210
|
-
|
|
211
|
-
formatter(decode(key, options), value, ret);
|
|
212
236
|
}
|
|
213
237
|
|
|
214
238
|
if (options.sort === false) {
|
|
@@ -243,7 +267,17 @@ exports.stringify = (object, options) => {
|
|
|
243
267
|
}, options);
|
|
244
268
|
|
|
245
269
|
const formatter = encoderForArrayFormat(options);
|
|
246
|
-
|
|
270
|
+
|
|
271
|
+
const objectCopy = Object.assign({}, object);
|
|
272
|
+
if (options.skipNull) {
|
|
273
|
+
for (const key of Object.keys(objectCopy)) {
|
|
274
|
+
if (objectCopy[key] === undefined || objectCopy[key] === null) {
|
|
275
|
+
delete objectCopy[key];
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const keys = Object.keys(objectCopy);
|
|
247
281
|
|
|
248
282
|
if (options.sort !== false) {
|
|
249
283
|
keys.sort(options.sort);
|
|
@@ -276,3 +310,17 @@ exports.parseUrl = (input, options) => {
|
|
|
276
310
|
query: parse(extract(input), options)
|
|
277
311
|
};
|
|
278
312
|
};
|
|
313
|
+
|
|
314
|
+
exports.stringifyUrl = (input, options) => {
|
|
315
|
+
const url = removeHash(input.url).split('?')[0] || '';
|
|
316
|
+
const queryFromUrl = exports.extract(input.url);
|
|
317
|
+
const parsedQueryFromUrl = exports.parse(queryFromUrl);
|
|
318
|
+
const hash = getHash(input.url);
|
|
319
|
+
const query = Object.assign(parsedQueryFromUrl, input.query);
|
|
320
|
+
let queryString = exports.stringify(query, options);
|
|
321
|
+
if (queryString) {
|
|
322
|
+
queryString = `?${queryString}`;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return `${url}${queryString}${hash}`;
|
|
326
|
+
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "query-string",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.10.1",
|
|
4
4
|
"description": "Parse and stringify URL query strings",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/query-string",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
7
8
|
"author": {
|
|
8
9
|
"name": "Sindre Sorhus",
|
|
9
10
|
"email": "sindresorhus@gmail.com",
|
package/readme.md
CHANGED
|
@@ -2,6 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
> Parse and stringify URL [query strings](https://en.wikipedia.org/wiki/Query_string)
|
|
4
4
|
|
|
5
|
+
<br>
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
<div align="center">
|
|
10
|
+
<p>
|
|
11
|
+
<p>
|
|
12
|
+
<sup>
|
|
13
|
+
<a href="https://github.com/sponsors/sindresorhus">My open source work is supported by the community</a>
|
|
14
|
+
</sup>
|
|
15
|
+
</p>
|
|
16
|
+
<sup>Special thanks to:</sup>
|
|
17
|
+
<br>
|
|
18
|
+
<br>
|
|
19
|
+
<a href="https://github.com/botpress/botpress">
|
|
20
|
+
<img src="https://sindresorhus.com/assets/thanks/botpress-logo.svg" width="260" alt="Botpress">
|
|
21
|
+
</a>
|
|
22
|
+
<br>
|
|
23
|
+
<sub><b>Botpress is an open-source conversational assistant creation platform.</b></sub>
|
|
24
|
+
<br>
|
|
25
|
+
<sub>They <a href="https://github.com/botpress/botpress/blob/master/.github/CONTRIBUTING.md">welcome contributions</a> from anyone, whether you're into machine learning,<br>want to get started in open-source, or just have an improvement idea.</sub>
|
|
26
|
+
<br>
|
|
27
|
+
</p>
|
|
28
|
+
</div>
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
<br>
|
|
5
33
|
|
|
6
34
|
## Install
|
|
7
35
|
|
|
@@ -9,8 +37,7 @@
|
|
|
9
37
|
$ npm install query-string
|
|
10
38
|
```
|
|
11
39
|
|
|
12
|
-
This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or,
|
|
13
|
-
|
|
40
|
+
This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or, if your project is using create-react-app v1, use version 5: `npm install query-string@5`.
|
|
14
41
|
|
|
15
42
|
## Usage
|
|
16
43
|
|
|
@@ -43,7 +70,6 @@ console.log(location.search);
|
|
|
43
70
|
//=> '?foo=unicorn&ilike=pizza'
|
|
44
71
|
```
|
|
45
72
|
|
|
46
|
-
|
|
47
73
|
## API
|
|
48
74
|
|
|
49
75
|
### .parse(string, options?)
|
|
@@ -58,19 +84,21 @@ Type: `object`
|
|
|
58
84
|
|
|
59
85
|
##### decode
|
|
60
86
|
|
|
61
|
-
Type: `boolean
|
|
87
|
+
Type: `boolean`\
|
|
62
88
|
Default: `true`
|
|
63
89
|
|
|
64
90
|
Decode the keys and values. URL components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
|
|
65
91
|
|
|
66
92
|
##### arrayFormat
|
|
67
93
|
|
|
68
|
-
Type: `string
|
|
94
|
+
Type: `string`\
|
|
69
95
|
Default: `'none'`
|
|
70
96
|
|
|
71
97
|
- `'bracket'`: Parse arrays with bracket representation:
|
|
72
98
|
|
|
73
99
|
```js
|
|
100
|
+
const queryString = require('query-string');
|
|
101
|
+
|
|
74
102
|
queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
|
|
75
103
|
//=> {foo: ['1', '2', '3']}
|
|
76
104
|
```
|
|
@@ -78,6 +106,8 @@ queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
|
|
|
78
106
|
- `'index'`: Parse arrays with index representation:
|
|
79
107
|
|
|
80
108
|
```js
|
|
109
|
+
const queryString = require('query-string');
|
|
110
|
+
|
|
81
111
|
queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
|
|
82
112
|
//=> {foo: ['1', '2', '3']}
|
|
83
113
|
```
|
|
@@ -85,6 +115,8 @@ queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
|
|
|
85
115
|
- `'comma'`: Parse arrays with elements separated by comma:
|
|
86
116
|
|
|
87
117
|
```js
|
|
118
|
+
const queryString = require('query-string');
|
|
119
|
+
|
|
88
120
|
queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
|
|
89
121
|
//=> {foo: ['1', '2', '3']}
|
|
90
122
|
```
|
|
@@ -92,23 +124,27 @@ queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
|
|
|
92
124
|
- `'none'`: Parse arrays with elements using duplicate keys:
|
|
93
125
|
|
|
94
126
|
```js
|
|
127
|
+
const queryString = require('query-string');
|
|
128
|
+
|
|
95
129
|
queryString.parse('foo=1&foo=2&foo=3');
|
|
96
130
|
//=> {foo: ['1', '2', '3']}
|
|
97
131
|
```
|
|
98
132
|
|
|
99
133
|
##### sort
|
|
100
134
|
|
|
101
|
-
Type: `Function | boolean
|
|
135
|
+
Type: `Function | boolean`\
|
|
102
136
|
Default: `true`
|
|
103
137
|
|
|
104
138
|
Supports both `Function` as a custom sorting function or `false` to disable sorting.
|
|
105
139
|
|
|
106
140
|
##### parseNumbers
|
|
107
141
|
|
|
108
|
-
Type: `boolean
|
|
142
|
+
Type: `boolean`\
|
|
109
143
|
Default: `false`
|
|
110
144
|
|
|
111
145
|
```js
|
|
146
|
+
const queryString = require('query-string');
|
|
147
|
+
|
|
112
148
|
queryString.parse('foo=1', {parseNumbers: true});
|
|
113
149
|
//=> {foo: 1}
|
|
114
150
|
```
|
|
@@ -117,17 +153,19 @@ Parse the value as a number type instead of string type if it's a number.
|
|
|
117
153
|
|
|
118
154
|
##### parseBooleans
|
|
119
155
|
|
|
120
|
-
Type: `boolean
|
|
156
|
+
Type: `boolean`\
|
|
121
157
|
Default: `false`
|
|
122
158
|
|
|
123
159
|
```js
|
|
160
|
+
const queryString = require('query-string');
|
|
161
|
+
|
|
124
162
|
queryString.parse('foo=true', {parseBooleans: true});
|
|
125
163
|
//=> {foo: true}
|
|
126
164
|
```
|
|
127
165
|
|
|
128
166
|
Parse the value as a boolean type instead of string type if it's a boolean.
|
|
129
167
|
|
|
130
|
-
### .stringify(object,
|
|
168
|
+
### .stringify(object, options?)
|
|
131
169
|
|
|
132
170
|
Stringify an object into a query string and sorting the keys.
|
|
133
171
|
|
|
@@ -137,26 +175,28 @@ Type: `object`
|
|
|
137
175
|
|
|
138
176
|
##### strict
|
|
139
177
|
|
|
140
|
-
Type: `boolean
|
|
178
|
+
Type: `boolean`\
|
|
141
179
|
Default: `true`
|
|
142
180
|
|
|
143
181
|
Strictly encode URI components with [strict-uri-encode](https://github.com/kevva/strict-uri-encode). It uses [encodeURIComponent](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) if set to false. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
|
|
144
182
|
|
|
145
183
|
##### encode
|
|
146
184
|
|
|
147
|
-
Type: `boolean
|
|
185
|
+
Type: `boolean`\
|
|
148
186
|
Default: `true`
|
|
149
187
|
|
|
150
188
|
[URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
|
|
151
189
|
|
|
152
190
|
##### arrayFormat
|
|
153
191
|
|
|
154
|
-
Type: `string
|
|
192
|
+
Type: `string`\
|
|
155
193
|
Default: `'none'`
|
|
156
194
|
|
|
157
195
|
- `'bracket'`: Serialize arrays using bracket representation:
|
|
158
196
|
|
|
159
197
|
```js
|
|
198
|
+
const queryString = require('query-string');
|
|
199
|
+
|
|
160
200
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
|
|
161
201
|
//=> 'foo[]=1&foo[]=2&foo[]=3'
|
|
162
202
|
```
|
|
@@ -164,6 +204,8 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
|
|
|
164
204
|
- `'index'`: Serialize arrays using index representation:
|
|
165
205
|
|
|
166
206
|
```js
|
|
207
|
+
const queryString = require('query-string');
|
|
208
|
+
|
|
167
209
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
|
|
168
210
|
//=> 'foo[0]=1&foo[1]=2&foo[2]=3'
|
|
169
211
|
```
|
|
@@ -171,6 +213,8 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
|
|
|
171
213
|
- `'comma'`: Serialize arrays by separating elements with comma:
|
|
172
214
|
|
|
173
215
|
```js
|
|
216
|
+
const queryString = require('query-string');
|
|
217
|
+
|
|
174
218
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
|
|
175
219
|
//=> 'foo=1,2,3'
|
|
176
220
|
```
|
|
@@ -178,6 +222,8 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
|
|
|
178
222
|
- `'none'`: Serialize arrays by using duplicate keys:
|
|
179
223
|
|
|
180
224
|
```js
|
|
225
|
+
const queryString = require('query-string');
|
|
226
|
+
|
|
181
227
|
queryString.stringify({foo: [1, 2, 3]});
|
|
182
228
|
//=> 'foo=1&foo=2&foo=3'
|
|
183
229
|
```
|
|
@@ -189,6 +235,8 @@ Type: `Function | boolean`
|
|
|
189
235
|
Supports both `Function` as a custom sorting function or `false` to disable sorting.
|
|
190
236
|
|
|
191
237
|
```js
|
|
238
|
+
const queryString = require('query-string');
|
|
239
|
+
|
|
192
240
|
const order = ['c', 'a', 'b'];
|
|
193
241
|
|
|
194
242
|
queryString.stringify({a: 1, b: 2, c: 3}, {
|
|
@@ -198,16 +246,47 @@ queryString.stringify({a: 1, b: 2, c: 3}, {
|
|
|
198
246
|
```
|
|
199
247
|
|
|
200
248
|
```js
|
|
249
|
+
const queryString = require('query-string');
|
|
250
|
+
|
|
201
251
|
queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
|
|
202
252
|
//=> 'b=1&c=2&a=3'
|
|
203
253
|
```
|
|
204
254
|
|
|
205
255
|
If omitted, keys are sorted using `Array#sort()`, which means, converting them to strings and comparing strings in Unicode code point order.
|
|
206
256
|
|
|
257
|
+
##### skipNull
|
|
258
|
+
|
|
259
|
+
Skip keys with `null` as the value.
|
|
260
|
+
|
|
261
|
+
Note that keys with `undefined` as the value are always skipped.
|
|
262
|
+
|
|
263
|
+
Type: `boolean`\
|
|
264
|
+
Default: `false`
|
|
265
|
+
|
|
266
|
+
```js
|
|
267
|
+
const queryString = require('query-string');
|
|
268
|
+
|
|
269
|
+
queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
|
|
270
|
+
skipNull: true
|
|
271
|
+
});
|
|
272
|
+
//=> 'a=1&d=4'
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
```js
|
|
276
|
+
const queryString = require('query-string');
|
|
277
|
+
|
|
278
|
+
queryString.stringify({a: undefined, b: null}, {
|
|
279
|
+
skipNull: true
|
|
280
|
+
});
|
|
281
|
+
//=> ''
|
|
282
|
+
```
|
|
283
|
+
|
|
207
284
|
### .extract(string)
|
|
208
285
|
|
|
209
286
|
Extract a query string from a URL that can be passed into `.parse()`.
|
|
210
287
|
|
|
288
|
+
Note: This behaviour can be changed with the `skipNull` option.
|
|
289
|
+
|
|
211
290
|
### .parseUrl(string, options?)
|
|
212
291
|
|
|
213
292
|
Extract the URL and the query string as an object.
|
|
@@ -217,10 +296,45 @@ The `options` are the same as for `.parse()`.
|
|
|
217
296
|
Returns an object with a `url` and `query` property.
|
|
218
297
|
|
|
219
298
|
```js
|
|
299
|
+
const queryString = require('query-string');
|
|
300
|
+
|
|
220
301
|
queryString.parseUrl('https://foo.bar?foo=bar');
|
|
221
302
|
//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
|
|
222
303
|
```
|
|
223
304
|
|
|
305
|
+
### .stringifyUrl(object, options?)
|
|
306
|
+
|
|
307
|
+
Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
|
|
308
|
+
|
|
309
|
+
The `options` are the same as for `.stringify()`.
|
|
310
|
+
|
|
311
|
+
Returns a string with the URL and a query string.
|
|
312
|
+
|
|
313
|
+
Query items in the `query` property overrides queries in the `url` property.
|
|
314
|
+
|
|
315
|
+
```js
|
|
316
|
+
queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
|
|
317
|
+
//=> 'https://foo.bar?foo=bar'
|
|
318
|
+
|
|
319
|
+
queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
|
|
320
|
+
//=> 'https://foo.bar?foo=bar'
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
#### object
|
|
324
|
+
|
|
325
|
+
Type: `object`
|
|
326
|
+
|
|
327
|
+
##### url
|
|
328
|
+
|
|
329
|
+
Type: `string`
|
|
330
|
+
|
|
331
|
+
The URL to stringify.
|
|
332
|
+
|
|
333
|
+
##### query
|
|
334
|
+
|
|
335
|
+
Type: `object`
|
|
336
|
+
|
|
337
|
+
Query items to add to the URL.
|
|
224
338
|
|
|
225
339
|
## Nesting
|
|
226
340
|
|
|
@@ -229,6 +343,8 @@ This module intentionally doesn't support nesting as it's not spec'd and varies
|
|
|
229
343
|
You're much better off just converting the object to a JSON string:
|
|
230
344
|
|
|
231
345
|
```js
|
|
346
|
+
const queryString = require('query-string');
|
|
347
|
+
|
|
232
348
|
queryString.stringify({
|
|
233
349
|
foo: 'bar',
|
|
234
350
|
nested: JSON.stringify({
|
|
@@ -241,6 +357,8 @@ queryString.stringify({
|
|
|
241
357
|
However, there is support for multiple instances of the same key:
|
|
242
358
|
|
|
243
359
|
```js
|
|
360
|
+
const queryString = require('query-string');
|
|
361
|
+
|
|
244
362
|
queryString.parse('likes=cake&name=bob&likes=icecream');
|
|
245
363
|
//=> {likes: ['cake', 'icecream'], name: 'bob'}
|
|
246
364
|
|
|
@@ -248,12 +366,13 @@ queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
|
|
|
248
366
|
//=> 'color=taupe&color=chartreuse&id=515'
|
|
249
367
|
```
|
|
250
368
|
|
|
251
|
-
|
|
252
369
|
## Falsy values
|
|
253
370
|
|
|
254
371
|
Sometimes you want to unset a key, or maybe just make it present without assigning a value to it. Here is how falsy values are stringified:
|
|
255
372
|
|
|
256
373
|
```js
|
|
374
|
+
const queryString = require('query-string');
|
|
375
|
+
|
|
257
376
|
queryString.stringify({foo: false});
|
|
258
377
|
//=> 'foo=false'
|
|
259
378
|
|
|
@@ -264,15 +383,8 @@ queryString.stringify({foo: undefined});
|
|
|
264
383
|
//=> ''
|
|
265
384
|
```
|
|
266
385
|
|
|
386
|
+
## query-string for enterprise
|
|
267
387
|
|
|
268
|
-
|
|
388
|
+
Available as part of the Tidelift Subscription.
|
|
269
389
|
|
|
270
|
-
|
|
271
|
-
<b>
|
|
272
|
-
<a href="https://tidelift.com/subscription/pkg/npm-query-string?utm_source=npm-query-string&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
|
273
|
-
</b>
|
|
274
|
-
<br>
|
|
275
|
-
<sub>
|
|
276
|
-
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|
277
|
-
</sub>
|
|
278
|
-
</div>
|
|
390
|
+
The maintainers of query-string and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-query-string?utm_source=npm-query-string&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|