query-string 6.14.1 → 7.0.0
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 +59 -3
- package/index.js +42 -3
- package/package.json +1 -1
- package/readme.md +79 -1
package/index.d.ts
CHANGED
|
@@ -45,6 +45,30 @@ export interface ParseOptions {
|
|
|
45
45
|
//=> {foo: ['1', '2', '3']}
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
+
- `bracket-separator`: Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
import queryString = require('query-string');
|
|
52
|
+
|
|
53
|
+
queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
54
|
+
//=> {foo: []}
|
|
55
|
+
|
|
56
|
+
queryString.parse('foo[]=', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
57
|
+
//=> {foo: ['']}
|
|
58
|
+
|
|
59
|
+
queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
60
|
+
//=> {foo: ['1']}
|
|
61
|
+
|
|
62
|
+
queryString.parse('foo[]=1|2|3', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
63
|
+
//=> {foo: ['1', '2', '3']}
|
|
64
|
+
|
|
65
|
+
queryString.parse('foo[]=1||3|||6', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
66
|
+
//=> {foo: ['1', '', 3, '', '', '6']}
|
|
67
|
+
|
|
68
|
+
queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
69
|
+
//=> {foo: ['1', '2', '3'], bar: 'fluffy', baz:['4']}
|
|
70
|
+
```
|
|
71
|
+
|
|
48
72
|
- `none`: Parse arrays with elements using duplicate keys:
|
|
49
73
|
|
|
50
74
|
```
|
|
@@ -54,7 +78,7 @@ export interface ParseOptions {
|
|
|
54
78
|
//=> {foo: ['1', '2', '3']}
|
|
55
79
|
```
|
|
56
80
|
*/
|
|
57
|
-
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'none';
|
|
81
|
+
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'bracket-separator' | 'none';
|
|
58
82
|
|
|
59
83
|
/**
|
|
60
84
|
The character used to separate array elements when using `{arrayFormat: 'separator'}`.
|
|
@@ -229,9 +253,14 @@ export interface StringifyOptions {
|
|
|
229
253
|
|
|
230
254
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
|
|
231
255
|
//=> 'foo=1,2,3'
|
|
256
|
+
|
|
257
|
+
queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
|
|
258
|
+
//=> 'foo=1,,'
|
|
259
|
+
// Note that typing information for null values is lost
|
|
260
|
+
// and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
|
|
232
261
|
```
|
|
233
262
|
|
|
234
|
-
|
|
263
|
+
- `separator`: Serialize arrays by separating elements with character:
|
|
235
264
|
|
|
236
265
|
```
|
|
237
266
|
import queryString = require('query-string');
|
|
@@ -240,6 +269,33 @@ export interface StringifyOptions {
|
|
|
240
269
|
//=> 'foo=1|2|3'
|
|
241
270
|
```
|
|
242
271
|
|
|
272
|
+
- `bracket-separator`: Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:
|
|
273
|
+
|
|
274
|
+
```
|
|
275
|
+
import queryString = require('query-string');
|
|
276
|
+
|
|
277
|
+
queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
278
|
+
//=> 'foo[]'
|
|
279
|
+
|
|
280
|
+
queryString.stringify({foo: ['']}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
281
|
+
//=> 'foo[]='
|
|
282
|
+
|
|
283
|
+
queryString.stringify({foo: [1]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
284
|
+
//=> 'foo[]=1'
|
|
285
|
+
|
|
286
|
+
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
287
|
+
//=> 'foo[]=1|2|3'
|
|
288
|
+
|
|
289
|
+
queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
290
|
+
//=> 'foo[]=1||3|||6'
|
|
291
|
+
|
|
292
|
+
queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true});
|
|
293
|
+
//=> 'foo[]=1||3|6'
|
|
294
|
+
|
|
295
|
+
queryString.stringify({foo: [1, 2, 3], bar: 'fluffy', baz: [4]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
296
|
+
//=> 'foo[]=1|2|3&bar=fluffy&baz[]=4'
|
|
297
|
+
```
|
|
298
|
+
|
|
243
299
|
- `none`: Serialize arrays by using duplicate keys:
|
|
244
300
|
|
|
245
301
|
```
|
|
@@ -249,7 +305,7 @@ export interface StringifyOptions {
|
|
|
249
305
|
//=> 'foo=1&foo=2&foo=3'
|
|
250
306
|
```
|
|
251
307
|
*/
|
|
252
|
-
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'none';
|
|
308
|
+
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'bracket-separator' | 'none';
|
|
253
309
|
|
|
254
310
|
/**
|
|
255
311
|
The character used to separate array elements when using `{arrayFormat: 'separator'}`.
|
package/index.js
CHANGED
|
@@ -49,17 +49,30 @@ function encoderForArrayFormat(options) {
|
|
|
49
49
|
|
|
50
50
|
case 'comma':
|
|
51
51
|
case 'separator':
|
|
52
|
+
case 'bracket-separator': {
|
|
53
|
+
const keyValueSep = options.arrayFormat === 'bracket-separator' ?
|
|
54
|
+
'[]=' :
|
|
55
|
+
'=';
|
|
56
|
+
|
|
52
57
|
return key => (result, value) => {
|
|
53
|
-
if (
|
|
58
|
+
if (
|
|
59
|
+
value === undefined ||
|
|
60
|
+
(options.skipNull && value === null) ||
|
|
61
|
+
(options.skipEmptyString && value === '')
|
|
62
|
+
) {
|
|
54
63
|
return result;
|
|
55
64
|
}
|
|
56
65
|
|
|
66
|
+
// Translate null to an empty string so that it doesn't serialize as 'null'
|
|
67
|
+
value = value === null ? '' : value;
|
|
68
|
+
|
|
57
69
|
if (result.length === 0) {
|
|
58
|
-
return [[encode(key, options),
|
|
70
|
+
return [[encode(key, options), keyValueSep, encode(value, options)].join('')];
|
|
59
71
|
}
|
|
60
72
|
|
|
61
73
|
return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
|
|
62
74
|
};
|
|
75
|
+
}
|
|
63
76
|
|
|
64
77
|
default:
|
|
65
78
|
return key => (result, value) => {
|
|
@@ -130,6 +143,28 @@ function parserForArrayFormat(options) {
|
|
|
130
143
|
accumulator[key] = newValue;
|
|
131
144
|
};
|
|
132
145
|
|
|
146
|
+
case 'bracket-separator':
|
|
147
|
+
return (key, value, accumulator) => {
|
|
148
|
+
const isArray = /(\[\])$/.test(key);
|
|
149
|
+
key = key.replace(/\[\]$/, '');
|
|
150
|
+
|
|
151
|
+
if (!isArray) {
|
|
152
|
+
accumulator[key] = value ? decode(value, options) : value;
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const arrayValue = value === null ?
|
|
157
|
+
[] :
|
|
158
|
+
value.split(options.arrayFormatSeparator).map(item => decode(item, options));
|
|
159
|
+
|
|
160
|
+
if (accumulator[key] === undefined) {
|
|
161
|
+
accumulator[key] = arrayValue;
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
accumulator[key] = [].concat(accumulator[key], arrayValue);
|
|
166
|
+
};
|
|
167
|
+
|
|
133
168
|
default:
|
|
134
169
|
return (key, value, accumulator) => {
|
|
135
170
|
if (accumulator[key] === undefined) {
|
|
@@ -253,7 +288,7 @@ function parse(query, options) {
|
|
|
253
288
|
|
|
254
289
|
// Missing `=` should be `null`:
|
|
255
290
|
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
|
|
256
|
-
value = value === undefined ? null : ['comma', 'separator'].includes(options.arrayFormat) ? value : decode(value, options);
|
|
291
|
+
value = value === undefined ? null : ['comma', 'separator', 'bracket-separator'].includes(options.arrayFormat) ? value : decode(value, options);
|
|
257
292
|
formatter(decode(key, options), value, ret);
|
|
258
293
|
}
|
|
259
294
|
|
|
@@ -335,6 +370,10 @@ exports.stringify = (object, options) => {
|
|
|
335
370
|
}
|
|
336
371
|
|
|
337
372
|
if (Array.isArray(value)) {
|
|
373
|
+
if (value.length === 0 && options.arrayFormat === 'bracket-separator') {
|
|
374
|
+
return encode(key, options) + '[]';
|
|
375
|
+
}
|
|
376
|
+
|
|
338
377
|
return value
|
|
339
378
|
.reduce(formatter(key), [])
|
|
340
379
|
.join('&');
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -17,7 +17,20 @@
|
|
|
17
17
|
<br>
|
|
18
18
|
<br>
|
|
19
19
|
<a href="https://standardresume.co/tech">
|
|
20
|
-
<img src="https://sindresorhus.com/assets/thanks/standard-resume-logo.svg" width="
|
|
20
|
+
<img src="https://sindresorhus.com/assets/thanks/standard-resume-logo.svg" width="180"/>
|
|
21
|
+
</a>
|
|
22
|
+
<br>
|
|
23
|
+
<br>
|
|
24
|
+
<a href="https://doppler.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=query-string&utm_source=github">
|
|
25
|
+
<div>
|
|
26
|
+
<img src="https://dashboard.doppler.com/imgs/logo-long.svg" width="240" alt="Doppler">
|
|
27
|
+
</div>
|
|
28
|
+
<b>All your environment variables, in one place</b>
|
|
29
|
+
<div>
|
|
30
|
+
<span>Stop struggling with scattered API keys, hacking together home-brewed tools,</span>
|
|
31
|
+
<br>
|
|
32
|
+
<span>and avoiding access controls. Keep your team and servers in sync with Doppler.</span>
|
|
33
|
+
</div>
|
|
21
34
|
</a>
|
|
22
35
|
</p>
|
|
23
36
|
</div>
|
|
@@ -125,6 +138,30 @@ queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator:
|
|
|
125
138
|
//=> {foo: ['1', '2', '3']}
|
|
126
139
|
```
|
|
127
140
|
|
|
141
|
+
- `'bracket-separator'`: Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
const queryString = require('query-string');
|
|
145
|
+
|
|
146
|
+
queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
147
|
+
//=> {foo: []}
|
|
148
|
+
|
|
149
|
+
queryString.parse('foo[]=', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
150
|
+
//=> {foo: ['']}
|
|
151
|
+
|
|
152
|
+
queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
153
|
+
//=> {foo: ['1']}
|
|
154
|
+
|
|
155
|
+
queryString.parse('foo[]=1|2|3', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
156
|
+
//=> {foo: ['1', '2', '3']}
|
|
157
|
+
|
|
158
|
+
queryString.parse('foo[]=1||3|||6', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
159
|
+
//=> {foo: ['1', '', 3, '', '', '6']}
|
|
160
|
+
|
|
161
|
+
queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
162
|
+
//=> {foo: ['1', '2', '3'], bar: 'fluffy', baz:['4']}
|
|
163
|
+
```
|
|
164
|
+
|
|
128
165
|
- `'none'`: Parse arrays with elements using duplicate keys:
|
|
129
166
|
|
|
130
167
|
```js
|
|
@@ -228,6 +265,47 @@ const queryString = require('query-string');
|
|
|
228
265
|
|
|
229
266
|
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
|
|
230
267
|
//=> 'foo=1,2,3'
|
|
268
|
+
|
|
269
|
+
queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
|
|
270
|
+
//=> 'foo=1,,'
|
|
271
|
+
// Note that typing information for null values is lost
|
|
272
|
+
// and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
- `'separator'`: Serialize arrays by separating elements with a custom character:
|
|
276
|
+
|
|
277
|
+
```js
|
|
278
|
+
const queryString = require('query-string');
|
|
279
|
+
|
|
280
|
+
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'});
|
|
281
|
+
//=> 'foo=1|2|3'
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
- `'bracket-separator'`: Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:
|
|
285
|
+
|
|
286
|
+
```js
|
|
287
|
+
const queryString = require('query-string');
|
|
288
|
+
|
|
289
|
+
queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
290
|
+
//=> 'foo[]'
|
|
291
|
+
|
|
292
|
+
queryString.stringify({foo: ['']}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
293
|
+
//=> 'foo[]='
|
|
294
|
+
|
|
295
|
+
queryString.stringify({foo: [1]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
296
|
+
//=> 'foo[]=1'
|
|
297
|
+
|
|
298
|
+
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
299
|
+
//=> 'foo[]=1|2|3'
|
|
300
|
+
|
|
301
|
+
queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
302
|
+
//=> 'foo[]=1||3|||6'
|
|
303
|
+
|
|
304
|
+
queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true});
|
|
305
|
+
//=> 'foo[]=1||3|6'
|
|
306
|
+
|
|
307
|
+
queryString.stringify({foo: [1, 2, 3], bar: 'fluffy', baz: [4]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
|
|
308
|
+
//=> 'foo[]=1|2|3&bar=fluffy&baz[]=4'
|
|
231
309
|
```
|
|
232
310
|
|
|
233
311
|
- `'none'`: Serialize arrays by using duplicate keys:
|