Skip to content

Commit 698b683

Browse files
Mohamed Omarljharb
authored andcommitted
[fix] parse: with comma true, do not split non-string values
1 parent 670254b commit 698b683

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var parseValues = function parseQueryStringValues(str, options) {
8383
val = interpretNumericEntities(val);
8484
}
8585

86-
if (val && options.comma && val.indexOf(',') > -1) {
86+
if (val && typeof val === 'string' && options.comma && val.indexOf(',') > -1) {
8787
val = val.split(',');
8888
}
8989

test/parse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,20 @@ test('parse()', function (t) {
400400
st.end();
401401
});
402402

403+
t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
404+
var decoder = function (str, defaultDecoder, charset, type) {
405+
if (!isNaN(Number(str))) {
406+
return parseFloat(str);
407+
}
408+
return defaultDecoder(str, defaultDecoder, charset, type);
409+
};
410+
411+
st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
412+
st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });
413+
414+
st.end();
415+
});
416+
403417
t.test('parses an object in dot notation', function (st) {
404418
var input = {
405419
'user.name': { 'pop[bob]': 3 },

0 commit comments

Comments
 (0)