Skip to content
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

[fix] comma option enabled value parsing to use strings only #334

Merged
merged 1 commit into from Oct 4, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
ljharb marked this conversation as resolved.
Show resolved Hide resolved
val = val.split(',');
}

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

t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
var decoder = function (str, defaultDecoder, charset, type) {
if (!isNaN(Number(str))) {
return parseFloat(str);
}
return defaultDecoder(str, defaultDecoder, charset, type);
};

st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });

st.end();
});

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