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

[update] stringify with comma:true to decode comma for an array as a reserved char per RFC3986 #338

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions lib/stringify.js
Expand Up @@ -100,6 +100,21 @@ var stringify = function stringify(
return [formatter(prefix) + '=' + formatter(String(obj))];
}

if (generateArrayPrefix === 'comma' && isArray(obj)) {
if (formatter !== formats.formatters['RFC3986']) {
obj = obj.join(',');
} else {
if (encoder) {
var commaKey = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset, 'key');
var commaValue = obj.map(function (item) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't add a dependency on .map here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is wrong with the .map?

Copy link
Owner

@ljharb ljharb May 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petersolopov Pre-ES5 browsers won't have it. The PR can be rebased and use utils.maybeMap instead.

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
16 changes: 14 additions & 2 deletions test/stringify.js
Expand Up @@ -105,7 +105,17 @@ 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(
qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma', format: 'RFC3986' }),
'a=b,c,d',
'comma => comma'
);
st.equal(
qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma', format: 'RFC1738' }),
'a=b%2cc%2cd',
'comma => comma'
);
st.equal(
Expand Down Expand Up @@ -134,7 +144,9 @@ 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'] } }, { arrayFormat: 'comma', format: 'RFC3986' }), 'a%5Bb%5D=c,d'); // a[b]=c,d
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'comma', format: 'RFC1738' }), 'a%5Bb%5D=c%2cd'); // 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