Skip to content

Commit 3e61882

Browse files
authored
Fix types option with arrayFormat: comma and one item in array (#406)
1 parent 88e1e36 commit 3e61882

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

base.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,22 @@ function parseValue(value, options, type) {
309309
return type(value);
310310
}
311311

312-
if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
313-
return value.toLowerCase() === 'true';
312+
if (type === 'string[]' && options.arrayFormat !== 'none' && typeof value === 'string') {
313+
return [value];
314+
}
315+
316+
if (type === 'number[]' && options.arrayFormat !== 'none' && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
317+
return [Number(value)];
314318
}
315319

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

324+
if (options.parseBooleans && value !== null && (value.toLowerCase() === 'true' || value.toLowerCase() === 'false')) {
325+
return value.toLowerCase() === 'true';
326+
}
327+
320328
if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === 'string' && value.trim() !== '')) {
321329
return Number(value);
322330
}

test/parse.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,7 @@ test('types option: all supported types work in conjunction with one another', t
547547
});
548548
});
549549

550-
// https://github.com/sindresorhus/query-string/issues/404
551-
test.failing('types option: single element with `{arrayFormat: "comma"}`', t => {
550+
test('types option: single element with `{arrayFormat: "comma"} and type: string[]`', t => {
552551
t.deepEqual(queryString.parse('a=b', {
553552
arrayFormat: 'comma',
554553
types: {
@@ -558,3 +557,14 @@ test.failing('types option: single element with `{arrayFormat: "comma"}`', t =>
558557
a: ['b'],
559558
});
560559
});
560+
561+
test('types option: single element with `{arrayFormat: "comma"}, and type: number[]`', t => {
562+
t.deepEqual(queryString.parse('a=1', {
563+
arrayFormat: 'comma',
564+
types: {
565+
a: 'number[]',
566+
},
567+
}), {
568+
a: [1],
569+
});
570+
});

0 commit comments

Comments
 (0)