Skip to content

Commit 15e3ee2

Browse files
committed
FEAT: enable boolean parsing for '0' and '1' values
1 parent 440bb46 commit 15e3ee2

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

base.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,17 @@ export type ParseOptions = {
276276
277277
@example
278278
Force `flagged` to be parsed as a boolean even when `parseBooleans` is false:
279-
````
280-
queryString.parse('?isAdmin=true&flagged=true', {
279+
```
280+
queryString.parse('?isAdmin=true&flagged=true&isOkay=0', {
281281
parseBooleans: false,
282282
types: {
283283
flagged: 'boolean',
284+
isOkay: 'boolean',
284285
},
285286
});
286-
//=> { isAdmin: 'true', flagged: true }
287-
````
287+
//=> { isAdmin: 'true', flagged: true, isOkay: false }
288+
```
289+
Note: The 'boolean' type will also convert "0" and "1" string values to booleans.
288290
*/
289291
readonly types?: Record<
290292
string,

base.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ function parseValue(value, options, type) {
313313
return value.toLowerCase() === 'true';
314314
}
315315

316+
if (type === 'boolean' && value !== null && (value.toLowerCase() === '1' || value.toLowerCase() === '0')) {
317+
return value.toLowerCase() === '1';
318+
}
319+
316320
if (type === 'string[]' && options.arrayFormat !== 'none' && typeof value === 'string') {
317321
return [value];
318322
}

readme.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,18 @@ Supported Types:
207207
- `'boolean'`: Parse `flagged` as a boolean (overriding the `parseBooleans` option):
208208

209209
```js
210-
queryString.parse('?isAdmin=true&flagged=true', {
211-
parseBooleans: false,
212-
types: {
213-
flagged: 'boolean',
214-
},
210+
queryString.parse('?isAdmin=true&flagged=true&isOkay=0', {
211+
parseBooleans: false,
212+
types: {
213+
flagged: 'boolean',
214+
isOkay: 'boolean',
215+
},
215216
});
216-
//=> { isAdmin: 'true', flagged: true }
217+
//=> { isAdmin: 'true', flagged: true, isOkay: false }
217218
```
218219

220+
Note: The 'boolean' type will also convert "0" and "1" string values to booleans.
221+
219222
- `'string'`: Parse `phoneNumber` as a string (overriding the `parseNumbers` option):
220223

221224
```js

test/parse.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,16 @@ test('types option: can parse boolean when parseboolean is false', t => {
579579
a: true,
580580
});
581581
});
582+
583+
test('types option: boolean type accepts 1 and 0 as boolean values', t => {
584+
t.deepEqual(queryString.parse('a=1&b=0', {
585+
parsebooleans: false,
586+
types: {
587+
a: 'boolean',
588+
b: 'boolean'
589+
},
590+
}), {
591+
a: true,
592+
b: false,
593+
});
594+
});

0 commit comments

Comments
 (0)