Skip to content

Commit

Permalink
[update] stringify with comma:true to decode comma for an array as a …
Browse files Browse the repository at this point in the history
…reserved char per RFC3986
  • Loading branch information
Mohamed Omar committed Oct 18, 2019
1 parent f884e2d commit 4c4f45b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ var stringify = function stringify(
obj = filter(prefix, obj);
} else if (obj instanceof Date) {
obj = serializeDate(obj);
} else if (generateArrayPrefix === 'comma' && isArray(obj)) {
obj = obj.join(',');
}

if (obj === null) {
Expand All @@ -94,6 +92,17 @@ var stringify = function stringify(
return [formatter(prefix) + '=' + formatter(String(obj))];
}

if (generateArrayPrefix === 'comma' && isArray(obj)) {
if (encoder) {
var commaKey = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset, 'key');
var commaValue = obj.map(function (item) {
return formatter(encoder(item, defaults.encoder, charset, 'value'));
});
return [formatter(commaKey) + '=' + commaValue];
}
return [formatter(prefix) + '=' + formatter(String(obj))];
}

var values = [];

if (typeof obj === 'undefined') {
Expand Down
4 changes: 2 additions & 2 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ test('stringify()', function (t) {
);
st.equal(
qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma' }),
'a=b%2Cc%2Cd',
'a=b,c,d',
'comma => comma'
);
st.equal(
Expand Down Expand Up @@ -134,7 +134,7 @@ test('stringify()', function (t) {
t.test('stringifies a nested array value', function (st) {
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'indices' }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'brackets' }), 'a%5Bb%5D%5B%5D=c&a%5Bb%5D%5B%5D=d');
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'comma' }), 'a%5Bb%5D=c%2Cd'); // a[b]=c,d
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'comma' }), 'a%5Bb%5D=c,d'); // a[b]=c,d
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
st.end();
});
Expand Down

0 comments on commit 4c4f45b

Please sign in to comment.