query-string 9.3.0 → 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 +21 -6
- package/base.js +2 -1
- package/package.json +1 -1
- package/readme.md +13 -6
package/base.d.ts
CHANGED
|
@@ -182,14 +182,14 @@ export type ParseOptions = {
|
|
|
182
182
|
|
|
183
183
|
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).
|
|
184
184
|
|
|
185
|
-
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 (
|
|
185
|
+
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.
|
|
186
186
|
|
|
187
187
|
NOTE: Array types (`string[]`, `number[]`) are ignored if `arrayFormat` is set to `'none'`.
|
|
188
188
|
|
|
189
189
|
@default {}
|
|
190
190
|
|
|
191
191
|
@example
|
|
192
|
-
Parse `phoneNumber` as a string, overriding the `
|
|
192
|
+
Parse `phoneNumber` as a string, overriding the `parseNumbers` option:
|
|
193
193
|
```
|
|
194
194
|
import queryString from 'query-string';
|
|
195
195
|
|
|
@@ -203,12 +203,12 @@ export type ParseOptions = {
|
|
|
203
203
|
```
|
|
204
204
|
|
|
205
205
|
@example
|
|
206
|
-
Parse `items` as an array of strings, overriding the `
|
|
206
|
+
Parse `items` as an array of strings, overriding the `parseNumbers` option:
|
|
207
207
|
```
|
|
208
208
|
import queryString from 'query-string';
|
|
209
209
|
|
|
210
210
|
queryString.parse('?age=20&items=1%2C2%2C3', {
|
|
211
|
-
|
|
211
|
+
parseNumbers: true,
|
|
212
212
|
types: {
|
|
213
213
|
items: 'string[]',
|
|
214
214
|
}
|
|
@@ -236,12 +236,27 @@ export type ParseOptions = {
|
|
|
236
236
|
|
|
237
237
|
queryString.parse('?age=20&id=01234&zipcode=90210', {
|
|
238
238
|
types: {
|
|
239
|
-
age:
|
|
239
|
+
age: value => value * 2,
|
|
240
240
|
}
|
|
241
241
|
});
|
|
242
242
|
//=> {age: 40, id: '01234', zipcode: '90210'}
|
|
243
243
|
```
|
|
244
244
|
|
|
245
|
+
@example
|
|
246
|
+
Custom functions are applied to each array element when using array formats:
|
|
247
|
+
```
|
|
248
|
+
import queryString from 'query-string';
|
|
249
|
+
|
|
250
|
+
// With arrays, the function is applied to each element
|
|
251
|
+
queryString.parse('?scores=10,20,30', {
|
|
252
|
+
arrayFormat: 'comma',
|
|
253
|
+
types: {
|
|
254
|
+
scores: value => Number(value) * 2,
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
//=> {scores: [20, 40, 60]}
|
|
258
|
+
```
|
|
259
|
+
|
|
245
260
|
@example
|
|
246
261
|
Array types are ignored when `arrayFormat` is set to `'none'`:
|
|
247
262
|
```
|
|
@@ -267,7 +282,7 @@ export type ParseOptions = {
|
|
|
267
282
|
items: 'string[]',
|
|
268
283
|
price: 'string',
|
|
269
284
|
numbers: 'number[]',
|
|
270
|
-
double:
|
|
285
|
+
double: value => value * 2,
|
|
271
286
|
number: 'number',
|
|
272
287
|
},
|
|
273
288
|
});
|
package/base.js
CHANGED
|
@@ -403,7 +403,8 @@ export function parse(query, options) {
|
|
|
403
403
|
for (const [key, value] of Object.entries(returnValue)) {
|
|
404
404
|
if (typeof value === 'object' && value !== null && options.types[key] !== 'string') {
|
|
405
405
|
for (const [key2, value2] of Object.entries(value)) {
|
|
406
|
-
const
|
|
406
|
+
const typeOption = options.types[key];
|
|
407
|
+
const type = typeof typeOption === 'function' ? typeOption : (typeOption ? typeOption.replace('[]', '') : undefined);
|
|
407
408
|
value[key2] = parseValue(value2, options, type);
|
|
408
409
|
}
|
|
409
410
|
} else if (typeof value === 'object' && value !== null && options.types[key] === 'string') {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -201,13 +201,11 @@ Parse the value as a boolean type instead of string type if it's a boolean.
|
|
|
201
201
|
Type: `object`\
|
|
202
202
|
Default: `{}`
|
|
203
203
|
|
|
204
|
-
|
|
205
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`.
|
|
206
205
|
|
|
207
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).
|
|
208
207
|
|
|
209
|
-
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.
|
|
210
|
-
|
|
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.
|
|
211
209
|
|
|
212
210
|
Supported Types:
|
|
213
211
|
|
|
@@ -281,17 +279,26 @@ queryString.parse('?age=20&items=1%2C2%2C3', {
|
|
|
281
279
|
//=> {age: '20', items: [1, 2, 3]}
|
|
282
280
|
```
|
|
283
281
|
|
|
284
|
-
- `'Function'`: Provide a custom function as the parameter type. The parameter's value will equal the function's return value.
|
|
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.
|
|
285
283
|
|
|
286
284
|
```js
|
|
287
285
|
import queryString from 'query-string';
|
|
288
286
|
|
|
289
287
|
queryString.parse('?age=20&id=01234&zipcode=90210', {
|
|
290
288
|
types: {
|
|
291
|
-
age:
|
|
289
|
+
age: value => value * 2,
|
|
292
290
|
}
|
|
293
291
|
});
|
|
294
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]}
|
|
295
302
|
```
|
|
296
303
|
|
|
297
304
|
NOTE: Array types (`string[]`, `number[]`) are ignored if `arrayFormat` is set to `'none'`.
|
|
@@ -314,7 +321,7 @@ import queryString from 'query-string';
|
|
|
314
321
|
|
|
315
322
|
queryString.parse('?age=20&id=01234&zipcode=90210', {
|
|
316
323
|
types: {
|
|
317
|
-
age:
|
|
324
|
+
age: value => value * 2,
|
|
318
325
|
}
|
|
319
326
|
});
|
|
320
327
|
//=> {age: 40, id: '01234', zipcode: '90210'}
|