query-string 6.13.8 → 9.3.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/base.d.ts +775 -0
- package/base.js +578 -0
- package/index.d.ts +16 -411
- package/index.js +2 -379
- package/license +1 -1
- package/package.json +26 -12
- package/readme.md +366 -56
package/readme.md
CHANGED
|
@@ -1,43 +1,22 @@
|
|
|
1
|
-
# query-string
|
|
1
|
+
# query-string
|
|
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://standardresume.co">
|
|
20
|
-
<img src="https://sindresorhus.com/assets/thanks/standard-resume-logo.svg" width="200"/>
|
|
21
|
-
</a>
|
|
22
|
-
</p>
|
|
23
|
-
</div>
|
|
24
|
-
|
|
25
|
-
---
|
|
26
|
-
|
|
27
|
-
<br>
|
|
28
|
-
|
|
29
5
|
## Install
|
|
30
6
|
|
|
7
|
+
```sh
|
|
8
|
+
npm install query-string
|
|
31
9
|
```
|
|
32
|
-
$ npm install query-string
|
|
33
|
-
```
|
|
34
10
|
|
|
35
|
-
|
|
11
|
+
> [!WARNING]
|
|
12
|
+
> Remember the hyphen! Do not install the deprecated [`querystring`](https://github.com/Gozala/querystring) package!
|
|
13
|
+
|
|
14
|
+
For browser usage, this package targets the latest version of Chrome, Firefox, and Safari.
|
|
36
15
|
|
|
37
16
|
## Usage
|
|
38
17
|
|
|
39
18
|
```js
|
|
40
|
-
|
|
19
|
+
import queryString from 'query-string';
|
|
41
20
|
|
|
42
21
|
console.log(location.search);
|
|
43
22
|
//=> '?foo=bar'
|
|
@@ -73,6 +52,14 @@ Parse a query string into an object. Leading `?` or `#` are ignored, so you can
|
|
|
73
52
|
|
|
74
53
|
The returned object is created with [`Object.create(null)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) and thus does not have a `prototype`.
|
|
75
54
|
|
|
55
|
+
```js
|
|
56
|
+
queryString.parse('?foo=bar');
|
|
57
|
+
//=> {foo: 'bar'}
|
|
58
|
+
|
|
59
|
+
queryString.parse('#token=secret&name=jhon');
|
|
60
|
+
//=> {token: 'secret', name: 'jhon'}
|
|
61
|
+
```
|
|
62
|
+
|
|
76
63
|
#### options
|
|
77
64
|
|
|
78
65
|
Type: `object`
|
|
@@ -92,7 +79,7 @@ Default: `'none'`
|
|
|
92
79
|
- `'bracket'`: Parse arrays with bracket representation:
|
|
93
80
|
|
|
94
81
|
```js
|
|
95
|
-
|
|
82
|
+
import queryString from 'query-string';
|
|
96
83
|
|
|
97
84
|
queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
|
|
98
85
|
//=> {foo: ['1', '2', '3']}
|
|
@@ -101,7 +88,7 @@ queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
|
|
|
101
88
|
- `'index'`: Parse arrays with index representation:
|
|
102
89
|
|
|
103
90
|
```js
|
|
104
|
-
|
|
91
|
+
import queryString from 'query-string';
|
|
105
92
|
|
|
106
93
|
queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
|
|
107
94
|
//=> {foo: ['1', '2', '3']}
|
|
@@ -110,7 +97,7 @@ queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
|
|
|
110
97
|
- `'comma'`: Parse arrays with elements separated by comma:
|
|
111
98
|
|
|
112
99
|
```js
|
|
113
|
-
|
|
100
|
+
import queryString from 'query-string';
|
|
114
101
|
|
|
115
102
|
queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
|
|
116
103
|
//=> {foo: ['1', '2', '3']}
|
|
@@ -119,16 +106,49 @@ queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
|
|
|
119
106
|
- `'separator'`: Parse arrays with elements separated by a custom character:
|
|
120
107
|
|
|
121
108
|
```js
|
|
122
|
-
|
|
109
|
+
import queryString from 'query-string';
|
|
123
110
|
|
|
124
111
|
queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
|
|
125
112
|
//=> {foo: ['1', '2', '3']}
|
|
126
113
|
```
|
|
127
114
|
|
|
115
|
+
- `'bracket-separator'`: Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
import queryString from 'query-string';
|
|
119
|
+
|
|
120
|
+
queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
121
|
+
//=> {foo: []}
|
|
122
|
+
|
|
123
|
+
queryString.parse('foo[]=', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
124
|
+
//=> {foo: ['']}
|
|
125
|
+
|
|
126
|
+
queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
127
|
+
//=> {foo: ['1']}
|
|
128
|
+
|
|
129
|
+
queryString.parse('foo[]=1|2|3', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
130
|
+
//=> {foo: ['1', '2', '3']}
|
|
131
|
+
|
|
132
|
+
queryString.parse('foo[]=1||3|||6', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
133
|
+
//=> {foo: ['1', '', 3, '', '', '6']}
|
|
134
|
+
|
|
135
|
+
queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
136
|
+
//=> {foo: ['1', '2', '3'], bar: 'fluffy', baz:['4']}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
- `'colon-list-separator'`: Parse arrays with parameter names that are explicitly marked with `:list`:
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
import queryString from 'query-string';
|
|
143
|
+
|
|
144
|
+
queryString.parse('foo:list=one&foo:list=two', {arrayFormat: 'colon-list-separator'});
|
|
145
|
+
//=> {foo: ['one', 'two']}
|
|
146
|
+
```
|
|
147
|
+
|
|
128
148
|
- `'none'`: Parse arrays with elements using duplicate keys:
|
|
129
149
|
|
|
130
150
|
```js
|
|
131
|
-
|
|
151
|
+
import queryString from 'query-string';
|
|
132
152
|
|
|
133
153
|
queryString.parse('foo=1&foo=2&foo=3');
|
|
134
154
|
//=> {foo: ['1', '2', '3']}
|
|
@@ -154,7 +174,7 @@ Type: `boolean`\
|
|
|
154
174
|
Default: `false`
|
|
155
175
|
|
|
156
176
|
```js
|
|
157
|
-
|
|
177
|
+
import queryString from 'query-string';
|
|
158
178
|
|
|
159
179
|
queryString.parse('foo=1', {parseNumbers: true});
|
|
160
180
|
//=> {foo: 1}
|
|
@@ -168,7 +188,7 @@ Type: `boolean`\
|
|
|
168
188
|
Default: `false`
|
|
169
189
|
|
|
170
190
|
```js
|
|
171
|
-
|
|
191
|
+
import queryString from 'query-string';
|
|
172
192
|
|
|
173
193
|
queryString.parse('foo=true', {parseBooleans: true});
|
|
174
194
|
//=> {foo: true}
|
|
@@ -176,10 +196,145 @@ queryString.parse('foo=true', {parseBooleans: true});
|
|
|
176
196
|
|
|
177
197
|
Parse the value as a boolean type instead of string type if it's a boolean.
|
|
178
198
|
|
|
199
|
+
##### types
|
|
200
|
+
|
|
201
|
+
Type: `object`\
|
|
202
|
+
Default: `{}`
|
|
203
|
+
|
|
204
|
+
Specifies a schema for parsing query values with explicit type declarations. When defined, the types provided here take precedence over general parsing options such as `parseNumbers`, `parseBooleans`, and `arrayFormat`.
|
|
205
|
+
|
|
206
|
+
Use this option to explicitly define the type of a specific parameter—particularly useful in cases where the type might otherwise be ambiguous (e.g., phone numbers or IDs).
|
|
207
|
+
|
|
208
|
+
You can also provide a custom function to transform the value. The function will receive the raw string and should return the desired parsed result. When used with array formats (like `comma`, `separator`, `bracket`, etc.), the function is applied to each array element individually.
|
|
209
|
+
|
|
210
|
+
Supported Types:
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
- `'boolean'`: Parse `flagged` as a boolean (overriding the `parseBooleans` option):
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
queryString.parse('?isAdmin=true&flagged=true&isOkay=0', {
|
|
217
|
+
parseBooleans: false,
|
|
218
|
+
types: {
|
|
219
|
+
flagged: 'boolean',
|
|
220
|
+
isOkay: 'boolean',
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
//=> {isAdmin: 'true', flagged: true, isOkay: false}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Note: The `'boolean'` type also converts `'0'` and `'1'` to booleans, and treats valueless keys (e.g. `?flag`) as `true`.
|
|
227
|
+
|
|
228
|
+
- `'string'`: Parse `phoneNumber` as a string (overriding the `parseNumbers` option):
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
import queryString from 'query-string';
|
|
232
|
+
|
|
233
|
+
queryString.parse('?phoneNumber=%2B380951234567&id=1', {
|
|
234
|
+
parseNumbers: true,
|
|
235
|
+
types: {
|
|
236
|
+
phoneNumber: 'string',
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
//=> {phoneNumber: '+380951234567', id: 1}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
- `'number'`: Parse `age` as a number (even when `parseNumbers` is false):
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
import queryString from 'query-string';
|
|
246
|
+
|
|
247
|
+
queryString.parse('?age=20&id=01234&zipcode=90210', {
|
|
248
|
+
types: {
|
|
249
|
+
age: 'number',
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
//=> {age: 20, id: '01234', zipcode: '90210'}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
- `'string[]'`: Parse `items` as an array of strings (overriding the `parseNumbers` option):
|
|
256
|
+
|
|
257
|
+
```js
|
|
258
|
+
import queryString from 'query-string';
|
|
259
|
+
|
|
260
|
+
queryString.parse('?age=20&items=1%2C2%2C3', {
|
|
261
|
+
parseNumbers: true,
|
|
262
|
+
types: {
|
|
263
|
+
items: 'string[]',
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
//=> {age: 20, items: ['1', '2', '3']}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
- `'number[]'`: Parse `items` as an array of numbers (even when `parseNumbers` is false):
|
|
270
|
+
|
|
271
|
+
```js
|
|
272
|
+
import queryString from 'query-string';
|
|
273
|
+
|
|
274
|
+
queryString.parse('?age=20&items=1%2C2%2C3', {
|
|
275
|
+
types: {
|
|
276
|
+
items: 'number[]',
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
//=> {age: '20', items: [1, 2, 3]}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
- `'Function'`: Provide a custom function as the parameter type. The parameter's value will equal the function's return value. When used with array formats (like `comma`, `separator`, `bracket`, etc.), the function is applied to each array element individually.
|
|
283
|
+
|
|
284
|
+
```js
|
|
285
|
+
import queryString from 'query-string';
|
|
286
|
+
|
|
287
|
+
queryString.parse('?age=20&id=01234&zipcode=90210', {
|
|
288
|
+
types: {
|
|
289
|
+
age: value => value * 2,
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
//=> {age: 40, id: '01234', zipcode: '90210'}
|
|
293
|
+
|
|
294
|
+
// With arrays, the function is applied to each element
|
|
295
|
+
queryString.parse('?scores=10,20,30', {
|
|
296
|
+
arrayFormat: 'comma',
|
|
297
|
+
types: {
|
|
298
|
+
scores: value => Number(value) * 2,
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
//=> {scores: [20, 40, 60]}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
NOTE: Array types (`string[]`, `number[]`) are ignored if `arrayFormat` is set to `'none'`.
|
|
305
|
+
|
|
306
|
+
```js
|
|
307
|
+
queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', {
|
|
308
|
+
arrayFormat: 'none',
|
|
309
|
+
types: {
|
|
310
|
+
ids: 'number[]',
|
|
311
|
+
foods: 'string[]',
|
|
312
|
+
},
|
|
313
|
+
}
|
|
314
|
+
//=> {ids:'001,002,003', foods:'apple,orange,mango'}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
###### Function
|
|
318
|
+
|
|
319
|
+
```js
|
|
320
|
+
import queryString from 'query-string';
|
|
321
|
+
|
|
322
|
+
queryString.parse('?age=20&id=01234&zipcode=90210', {
|
|
323
|
+
types: {
|
|
324
|
+
age: value => value * 2,
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
//=> {age: 40, id: '01234', zipcode: '90210'}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
Parse the value as a boolean type instead of string type if it's a boolean.
|
|
331
|
+
|
|
179
332
|
### .stringify(object, options?)
|
|
180
333
|
|
|
181
334
|
Stringify an object into a query string and sorting the keys.
|
|
182
335
|
|
|
336
|
+
**Supported value types:** `string`, `number`, `bigint`, `boolean`, `null`, `undefined`, and arrays of these types. Other types like `Symbol`, functions, or objects (except arrays) will throw an error.
|
|
337
|
+
|
|
183
338
|
#### options
|
|
184
339
|
|
|
185
340
|
Type: `object`
|
|
@@ -189,7 +344,7 @@ Type: `object`
|
|
|
189
344
|
Type: `boolean`\
|
|
190
345
|
Default: `true`
|
|
191
346
|
|
|
192
|
-
Strictly encode URI components
|
|
347
|
+
Strictly encode URI components. 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.
|
|
193
348
|
|
|
194
349
|
##### encode
|
|
195
350
|
|
|
@@ -206,7 +361,7 @@ Default: `'none'`
|
|
|
206
361
|
- `'bracket'`: Serialize arrays using bracket representation:
|
|
207
362
|
|
|
208
363
|
```js
|
|
209
|
-
|
|
364
|
+
import queryString from 'query-string';
|
|
210
365
|
|
|
211
366
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
|
|
212
367
|
//=> 'foo[]=1&foo[]=2&foo[]=3'
|
|
@@ -215,7 +370,7 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
|
|
|
215
370
|
- `'index'`: Serialize arrays using index representation:
|
|
216
371
|
|
|
217
372
|
```js
|
|
218
|
-
|
|
373
|
+
import queryString from 'query-string';
|
|
219
374
|
|
|
220
375
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
|
|
221
376
|
//=> 'foo[0]=1&foo[1]=2&foo[2]=3'
|
|
@@ -224,16 +379,66 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
|
|
|
224
379
|
- `'comma'`: Serialize arrays by separating elements with comma:
|
|
225
380
|
|
|
226
381
|
```js
|
|
227
|
-
|
|
382
|
+
import queryString from 'query-string';
|
|
228
383
|
|
|
229
384
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
|
|
230
385
|
//=> 'foo=1,2,3'
|
|
386
|
+
|
|
387
|
+
queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
|
|
388
|
+
//=> 'foo=1,,'
|
|
389
|
+
// Note that typing information for null values is lost
|
|
390
|
+
// and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
- `'separator'`: Serialize arrays by separating elements with a custom character:
|
|
394
|
+
|
|
395
|
+
```js
|
|
396
|
+
import queryString from 'query-string';
|
|
397
|
+
|
|
398
|
+
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'});
|
|
399
|
+
//=> 'foo=1|2|3'
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
- `'bracket-separator'`: Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:
|
|
403
|
+
|
|
404
|
+
```js
|
|
405
|
+
import queryString from 'query-string';
|
|
406
|
+
|
|
407
|
+
queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
408
|
+
//=> 'foo[]'
|
|
409
|
+
|
|
410
|
+
queryString.stringify({foo: ['']}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
411
|
+
//=> 'foo[]='
|
|
412
|
+
|
|
413
|
+
queryString.stringify({foo: [1]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
414
|
+
//=> 'foo[]=1'
|
|
415
|
+
|
|
416
|
+
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
417
|
+
//=> 'foo[]=1|2|3'
|
|
418
|
+
|
|
419
|
+
queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
420
|
+
//=> 'foo[]=1||3|||6'
|
|
421
|
+
|
|
422
|
+
queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true});
|
|
423
|
+
//=> 'foo[]=1||3|6'
|
|
424
|
+
|
|
425
|
+
queryString.stringify({foo: [1, 2, 3], bar: 'fluffy', baz: [4]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
426
|
+
//=> 'foo[]=1|2|3&bar=fluffy&baz[]=4'
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
- `'colon-list-separator'`: Serialize arrays with parameter names that are explicitly marked with `:list`:
|
|
430
|
+
|
|
431
|
+
```js
|
|
432
|
+
import queryString from 'query-string';
|
|
433
|
+
|
|
434
|
+
queryString.stringify({foo: ['one', 'two']}, {arrayFormat: 'colon-list-separator'});
|
|
435
|
+
//=> 'foo:list=one&foo:list=two'
|
|
231
436
|
```
|
|
232
437
|
|
|
233
438
|
- `'none'`: Serialize arrays by using duplicate keys:
|
|
234
439
|
|
|
235
440
|
```js
|
|
236
|
-
|
|
441
|
+
import queryString from 'query-string';
|
|
237
442
|
|
|
238
443
|
queryString.stringify({foo: [1, 2, 3]});
|
|
239
444
|
//=> 'foo=1&foo=2&foo=3'
|
|
@@ -253,7 +458,7 @@ Type: `Function | boolean`
|
|
|
253
458
|
Supports both `Function` as a custom sorting function or `false` to disable sorting.
|
|
254
459
|
|
|
255
460
|
```js
|
|
256
|
-
|
|
461
|
+
import queryString from 'query-string';
|
|
257
462
|
|
|
258
463
|
const order = ['c', 'a', 'b'];
|
|
259
464
|
|
|
@@ -264,7 +469,7 @@ queryString.stringify({a: 1, b: 2, c: 3}, {
|
|
|
264
469
|
```
|
|
265
470
|
|
|
266
471
|
```js
|
|
267
|
-
|
|
472
|
+
import queryString from 'query-string';
|
|
268
473
|
|
|
269
474
|
queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
|
|
270
475
|
//=> 'b=1&c=2&a=3'
|
|
@@ -282,7 +487,7 @@ Type: `boolean`\
|
|
|
282
487
|
Default: `false`
|
|
283
488
|
|
|
284
489
|
```js
|
|
285
|
-
|
|
490
|
+
import queryString from 'query-string';
|
|
286
491
|
|
|
287
492
|
queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
|
|
288
493
|
skipNull: true
|
|
@@ -291,7 +496,7 @@ queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
|
|
|
291
496
|
```
|
|
292
497
|
|
|
293
498
|
```js
|
|
294
|
-
|
|
499
|
+
import queryString from 'query-string';
|
|
295
500
|
|
|
296
501
|
queryString.stringify({a: undefined, b: null}, {
|
|
297
502
|
skipNull: true
|
|
@@ -307,7 +512,7 @@ Type: `boolean`\
|
|
|
307
512
|
Default: `false`
|
|
308
513
|
|
|
309
514
|
```js
|
|
310
|
-
|
|
515
|
+
import queryString from 'query-string';
|
|
311
516
|
|
|
312
517
|
queryString.stringify({a: 1, b: '', c: '', d: 4}, {
|
|
313
518
|
skipEmptyString: true
|
|
@@ -316,7 +521,7 @@ queryString.stringify({a: 1, b: '', c: '', d: 4}, {
|
|
|
316
521
|
```
|
|
317
522
|
|
|
318
523
|
```js
|
|
319
|
-
|
|
524
|
+
import queryString from 'query-string';
|
|
320
525
|
|
|
321
526
|
queryString.stringify({a: '', b: ''}, {
|
|
322
527
|
skipEmptyString: true
|
|
@@ -324,11 +529,58 @@ queryString.stringify({a: '', b: ''}, {
|
|
|
324
529
|
//=> ''
|
|
325
530
|
```
|
|
326
531
|
|
|
532
|
+
##### replacer
|
|
533
|
+
|
|
534
|
+
A function that transforms key-value pairs before stringification.
|
|
535
|
+
|
|
536
|
+
Type: `function`\
|
|
537
|
+
Default: `undefined`
|
|
538
|
+
|
|
539
|
+
Similar to the `replacer` parameter of `JSON.stringify()`, this function is called for each key-value pair and can be used to transform values before they are stringified. The function receives the key and value, and should return the transformed value. Returning `undefined` will omit the key-value pair from the resulting query string.
|
|
540
|
+
|
|
541
|
+
This is useful for custom serialization of non-primitive types like `Date`:
|
|
542
|
+
|
|
543
|
+
```js
|
|
544
|
+
import queryString from 'query-string';
|
|
545
|
+
|
|
546
|
+
queryString.stringify({
|
|
547
|
+
date: new Date('2024-01-15T10:30:00Z'),
|
|
548
|
+
name: 'John'
|
|
549
|
+
}, {
|
|
550
|
+
replacer: (key, value) => {
|
|
551
|
+
if (value instanceof Date) {
|
|
552
|
+
return value.toISOString();
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
return value;
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
//=> 'date=2024-01-15T10%3A30%3A00.000Z&name=John'
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
You can also use it to filter out keys:
|
|
562
|
+
|
|
563
|
+
```js
|
|
564
|
+
import queryString from 'query-string';
|
|
565
|
+
|
|
566
|
+
queryString.stringify({
|
|
567
|
+
a: 1,
|
|
568
|
+
b: null,
|
|
569
|
+
c: 3
|
|
570
|
+
}, {
|
|
571
|
+
replacer: (key, value) => value === null ? undefined : value
|
|
572
|
+
});
|
|
573
|
+
//=> 'a=1&c=3'
|
|
574
|
+
```
|
|
575
|
+
|
|
327
576
|
### .extract(string)
|
|
328
577
|
|
|
329
578
|
Extract a query string from a URL that can be passed into `.parse()`.
|
|
330
579
|
|
|
331
|
-
|
|
580
|
+
```js
|
|
581
|
+
queryString.extract('https://foo.bar?foo=bar');
|
|
582
|
+
//=> 'foo=bar'
|
|
583
|
+
```
|
|
332
584
|
|
|
333
585
|
### .parseUrl(string, options?)
|
|
334
586
|
|
|
@@ -339,7 +591,7 @@ Returns an object with a `url` and `query` property.
|
|
|
339
591
|
If the `parseFragmentIdentifier` option is `true`, the object will also contain a `fragmentIdentifier` property.
|
|
340
592
|
|
|
341
593
|
```js
|
|
342
|
-
|
|
594
|
+
import queryString from 'query-string';
|
|
343
595
|
|
|
344
596
|
queryString.parseUrl('https://foo.bar?foo=bar');
|
|
345
597
|
//=> {url: 'https://foo.bar', query: {foo: 'bar'}}
|
|
@@ -364,7 +616,7 @@ Type: `boolean`\
|
|
|
364
616
|
Default: `false`
|
|
365
617
|
|
|
366
618
|
```js
|
|
367
|
-
|
|
619
|
+
import queryString from 'query-string';
|
|
368
620
|
|
|
369
621
|
queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
|
|
370
622
|
//=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
|
|
@@ -415,6 +667,64 @@ Type: `object`
|
|
|
415
667
|
|
|
416
668
|
Query items to add to the URL.
|
|
417
669
|
|
|
670
|
+
### .pick(url, keys, options?)
|
|
671
|
+
### .pick(url, filter, options?)
|
|
672
|
+
|
|
673
|
+
Pick query parameters from a URL.
|
|
674
|
+
|
|
675
|
+
Returns a string with the new URL.
|
|
676
|
+
|
|
677
|
+
```js
|
|
678
|
+
import queryString from 'query-string';
|
|
679
|
+
|
|
680
|
+
queryString.pick('https://foo.bar?foo=1&bar=2#hello', ['foo']);
|
|
681
|
+
//=> 'https://foo.bar?foo=1#hello'
|
|
682
|
+
|
|
683
|
+
queryString.pick('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
|
|
684
|
+
//=> 'https://foo.bar?bar=2#hello'
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
### .exclude(url, keys, options?)
|
|
688
|
+
### .exclude(url, filter, options?)
|
|
689
|
+
|
|
690
|
+
Exclude query parameters from a URL.
|
|
691
|
+
|
|
692
|
+
Returns a string with the new URL.
|
|
693
|
+
|
|
694
|
+
```js
|
|
695
|
+
import queryString from 'query-string';
|
|
696
|
+
|
|
697
|
+
queryString.exclude('https://foo.bar?foo=1&bar=2#hello', ['foo']);
|
|
698
|
+
//=> 'https://foo.bar?bar=2#hello'
|
|
699
|
+
|
|
700
|
+
queryString.exclude('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
|
|
701
|
+
//=> 'https://foo.bar?foo=1#hello'
|
|
702
|
+
```
|
|
703
|
+
|
|
704
|
+
#### url
|
|
705
|
+
|
|
706
|
+
Type: `string`
|
|
707
|
+
|
|
708
|
+
The URL containing the query parameters to filter.
|
|
709
|
+
|
|
710
|
+
#### keys
|
|
711
|
+
|
|
712
|
+
Type: `string[]`
|
|
713
|
+
|
|
714
|
+
The names of the query parameters to filter based on the function used.
|
|
715
|
+
|
|
716
|
+
#### filter
|
|
717
|
+
|
|
718
|
+
Type: `(key, value) => boolean`
|
|
719
|
+
|
|
720
|
+
A filter predicate that will be provided the name of each query parameter and its value. The `parseNumbers` and `parseBooleans` options also affect `value`.
|
|
721
|
+
|
|
722
|
+
#### options
|
|
723
|
+
|
|
724
|
+
Type: `object`
|
|
725
|
+
|
|
726
|
+
[Parse options](#options) and [stringify options](#options-1).
|
|
727
|
+
|
|
418
728
|
## Nesting
|
|
419
729
|
|
|
420
730
|
This module intentionally doesn't support nesting as it's not spec'd and varies between implementations, which causes a lot of [edge cases](https://github.com/visionmedia/node-querystring/issues).
|
|
@@ -422,7 +732,7 @@ This module intentionally doesn't support nesting as it's not spec'd and varies
|
|
|
422
732
|
You're much better off just converting the object to a JSON string:
|
|
423
733
|
|
|
424
734
|
```js
|
|
425
|
-
|
|
735
|
+
import queryString from 'query-string';
|
|
426
736
|
|
|
427
737
|
queryString.stringify({
|
|
428
738
|
foo: 'bar',
|
|
@@ -436,7 +746,7 @@ queryString.stringify({
|
|
|
436
746
|
However, there is support for multiple instances of the same key:
|
|
437
747
|
|
|
438
748
|
```js
|
|
439
|
-
|
|
749
|
+
import queryString from 'query-string';
|
|
440
750
|
|
|
441
751
|
queryString.parse('likes=cake&name=bob&likes=icecream');
|
|
442
752
|
//=> {likes: ['cake', 'icecream'], name: 'bob'}
|
|
@@ -450,7 +760,7 @@ queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
|
|
|
450
760
|
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:
|
|
451
761
|
|
|
452
762
|
```js
|
|
453
|
-
|
|
763
|
+
import queryString from 'query-string';
|
|
454
764
|
|
|
455
765
|
queryString.stringify({foo: false});
|
|
456
766
|
//=> 'foo=false'
|
|
@@ -462,8 +772,8 @@ queryString.stringify({foo: undefined});
|
|
|
462
772
|
//=> ''
|
|
463
773
|
```
|
|
464
774
|
|
|
465
|
-
##
|
|
775
|
+
## FAQ
|
|
466
776
|
|
|
467
|
-
|
|
777
|
+
### Why is it parsing `+` as a space?
|
|
468
778
|
|
|
469
|
-
|
|
779
|
+
See [this answer](https://github.com/sindresorhus/query-string/issues/305).
|