Skip to content

FIX: types option - single array item parsing #406

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

Merged
merged 1 commit into from
May 2, 2025
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
12 changes: 10 additions & 2 deletions base.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,22 @@ function parseValue(value, options, type) {
return type(value);
}

if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
return value.toLowerCase() === 'true';
if (type === 'string[]' && options.arrayFormat !== 'none' && typeof value === 'string') {
return [value];
}

if (type === 'number[]' && options.arrayFormat !== 'none' && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
return [Number(value)];
}

if (type === 'number' && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
return Number(value);
}

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

if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
return Number(value);
}
Expand Down
14 changes: 12 additions & 2 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@ test('types option: all supported types work in conjunction with one another', t
});
});

// https://github.com/sindresorhus/query-string/issues/404
test.failing('types option: single element with `{arrayFormat: "comma"}`', t => {
test('types option: single element with `{arrayFormat: "comma"} and type: string[]`', t => {
t.deepEqual(queryString.parse('a=b', {
arrayFormat: 'comma',
types: {
Expand All @@ -558,3 +557,14 @@ test.failing('types option: single element with `{arrayFormat: "comma"}`', t =>
a: ['b'],
});
});

test('types option: single element with `{arrayFormat: "comma"}, and type: number[]`', t => {
t.deepEqual(queryString.parse('a=1', {
arrayFormat: 'comma',
types: {
a: 'number[]',
},
}), {
a: [1],
});
});