Skip to content

Commit 4a17709

Browse files
committed
[Fix] stringify: avoid encoding arrayformat comma when encodeValuesOnly = true (#424)
Fixes #410
1 parent c0e13e9 commit 4a17709

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

lib/stringify.js

+11-2
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]);
@@ -95,6 +96,14 @@ var stringify = function stringify(
9596
if (isNonNullishPrimitive(obj) || utils.isBuffer(obj)) {
9697
if (encoder) {
9798
var keyValue = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset, 'key', format);
99+
if (generateArrayPrefix === 'comma' && encodeValuesOnly) {
100+
var valuesArray = split.call(String(obj), ',');
101+
var valuesJoined = '';
102+
for (var i = 0; i < valuesArray.length; ++i) {
103+
valuesJoined += (i === 0 ? '' : ',') + formatter(encoder(valuesArray[i], defaults.encoder, charset, 'value', format));
104+
}
105+
return [formatter(keyValue) + '=' + valuesJoined];
106+
}
98107
return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder, charset, 'value', format))];
99108
}
100109
return [formatter(prefix) + '=' + formatter(String(obj))];
@@ -117,8 +126,8 @@ var stringify = function stringify(
117126
objKeys = sort ? keys.sort(sort) : keys;
118127
}
119128

120-
for (var i = 0; i < objKeys.length; ++i) {
121-
var key = objKeys[i];
129+
for (var j = 0; j < objKeys.length; ++j) {
130+
var key = objKeys[j];
122131
var value = typeof key === 'object' && key.value !== undefined ? key.value : obj[key];
123132

124133
if (skipNulls && value === null) {

test/stringify.js

+10-15
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 }),
@@ -800,7 +790,12 @@ test('stringify()', function (t) {
800790
st.equal(qs.stringify(withArray, { encode: false }), 'a[b][0][c]=d&a[b][0][e]=f', 'array, no arrayFormat');
801791
st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'bracket' }), 'a[b][0][c]=d&a[b][0][e]=f', 'array, bracket');
802792
st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'indices' }), 'a[b][0][c]=d&a[b][0][e]=f', 'array, indices');
803-
st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'comma' }), '???', 'array, comma (pending issue #378)', { skip: true });
793+
st.equal(
794+
qs.stringify(withArray, { encode: false, arrayFormat: 'comma' }),
795+
'???',
796+
'array, comma',
797+
{ skip: 'TODO: figure out what this should do' }
798+
);
804799

805800
st.end();
806801
});

0 commit comments

Comments
 (0)