From 73b37ada8706cf4c43afa63d4ce64d57f893f922 Mon Sep 17 00:00:00 2001 From: scottenock Date: Wed, 2 Jul 2025 14:58:45 +0100 Subject: [PATCH 1/3] FEAT: boolean type accepts an empty string as true --- base.js | 4 ++++ test/parse.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/base.js b/base.js index 07daf47..9b9ab4f 100644 --- a/base.js +++ b/base.js @@ -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'; } diff --git a/test/parse.js b/test/parse.js index ad25dbe..0befd37 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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, + }, + ); +}); From e89d3afbd68d59963b77c310df7a6211732debae Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 3 Jul 2025 22:25:24 +0200 Subject: [PATCH 2/3] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 1813507..a381843 100644 --- a/readme.md +++ b/readme.md @@ -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): From 36c2964a0a6905ad17bc05fedecdedeb6df4f3b6 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 3 Jul 2025 22:26:03 +0200 Subject: [PATCH 3/3] Update base.d.ts --- base.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base.d.ts b/base.d.ts index a008640..77ec1a8 100644 --- a/base.d.ts +++ b/base.d.ts @@ -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,