query-string 6.10.1 → 6.11.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 +34 -2
- package/index.js +19 -5
- package/package.json +1 -1
- package/readme.md +23 -0
package/index.d.ts
CHANGED
|
@@ -36,6 +36,15 @@ export interface ParseOptions {
|
|
|
36
36
|
//=> {foo: ['1', '2', '3']}
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
+
- `separator`: Parse arrays with elements separated by a custom character:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
import queryString = require('query-string');
|
|
43
|
+
|
|
44
|
+
queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
|
|
45
|
+
//=> {foo: ['1', '2', '3']}
|
|
46
|
+
```
|
|
47
|
+
|
|
39
48
|
- `none`: Parse arrays with elements using duplicate keys:
|
|
40
49
|
|
|
41
50
|
```
|
|
@@ -45,7 +54,14 @@ export interface ParseOptions {
|
|
|
45
54
|
//=> {foo: ['1', '2', '3']}
|
|
46
55
|
```
|
|
47
56
|
*/
|
|
48
|
-
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none';
|
|
57
|
+
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'none';
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
The character used to separate array elements when using `{arrayFormat: 'separator'}`.
|
|
61
|
+
|
|
62
|
+
@default ,
|
|
63
|
+
*/
|
|
64
|
+
readonly arrayFormatSeparator?: 'string';
|
|
49
65
|
|
|
50
66
|
/**
|
|
51
67
|
Supports both `Function` as a custom sorting function or `false` to disable sorting.
|
|
@@ -188,6 +204,15 @@ export interface StringifyOptions {
|
|
|
188
204
|
//=> 'foo=1,2,3'
|
|
189
205
|
```
|
|
190
206
|
|
|
207
|
+
- `separator`: Serialize arrays by separating elements with character:
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
import queryString = require('query-string');
|
|
211
|
+
|
|
212
|
+
queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'});
|
|
213
|
+
//=> 'foo=1|2|3'
|
|
214
|
+
```
|
|
215
|
+
|
|
191
216
|
- `none`: Serialize arrays by using duplicate keys:
|
|
192
217
|
|
|
193
218
|
```
|
|
@@ -197,7 +222,14 @@ export interface StringifyOptions {
|
|
|
197
222
|
//=> 'foo=1&foo=2&foo=3'
|
|
198
223
|
```
|
|
199
224
|
*/
|
|
200
|
-
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none';
|
|
225
|
+
readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'none';
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
The character used to separate array elements when using `{arrayFormat: 'separator'}`.
|
|
229
|
+
|
|
230
|
+
@default ,
|
|
231
|
+
*/
|
|
232
|
+
readonly arrayFormatSeparator?: 'string';
|
|
201
233
|
|
|
202
234
|
/**
|
|
203
235
|
Supports both `Function` as a custom sorting function or `false` to disable sorting.
|
package/index.js
CHANGED
|
@@ -36,6 +36,7 @@ function encoderForArrayFormat(options) {
|
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
case 'comma':
|
|
39
|
+
case 'separator':
|
|
39
40
|
return key => (result, value) => {
|
|
40
41
|
if (value === null || value === undefined || value.length === 0) {
|
|
41
42
|
return result;
|
|
@@ -45,7 +46,7 @@ function encoderForArrayFormat(options) {
|
|
|
45
46
|
return [[encode(key, options), '=', encode(value, options)].join('')];
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
return [[result, encode(value, options)].join(
|
|
49
|
+
return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
|
|
49
50
|
};
|
|
50
51
|
|
|
51
52
|
default:
|
|
@@ -104,9 +105,10 @@ function parserForArrayFormat(options) {
|
|
|
104
105
|
};
|
|
105
106
|
|
|
106
107
|
case 'comma':
|
|
108
|
+
case 'separator':
|
|
107
109
|
return (key, value, accumulator) => {
|
|
108
|
-
const isArray = typeof value === 'string' && value.split('').indexOf(
|
|
109
|
-
const newValue = isArray ? value.split(
|
|
110
|
+
const isArray = typeof value === 'string' && value.split('').indexOf(options.arrayFormatSeparator) > -1;
|
|
111
|
+
const newValue = isArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : value === null ? value : decode(value, options);
|
|
110
112
|
accumulator[key] = newValue;
|
|
111
113
|
};
|
|
112
114
|
|
|
@@ -122,6 +124,12 @@ function parserForArrayFormat(options) {
|
|
|
122
124
|
}
|
|
123
125
|
}
|
|
124
126
|
|
|
127
|
+
function validateArrayFormatSeparator(value) {
|
|
128
|
+
if (typeof value !== 'string' || value.length !== 1) {
|
|
129
|
+
throw new TypeError('arrayFormatSeparator must be single character string');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
125
133
|
function encode(value, options) {
|
|
126
134
|
if (options.encode) {
|
|
127
135
|
return options.strict ? strictUriEncode(value) : encodeURIComponent(value);
|
|
@@ -196,10 +204,13 @@ function parse(input, options) {
|
|
|
196
204
|
decode: true,
|
|
197
205
|
sort: true,
|
|
198
206
|
arrayFormat: 'none',
|
|
207
|
+
arrayFormatSeparator: ',',
|
|
199
208
|
parseNumbers: false,
|
|
200
209
|
parseBooleans: false
|
|
201
210
|
}, options);
|
|
202
211
|
|
|
212
|
+
validateArrayFormatSeparator(options.arrayFormatSeparator);
|
|
213
|
+
|
|
203
214
|
const formatter = parserForArrayFormat(options);
|
|
204
215
|
|
|
205
216
|
// Create an object with no prototype
|
|
@@ -220,7 +231,7 @@ function parse(input, options) {
|
|
|
220
231
|
|
|
221
232
|
// Missing `=` should be `null`:
|
|
222
233
|
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
|
|
223
|
-
value = value === undefined ? null : decode(value, options);
|
|
234
|
+
value = value === undefined ? null : options.arrayFormat === 'comma' ? value : decode(value, options);
|
|
224
235
|
formatter(decode(key, options), value, ret);
|
|
225
236
|
}
|
|
226
237
|
|
|
@@ -263,9 +274,12 @@ exports.stringify = (object, options) => {
|
|
|
263
274
|
options = Object.assign({
|
|
264
275
|
encode: true,
|
|
265
276
|
strict: true,
|
|
266
|
-
arrayFormat: 'none'
|
|
277
|
+
arrayFormat: 'none',
|
|
278
|
+
arrayFormatSeparator: ','
|
|
267
279
|
}, options);
|
|
268
280
|
|
|
281
|
+
validateArrayFormatSeparator(options.arrayFormatSeparator);
|
|
282
|
+
|
|
269
283
|
const formatter = encoderForArrayFormat(options);
|
|
270
284
|
|
|
271
285
|
const objectCopy = Object.assign({}, object);
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -121,6 +121,15 @@ queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
|
|
|
121
121
|
//=> {foo: ['1', '2', '3']}
|
|
122
122
|
```
|
|
123
123
|
|
|
124
|
+
- `'separator'`: Parse arrays with elements separated by a custom character:
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
const queryString = require('query-string');
|
|
128
|
+
|
|
129
|
+
queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
|
|
130
|
+
//=> {foo: ['1', '2', '3']}
|
|
131
|
+
```
|
|
132
|
+
|
|
124
133
|
- `'none'`: Parse arrays with elements using duplicate keys:
|
|
125
134
|
|
|
126
135
|
```js
|
|
@@ -130,6 +139,13 @@ queryString.parse('foo=1&foo=2&foo=3');
|
|
|
130
139
|
//=> {foo: ['1', '2', '3']}
|
|
131
140
|
```
|
|
132
141
|
|
|
142
|
+
##### arrayFormatSeparator
|
|
143
|
+
|
|
144
|
+
Type: `string`\
|
|
145
|
+
Default: `','`
|
|
146
|
+
|
|
147
|
+
The character used to separate array elements when using `{arrayFormat: 'separator'}`.
|
|
148
|
+
|
|
133
149
|
##### sort
|
|
134
150
|
|
|
135
151
|
Type: `Function | boolean`\
|
|
@@ -228,6 +244,13 @@ queryString.stringify({foo: [1, 2, 3]});
|
|
|
228
244
|
//=> 'foo=1&foo=2&foo=3'
|
|
229
245
|
```
|
|
230
246
|
|
|
247
|
+
##### arrayFormatSeparator
|
|
248
|
+
|
|
249
|
+
Type: `string`\
|
|
250
|
+
Default: `','`
|
|
251
|
+
|
|
252
|
+
The character used to separate array elements when using `{arrayFormat: 'separator'}`.
|
|
253
|
+
|
|
231
254
|
##### sort
|
|
232
255
|
|
|
233
256
|
Type: `Function | boolean`
|