Skip to content

Commit

Permalink
[Fix] parse: Fix parsing array from object with comma true
Browse files Browse the repository at this point in the history
Fixes #357
  • Loading branch information
Alexander Yefanov authored and ljharb committed Mar 15, 2020
1 parent 0625c49 commit eecd28d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
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

0 comments on commit eecd28d

Please sign in to comment.