@common.js/query-string 9.0.0 → 9.1.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/README.md +1 -1
- package/base.d.ts +110 -1
- package/base.js +24 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
The [query-string](https://www.npmjs.com/package/query-string) package exported as CommonJS modules.
|
|
4
4
|
|
|
5
|
-
Exported from [query-string@9.
|
|
5
|
+
Exported from [query-string@9.1.1](https://www.npmjs.com/package/query-string/v/9.1.1) using https://github.com/etienne-martin/common.js.
|
package/base.d.ts
CHANGED
|
@@ -87,7 +87,14 @@ export type ParseOptions = {
|
|
|
87
87
|
//=> {foo: ['1', '2', '3']}
|
|
88
88
|
```
|
|
89
89
|
*/
|
|
90
|
-
readonly arrayFormat?:
|
|
90
|
+
readonly arrayFormat?:
|
|
91
|
+
| 'bracket'
|
|
92
|
+
| 'index'
|
|
93
|
+
| 'comma'
|
|
94
|
+
| 'separator'
|
|
95
|
+
| 'bracket-separator'
|
|
96
|
+
| 'colon-list-separator'
|
|
97
|
+
| 'none';
|
|
91
98
|
|
|
92
99
|
/**
|
|
93
100
|
The character used to separate array elements when using `{arrayFormat: 'separator'}`.
|
|
@@ -169,6 +176,108 @@ export type ParseOptions = {
|
|
|
169
176
|
```
|
|
170
177
|
*/
|
|
171
178
|
readonly parseFragmentIdentifier?: boolean;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
Specify a pre-defined schema to be used when parsing values. The types specified will take precedence over options such as: `parseNumber`, `parseBooleans`, and `arrayFormat`.
|
|
182
|
+
|
|
183
|
+
Use this feature to override the type of a value. This can be useful when the type is ambiguous such as a phone number (see example 1 and 2).
|
|
184
|
+
|
|
185
|
+
It is possible to provide a custom function as the parameter type. The parameter's value will equal the function's return value (see example 4).
|
|
186
|
+
|
|
187
|
+
NOTE: Array types (`string[]` and `number[]`) will have no effect if `arrayFormat` is set to `none` (see example 5).
|
|
188
|
+
|
|
189
|
+
@default {}
|
|
190
|
+
|
|
191
|
+
@example
|
|
192
|
+
Parse `phoneNumber` as a string, overriding the `parseNumber` option:
|
|
193
|
+
```
|
|
194
|
+
import queryString from 'query-string';
|
|
195
|
+
|
|
196
|
+
queryString.parse('?phoneNumber=%2B380951234567&id=1', {
|
|
197
|
+
parseNumbers: true,
|
|
198
|
+
types: {
|
|
199
|
+
phoneNumber: 'string',
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
//=> {phoneNumber: '+380951234567', id: 1}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
@example
|
|
206
|
+
Parse `items` as an array of strings, overriding the `parseNumber` option:
|
|
207
|
+
```
|
|
208
|
+
import queryString from 'query-string';
|
|
209
|
+
|
|
210
|
+
queryString.parse('?age=20&items=1%2C2%2C3', {
|
|
211
|
+
parseNumber: true,
|
|
212
|
+
types: {
|
|
213
|
+
items: 'string[]',
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
//=> {age: 20, items: ['1', '2', '3']}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
@example
|
|
220
|
+
Parse `age` as a number, even when `parseNumber` is false:
|
|
221
|
+
```
|
|
222
|
+
import queryString from 'query-string';
|
|
223
|
+
|
|
224
|
+
queryString.parse('?age=20&id=01234&zipcode=90210', {
|
|
225
|
+
types: {
|
|
226
|
+
age: 'number',
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
//=> {age: 20, id: '01234', zipcode: '90210 }
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
@example
|
|
233
|
+
Parse `age` using a custom value parser:
|
|
234
|
+
```
|
|
235
|
+
import queryString from 'query-string';
|
|
236
|
+
|
|
237
|
+
queryString.parse('?age=20&id=01234&zipcode=90210', {
|
|
238
|
+
types: {
|
|
239
|
+
age: (value) => value * 2,
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
//=> {age: 40, id: '01234', zipcode: '90210 }
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
@example
|
|
246
|
+
Array types will have no effect when `arrayFormat` is set to `none`
|
|
247
|
+
```
|
|
248
|
+
queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', {
|
|
249
|
+
arrayFormat: 'none',
|
|
250
|
+
types: {
|
|
251
|
+
ids: 'number[]',
|
|
252
|
+
foods: 'string[]',
|
|
253
|
+
},
|
|
254
|
+
}
|
|
255
|
+
//=> {ids:'001,002,003', foods:'apple,orange,mango'}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
@example
|
|
259
|
+
Parse a query utilizing all types:
|
|
260
|
+
```
|
|
261
|
+
import queryString from 'query-string';
|
|
262
|
+
|
|
263
|
+
queryString.parse('?ids=001%2C002%2C003&items=1%2C2%2C3&price=22%2E00&numbers=1%2C2%2C3&double=5&number=20', {
|
|
264
|
+
arrayFormat: 'comma',
|
|
265
|
+
types: {
|
|
266
|
+
ids: 'string',
|
|
267
|
+
items: 'string[]',
|
|
268
|
+
price: 'string',
|
|
269
|
+
numbers: 'number[]',
|
|
270
|
+
double: (value) => value * 2,
|
|
271
|
+
number: 'number',
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
//=> {ids: '001,002,003', items: ['1', '2', '3'], price: '22.00', numbers: [1, 2, 3], double: 10, number: 20}
|
|
275
|
+
```
|
|
276
|
+
*/
|
|
277
|
+
readonly types?: Record<
|
|
278
|
+
string,
|
|
279
|
+
'number' | 'string' | 'string[]' | 'number[]' | ((value: string) => unknown)
|
|
280
|
+
>;
|
|
172
281
|
};
|
|
173
282
|
|
|
174
283
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
package/base.js
CHANGED
|
@@ -32,8 +32,8 @@ _export(exports, {
|
|
|
32
32
|
}
|
|
33
33
|
});
|
|
34
34
|
var _decodeUriComponent = /*#__PURE__*/ _interopRequireDefault(require("decode-uri-component"));
|
|
35
|
-
var _splitOnFirst = /*#__PURE__*/ _interopRequireDefault(require("split-on-first"));
|
|
36
35
|
var _filterObj = require("filter-obj");
|
|
36
|
+
var _splitOnFirst = /*#__PURE__*/ _interopRequireDefault(require("split-on-first"));
|
|
37
37
|
function _arrayLikeToArray(arr, len) {
|
|
38
38
|
if (len == null || len > arr.length) len = arr.length;
|
|
39
39
|
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
@@ -351,9 +351,7 @@ function parserForArrayFormat(options) {
|
|
|
351
351
|
accumulator[key] = value ? decode(value, options) : value;
|
|
352
352
|
return;
|
|
353
353
|
}
|
|
354
|
-
var arrayValue = value === null ? [] : value.split(options.arrayFormatSeparator)
|
|
355
|
-
return decode(item, options);
|
|
356
|
-
});
|
|
354
|
+
var arrayValue = value === null ? [] : decode(value, options).split(options.arrayFormatSeparator);
|
|
357
355
|
if (accumulator[key] === undefined) {
|
|
358
356
|
accumulator[key] = arrayValue;
|
|
359
357
|
return;
|
|
@@ -422,11 +420,21 @@ function getHash(url) {
|
|
|
422
420
|
}
|
|
423
421
|
return hash;
|
|
424
422
|
}
|
|
425
|
-
function parseValue(value, options) {
|
|
423
|
+
function parseValue(value, options, type) {
|
|
424
|
+
if (type === "string" && typeof value === "string") {
|
|
425
|
+
return value;
|
|
426
|
+
}
|
|
427
|
+
if (typeof type === "function" && typeof value === "string") {
|
|
428
|
+
return type(value);
|
|
429
|
+
}
|
|
430
|
+
if (options.parseBooleans && value !== null && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) {
|
|
431
|
+
return value.toLowerCase() === "true";
|
|
432
|
+
}
|
|
433
|
+
if (type === "number" && !Number.isNaN(Number(value)) && typeof value === "string" && value.trim() !== "") {
|
|
434
|
+
return Number(value);
|
|
435
|
+
}
|
|
426
436
|
if (options.parseNumbers && !Number.isNaN(Number(value)) && typeof value === "string" && value.trim() !== "") {
|
|
427
|
-
|
|
428
|
-
} else if (options.parseBooleans && value !== null && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) {
|
|
429
|
-
value = value.toLowerCase() === "true";
|
|
437
|
+
return Number(value);
|
|
430
438
|
}
|
|
431
439
|
return value;
|
|
432
440
|
}
|
|
@@ -445,7 +453,8 @@ function parse(query, options) {
|
|
|
445
453
|
arrayFormat: "none",
|
|
446
454
|
arrayFormatSeparator: ",",
|
|
447
455
|
parseNumbers: false,
|
|
448
|
-
parseBooleans: false
|
|
456
|
+
parseBooleans: false,
|
|
457
|
+
types: Object.create(null)
|
|
449
458
|
}, options);
|
|
450
459
|
validateArrayFormatSeparator(options.arrayFormatSeparator);
|
|
451
460
|
var formatter = parserForArrayFormat(options);
|
|
@@ -497,12 +506,13 @@ function parse(query, options) {
|
|
|
497
506
|
try {
|
|
498
507
|
for(var _iterator1 = Object.entries(returnValue)[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
|
|
499
508
|
var _value = _slicedToArray(_step1.value, 2), key1 = _value[0], value1 = _value[1];
|
|
500
|
-
if (typeof value1 === "object" && value1 !== null) {
|
|
509
|
+
if (typeof value1 === "object" && value1 !== null && options.types[key1] !== "string") {
|
|
501
510
|
var _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = undefined;
|
|
502
511
|
try {
|
|
503
512
|
for(var _iterator2 = Object.entries(value1)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true){
|
|
504
513
|
var _value1 = _slicedToArray(_step2.value, 2), key2 = _value1[0], value2 = _value1[1];
|
|
505
|
-
|
|
514
|
+
var type = options.types[key1] ? options.types[key1].replace("[]", "") : undefined;
|
|
515
|
+
value1[key2] = parseValue(value2, options, type);
|
|
506
516
|
}
|
|
507
517
|
} catch (err) {
|
|
508
518
|
_didIteratorError2 = true;
|
|
@@ -518,8 +528,10 @@ function parse(query, options) {
|
|
|
518
528
|
}
|
|
519
529
|
}
|
|
520
530
|
}
|
|
531
|
+
} else if (typeof value1 === "object" && value1 !== null && options.types[key1] === "string") {
|
|
532
|
+
returnValue[key1] = Object.values(value1).join(options.arrayFormatSeparator);
|
|
521
533
|
} else {
|
|
522
|
-
returnValue[key1] = parseValue(value1, options);
|
|
534
|
+
returnValue[key1] = parseValue(value1, options, options.types[key1]);
|
|
523
535
|
}
|
|
524
536
|
}
|
|
525
537
|
} catch (err) {
|