Skip to content

Commit 57918da

Browse files
committed
[Fix] stringify: avoid encoding arrayformat comma when encodeValuesOnly = true (#424)
Fixes #410
1 parent 48673ca commit 57918da

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

lib/stringify.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var arrayPrefixGenerators = {
1818
};
1919

2020
var isArray = Array.isArray;
21+
var split = String.prototype.split;
2122
var push = Array.prototype.push;
2223
var pushToArray = function (arr, valueOrArray) {
2324
push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]);
@@ -89,6 +90,14 @@ var stringify = function stringify(
8990
if (isNonNullishPrimitive(obj) || utils.isBuffer(obj)) {
9091
if (encoder) {
9192
var keyValue = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset);
93+
if (generateArrayPrefix === 'comma' && encodeValuesOnly) {
94+
var valuesArray = split.call(String(obj), ',');
95+
var valuesJoined = '';
96+
for (var i = 0; i < valuesArray.length; ++i) {
97+
valuesJoined += (i === 0 ? '' : ',') + formatter(encoder(valuesArray[i], defaults.encoder, charset));
98+
}
99+
return [formatter(keyValue) + '=' + valuesJoined];
100+
}
92101
return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder, charset))];
93102
}
94103
return [formatter(prefix) + '=' + formatter(String(obj))];
@@ -108,9 +117,9 @@ var stringify = function stringify(
108117
objKeys = sort ? keys.sort(sort) : keys;
109118
}
110119

111-
for (var i = 0; i < objKeys.length; ++i) {
112-
var key = objKeys[i];
113-
var value = obj[key];
120+
for (var j = 0; j < objKeys.length; ++j) {
121+
var key = objKeys[j];
122+
var value = typeof key === 'object' && key.value !== undefined ? key.value : obj[key];
114123

115124
if (skipNulls && value === null) {
116125
continue;

test/stringify.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -134,8 +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'] } }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[b][0]=c&a[b][1]=d');
136136
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[b][]=c&a[b][]=d');
137-
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[b]=c%2Cd');
138-
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[b]=c,d', '(pending issue #378)', { skip: true });
137+
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[b]=c,d');
139138
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true }), 'a[b][0]=c&a[b][1]=d');
140139
st.end();
141140
});
@@ -157,22 +156,13 @@ test('stringify()', function (t) {
157156
'a.b[]=c&a.b[]=d',
158157
'brackets: stringifies with dots + brackets'
159158
);
160-
st.equal(
161-
qs.stringify(
162-
{ a: { b: ['c', 'd'] } },
163-
{ allowDots: true, encodeValuesOnly: true, arrayFormat: 'comma' }
164-
),
165-
'a.b=c%2Cd',
166-
'comma: stringifies with dots + comma'
167-
);
168159
st.equal(
169160
qs.stringify(
170161
{ a: { b: ['c', 'd'] } },
171162
{ allowDots: true, encodeValuesOnly: true, arrayFormat: 'comma' }
172163
),
173164
'a.b=c,d',
174-
'comma: stringifies with dots + comma (pending issue #378)',
175-
{ skip: true }
165+
'comma: stringifies with dots + comma'
176166
);
177167
st.equal(
178168
qs.stringify(
@@ -237,8 +227,8 @@ test('stringify()', function (t) {
237227
st.equal(
238228
qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'comma' }),
239229
'???',
240-
'brackets => brackets (pending issue #378)',
241-
{ skip: true }
230+
'brackets => brackets',
231+
{ skip: 'TODO: figure out what this should do' }
242232
);
243233
st.equal(
244234
qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true }),

0 commit comments

Comments
 (0)