From 48177b155cb96ce63aea448e30621d608382653c Mon Sep 17 00:00:00 2001 From: scottenock Date: Thu, 1 May 2025 17:56:17 +0100 Subject: [PATCH] FIX: types option - single array item parsing --- base.js | 12 ++++++++++-- test/parse.js | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/base.js b/base.js index ad9e7b1..e67109d 100644 --- a/base.js +++ b/base.js @@ -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); } diff --git a/test/parse.js b/test/parse.js index 34929e5..d20f540 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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: { @@ -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], + }); +});