Skip to content
Merged
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
3 changes: 2 additions & 1 deletion base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ export type ParseOptions = {
});
//=> {isAdmin: 'true', flagged: true, isOkay: false}
```
Note: The `'boolean'` type will also convert `'0'` and `'1'` string values to booleans.

Note: The `'boolean'` type also converts `'0'` and `'1'` to booleans, and treats valueless keys (e.g. `?flag`) as `true`.
*/
readonly types?: Record<
string,
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) {
return true;
}

if (type === 'boolean' && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
return value.toLowerCase() === 'true';
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ queryString.parse('?isAdmin=true&flagged=true&isOkay=0', {
//=> {isAdmin: 'true', flagged: true, isOkay: false}
```

Note: The `'boolean'` type will also convert `'0'` and `'1'` string values to booleans.
Note: The `'boolean'` type also converts `'0'` and `'1'` to booleans, and treats valueless keys (e.g. `?flag`) as `true`.

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

Expand Down
16 changes: 16 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,19 @@ test('types option: boolean type accepts 1 and 0 as boolean values', t => {
b: false,
});
});

test('types option: boolean type accepts an empty string as true', t => {
t.deepEqual(
queryString.parse('a&b', {
parsebooleans: false,
types: {
a: 'boolean',
b: 'boolean',
},
}),
{
a: true,
b: true,
},
);
});