Skip to content

Commit

Permalink
[fix] when comma option enabled value parsing to use strings only
Browse files Browse the repository at this point in the history
Fixes #334.
  • Loading branch information
Mohamed Omar authored and ljharb committed Oct 4, 2019
1 parent 670254b commit 982681a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/parse.js
Expand Up @@ -83,7 +83,7 @@ var parseValues = function parseQueryStringValues(str, options) {
val = interpretNumericEntities(val);
}

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

Expand Down
29 changes: 29 additions & 0 deletions test/parse.js
Expand Up @@ -400,6 +400,35 @@ test('parse()', function (t) {
st.end();
});

t.test('use numbers decoder and parses string with comma as array divider',
function (st) {
var decoder = function (str, defaultDecoder, charset, type) {
if (!isNaN(str)) {
return parseFloat(str);
}

if (str && typeof str === 'string' && str.indexOf(',') > -1) {
return str.split(',').map(function (item) {
if (!isNaN(item)) {
return parseFloat(item);
}
return defaultDecoder(item, defaultDecoder, charset, type);
});
}

return defaultDecoder(str, defaultDecoder, charset, type);
};
st.deepEqual(qs.parse('foo[bar]=1.2,1.5', { comma: true, decoder: decoder }), {
foo: { bar: [1.2, 1.5] }
});
st.deepEqual(qs.parse('foo=1,2', { comma: true, decoder: decoder }), { foo: [1, 2] });
st.deepEqual(qs.parse('foo=1.2,2.3', { comma: true, decoder: decoder }), { foo: [1.2, 2.3] });
st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true, decoder: decoder }), { foo: null });

st.end();
}
);

t.test('parses an object in dot notation', function (st) {
var input = {
'user.name': { 'pop[bob]': 3 },
Expand Down

0 comments on commit 982681a

Please sign in to comment.