Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

scottenock
Copy link
Contributor

@scottenock scottenock commented May 8, 2025

Hey @sindresorhus , when implementing the parsing fix for PR #406 I noticed it would be pretty quick to add support for a boolean type. This allows the user to override the behaviour of parseBoolean.

I've updated the README and type definitions to include the new boolean type.

I've also updated the description of the types feature to be clearer. Let me know your thoughts!

@sindresorhus
Copy link
Owner

Thanks for implementing this. I think it should also accept 0 and 1 as those are commonly used as booleans.

@scottenock
Copy link
Contributor Author

Good shout! I've added support for 0 and 1 when the boolean type is used.

I haven’t extended this to the parseBoolean option yet—wanted to check with you first @sindresorhus . If you think it makes sense, I’ll get it added. Otherwise, I believe the PR is good to go 💪

@sindresorhus
Copy link
Owner

I haven’t extended this to the parseBoolean option yet—wanted to check with you first @sindresorhus . If you think it makes sense, I’ll get it added. Otherwise, I believe the PR is good to go 💪

No, it should not. parseBooleans means that it would parse numbers as booleans, even if it's meant to be a number. For example, a query parameter that could be 0, 1, 2, 3, etc.

@sindresorhus sindresorhus merged commit 10d263a into sindresorhus:main May 20, 2025
2 checks passed
@julienw
Copy link

julienw commented May 28, 2025

Hey folks,
question: what about ?booleanParam without a value? I feel like it should be true. But it's now parsed as "null".

Here is the added test:

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,
		},
	);
});

@sindresorhus
Copy link
Owner

Yeah, that makes sense to me.

@scottenock
Copy link
Contributor Author

@sindresorhus I'll get this implemented 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants