@common.js/query-string 9.0.0 → 9.1.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/README.md +1 -1
- package/base.d.ts +110 -1
- package/base.js +23 -9
- 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.0](https://www.npmjs.com/package/query-string/v/9.1.0) 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];
|
|
@@ -422,11 +422,21 @@ function getHash(url) {
|
|
|
422
422
|
}
|
|
423
423
|
return hash;
|
|
424
424
|
}
|
|
425
|
-
function parseValue(value, options) {
|
|
425
|
+
function parseValue(value, options, type) {
|
|
426
|
+
if (type === "string" && typeof value === "string") {
|
|
427
|
+
return value;
|
|
428
|
+
}
|
|
429
|
+
if (typeof type === "function" && typeof value === "string") {
|
|
430
|
+
return type(value);
|
|
431
|
+
}
|
|
432
|
+
if (options.parseBooleans && value !== null && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) {
|
|
433
|
+
return value.toLowerCase() === "true";
|
|
434
|
+
}
|
|
435
|
+
if (type === "number" && !Number.isNaN(Number(value)) && typeof value === "string" && value.trim() !== "") {
|
|
436
|
+
return Number(value);
|
|
437
|
+
}
|
|
426
438
|
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";
|
|
439
|
+
return Number(value);
|
|
430
440
|
}
|
|
431
441
|
return value;
|
|
432
442
|
}
|
|
@@ -445,7 +455,8 @@ function parse(query, options) {
|
|
|
445
455
|
arrayFormat: "none",
|
|
446
456
|
arrayFormatSeparator: ",",
|
|
447
457
|
parseNumbers: false,
|
|
448
|
-
parseBooleans: false
|
|
458
|
+
parseBooleans: false,
|
|
459
|
+
types: Object.create(null)
|
|
449
460
|
}, options);
|
|
450
461
|
validateArrayFormatSeparator(options.arrayFormatSeparator);
|
|
451
462
|
var formatter = parserForArrayFormat(options);
|
|
@@ -497,12 +508,13 @@ function parse(query, options) {
|
|
|
497
508
|
try {
|
|
498
509
|
for(var _iterator1 = Object.entries(returnValue)[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
|
|
499
510
|
var _value = _slicedToArray(_step1.value, 2), key1 = _value[0], value1 = _value[1];
|
|
500
|
-
if (typeof value1 === "object" && value1 !== null) {
|
|
511
|
+
if (typeof value1 === "object" && value1 !== null && options.types[key1] !== "string") {
|
|
501
512
|
var _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = undefined;
|
|
502
513
|
try {
|
|
503
514
|
for(var _iterator2 = Object.entries(value1)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true){
|
|
504
515
|
var _value1 = _slicedToArray(_step2.value, 2), key2 = _value1[0], value2 = _value1[1];
|
|
505
|
-
|
|
516
|
+
var type = options.types[key1] ? options.types[key1].replace("[]", "") : undefined;
|
|
517
|
+
value1[key2] = parseValue(value2, options, type);
|
|
506
518
|
}
|
|
507
519
|
} catch (err) {
|
|
508
520
|
_didIteratorError2 = true;
|
|
@@ -518,8 +530,10 @@ function parse(query, options) {
|
|
|
518
530
|
}
|
|
519
531
|
}
|
|
520
532
|
}
|
|
533
|
+
} else if (typeof value1 === "object" && value1 !== null && options.types[key1] === "string") {
|
|
534
|
+
returnValue[key1] = Object.values(value1).join(options.arrayFormatSeparator);
|
|
521
535
|
} else {
|
|
522
|
-
returnValue[key1] = parseValue(value1, options);
|
|
536
|
+
returnValue[key1] = parseValue(value1, options, options.types[key1]);
|
|
523
537
|
}
|
|
524
538
|
}
|
|
525
539
|
} catch (err) {
|