query-string
Advanced tools
Comparing version
@@ -181,9 +181,9 @@ export type ParseOptions = { | ||
/** | ||
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`. | ||
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`. | ||
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). | ||
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). | ||
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). | ||
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 (see Example 4). | ||
NOTE: Array types (`string[]` and `number[]`) will have no effect if `arrayFormat` is set to `none` (see example 5). | ||
NOTE: Array types (`string[]`, `number[]`) are ignored if `arrayFormat` is set to `'none'`. (See Example 5.) | ||
@@ -221,3 +221,3 @@ @default {} | ||
@example | ||
Parse `age` as a number, even when `parseNumber` is false: | ||
Force `age` to be parsed as a number even when `parseNumbers` is false: | ||
``` | ||
@@ -231,7 +231,7 @@ import queryString from 'query-string'; | ||
}); | ||
//=> {age: 20, id: '01234', zipcode: '90210 } | ||
//=> {age: 20, id: '01234', zipcode: '90210'} | ||
``` | ||
@example | ||
Parse `age` using a custom value parser: | ||
Use a custom parser function to transform the value of `age`: | ||
``` | ||
@@ -245,7 +245,7 @@ import queryString from 'query-string'; | ||
}); | ||
//=> {age: 40, id: '01234', zipcode: '90210 } | ||
//=> {age: 40, id: '01234', zipcode: '90210'} | ||
``` | ||
@example | ||
Array types will have no effect when `arrayFormat` is set to `none` | ||
Array types are ignored when `arrayFormat` is set to `'none'`: | ||
``` | ||
@@ -263,3 +263,3 @@ queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', { | ||
@example | ||
Parse a query utilizing all types: | ||
Parse a query using multiple type definitions: | ||
``` | ||
@@ -281,6 +281,20 @@ import queryString from 'query-string'; | ||
``` | ||
@example | ||
Force `flagged` to be parsed as a boolean even when `parseBooleans` is false: | ||
``` | ||
queryString.parse('?isAdmin=true&flagged=true&isOkay=0', { | ||
parseBooleans: false, | ||
types: { | ||
flagged: 'boolean', | ||
isOkay: 'boolean', | ||
}, | ||
}); | ||
//=> {isAdmin: 'true', flagged: true, isOkay: false} | ||
``` | ||
Note: The `'boolean'` type will also convert `'0'` and `'1'` string values to booleans. | ||
*/ | ||
readonly types?: Record< | ||
string, | ||
'number' | 'string' | 'string[]' | 'number[]' | ((value: string) => unknown) | ||
'boolean' | 'number' | 'string' | 'string[]' | 'number[]' | ((value: string) => unknown) | ||
>; | ||
@@ -287,0 +301,0 @@ }; |
@@ -312,2 +312,10 @@ import decodeComponent from 'decode-uri-component'; | ||
if (type === 'boolean' && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) { | ||
return value.toLowerCase() === 'true'; | ||
} | ||
if (type === 'boolean' && value !== null && (value.toLowerCase() === '1' || value.toLowerCase() === '0')) { | ||
return value.toLowerCase() === '1'; | ||
} | ||
if (type === 'string[]' && options.arrayFormat !== 'none' && typeof value === 'string') { | ||
@@ -314,0 +322,0 @@ return [value]; |
{ | ||
"name": "query-string", | ||
"version": "9.1.2", | ||
"version": "9.2.0", | ||
"description": "Parse and stringify URL query strings", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -196,10 +196,28 @@ # query-string | ||
Specify a pre-defined schema to be used when parsing values. The types specified will take precedence over options such as: `parseNumbers`, `parseBooleans`, and `arrayFormat`. | ||
Use this feature to override the type of a value. This can be useful when the type is ambiguous such as a phone number. | ||
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`. | ||
It is possible to provide a custom function as the parameter type. The parameter's value will equal the function's return value. | ||
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). | ||
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. | ||
Supported Types: | ||
- `'boolean'`: Parse `flagged` as a boolean (overriding the `parseBooleans` option): | ||
```js | ||
queryString.parse('?isAdmin=true&flagged=true&isOkay=0', { | ||
parseBooleans: false, | ||
types: { | ||
flagged: 'boolean', | ||
isOkay: 'boolean', | ||
}, | ||
}); | ||
//=> {isAdmin: 'true', flagged: true, isOkay: false} | ||
``` | ||
Note: The `'boolean'` type will also convert `'0'` and `'1'` string values to booleans. | ||
- `'string'`: Parse `phoneNumber` as a string (overriding the `parseNumbers` option): | ||
@@ -229,3 +247,3 @@ | ||
}); | ||
//=> {age: 20, id: '01234', zipcode: '90210 } | ||
//=> {age: 20, id: '01234', zipcode: '90210'} | ||
``` | ||
@@ -270,6 +288,6 @@ | ||
}); | ||
//=> {age: 40, id: '01234', zipcode: '90210 } | ||
//=> {age: 40, id: '01234', zipcode: '90210'} | ||
``` | ||
NOTE: Array types (`string[]` and `number[]`) will have no effect if `arrayFormat` is set to `none`. | ||
NOTE: Array types (`string[]`, `number[]`) are ignored if `arrayFormat` is set to `'none'`. | ||
@@ -297,3 +315,3 @@ ```js | ||
}); | ||
//=> {age: 40, id: '01234', zipcode: '90210 } | ||
//=> {age: 40, id: '01234', zipcode: '90210'} | ||
``` | ||
@@ -300,0 +318,0 @@ |
52828
2.64%986
1.96%716
2.58%