Skip to content

Commit 4c4f45b

Browse files
author
Mohamed Omar
committed
[update] stringify with comma:true to decode comma for an array as a reserved char per RFC3986
1 parent f884e2d commit 4c4f45b

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/stringify.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ var stringify = function stringify(
7474
obj = filter(prefix, obj);
7575
} else if (obj instanceof Date) {
7676
obj = serializeDate(obj);
77-
} else if (generateArrayPrefix === 'comma' && isArray(obj)) {
78-
obj = obj.join(',');
7977
}
8078

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

95+
if (generateArrayPrefix === 'comma' && isArray(obj)) {
96+
if (encoder) {
97+
var commaKey = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset, 'key');
98+
var commaValue = obj.map(function (item) {
99+
return formatter(encoder(item, defaults.encoder, charset, 'value'));
100+
});
101+
return [formatter(commaKey) + '=' + commaValue];
102+
}
103+
return [formatter(prefix) + '=' + formatter(String(obj))];
104+
}
105+
97106
var values = [];
98107

99108
if (typeof obj === 'undefined') {

test/stringify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ test('stringify()', function (t) {
105105
);
106106
st.equal(
107107
qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma' }),
108-
'a=b%2Cc%2Cd',
108+
'a=b,c,d',
109109
'comma => comma'
110110
);
111111
st.equal(
@@ -134,7 +134,7 @@ test('stringify()', function (t) {
134134
t.test('stringifies a nested array value', function (st) {
135135
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'indices' }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
136136
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'brackets' }), 'a%5Bb%5D%5B%5D=c&a%5Bb%5D%5B%5D=d');
137-
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'comma' }), 'a%5Bb%5D=c%2Cd'); // a[b]=c,d
137+
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'comma' }), 'a%5Bb%5D=c,d'); // a[b]=c,d
138138
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
139139
st.end();
140140
});

0 commit comments

Comments
 (0)