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 issue with parsing array from object #359

Merged
merged 1 commit into from Mar 22, 2020
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
14 changes: 10 additions & 4 deletions lib/parse.js
Expand Up @@ -29,6 +29,14 @@ var interpretNumericEntities = function (str) {
});
};

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

return val;
};

// This is what browsers will submit when the ✓ character occurs in an
// application/x-www-form-urlencoded body and the encoding of the page containing
// the form is iso-8859-1, or when the submitted form has an accept-charset
Expand Down Expand Up @@ -84,9 +92,7 @@ var parseValues = function parseQueryStringValues(str, options) {
val = interpretNumericEntities(val);
}

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

if (part.indexOf('[]=') > -1) {
val = isArray(val) ? [val] : val;
Expand All @@ -103,7 +109,7 @@ var parseValues = function parseQueryStringValues(str, options) {
};

var parseObject = function (chain, val, options) {
var leaf = val;
var leaf = parseArrayValue(val, options);

for (var i = chain.length - 1; i >= 0; --i) {
var obj;
Expand Down
6 changes: 6 additions & 0 deletions test/parse.js
Expand Up @@ -400,6 +400,12 @@ test('parse()', function (t) {
st.end();
});

t.test('parses values with comma as array divider', function (st) {
st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });
st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });
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))) {
Expand Down