Skip to content

FEAT: implement support for boolean type #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ export type ParseOptions = {
readonly parseFragmentIdentifier?: boolean;

/**
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.)

@default {}

Expand Down Expand Up @@ -217,7 +217,7 @@ export type ParseOptions = {
```

@example
Parse `age` as a number, even when `parseNumber` is false:
Force `age` to be parsed as a number even when `parseNumbers` is false:
```
import queryString from 'query-string';

Expand All @@ -230,7 +230,7 @@ export type ParseOptions = {
```

@example
Parse `age` using a custom value parser:
Use a custom parser function to transform the value of `age`:
```
import queryString from 'query-string';

Expand All @@ -243,7 +243,7 @@ export type ParseOptions = {
```

@example
Array types will have no effect when `arrayFormat` is set to `none`
Array types are ignored when `arrayFormat` is set to `'none'`:
```
queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', {
arrayFormat: 'none',
Expand All @@ -256,7 +256,7 @@ export type ParseOptions = {
```

@example
Parse a query utilizing all types:
Parse a query using multiple type definitions:
```
import queryString from 'query-string';

Expand All @@ -273,10 +273,22 @@ export type ParseOptions = {
});
//=> {ids: '001,002,003', items: ['1', '2', '3'], price: '22.00', numbers: [1, 2, 3], double: 10, number: 20}
```

@example
Force `flagged` to be parsed as a boolean even when `parseBooleans` is false:
````
queryString.parse('?isAdmin=true&flagged=true', {
parseBooleans: false,
types: {
flagged: 'boolean',
},
});
//=> { isAdmin: 'true', flagged: true }
````
*/
readonly types?: Record<
string,
'number' | 'string' | 'string[]' | 'number[]' | ((value: string) => unknown)
'boolean' | 'number' | 'string' | 'string[]' | 'number[]' | ((value: string) => unknown)
>;
};

Expand Down
4 changes: 4 additions & 0 deletions base.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ function parseValue(value, options, type) {
return type(value);
}

if (type === 'boolean' && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
return value.toLowerCase() === 'true';
}

if (type === 'string[]' && options.arrayFormat !== 'none' && typeof value === 'string') {
return [value];
}
Expand Down
23 changes: 19 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,29 @@ Parse the value as a boolean type instead of string type if it's a boolean.
Type: `object`\
Default: `{}`

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`.

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.

It is possible to provide a custom function as the parameter type. The parameter's value will equal the function's return value.

Supported Types:


- `'boolean'`: Parse `flagged` as a boolean (overriding the `parseBooleans` option):

```js
queryString.parse('?isAdmin=true&flagged=true', {
parseBooleans: false,
types: {
flagged: 'boolean',
},
});
//=> { isAdmin: 'true', flagged: true }
```

- `'string'`: Parse `phoneNumber` as a string (overriding the `parseNumbers` option):

```js
Expand Down Expand Up @@ -268,7 +283,7 @@ queryString.parse('?age=20&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'`.

```js
queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', {
Expand Down
11 changes: 11 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,14 @@ test('types option: single element with `{arrayFormat: "comma"}, and type: numbe
a: [1],
});
});

test('types option: can parse boolean when parseboolean is false', t => {
t.deepEqual(queryString.parse('a=true', {
parsebooleans: false,
types: {
a: 'boolean',
},
}), {
a: true,
});
});