Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e0b0ddd

Browse files
sapicscodebytere
authored andcommittedJun 27, 2020
querystring: fix stringify for empty array
PR-URL: #33918 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 4a61013 commit e0b0ddd

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed
 

‎lib/querystring.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ function stringify(obj, sep, eq, options) {
196196
if (obj !== null && typeof obj === 'object') {
197197
const keys = ObjectKeys(obj);
198198
const len = keys.length;
199-
const flast = len - 1;
200199
let fields = '';
201200
for (let i = 0; i < len; ++i) {
202201
const k = keys[i];
@@ -207,20 +206,20 @@ function stringify(obj, sep, eq, options) {
207206
if (ArrayIsArray(v)) {
208207
const vlen = v.length;
209208
if (vlen === 0) continue;
210-
const vlast = vlen - 1;
209+
if (fields)
210+
fields += sep;
211211
for (let j = 0; j < vlen; ++j) {
212+
if (j)
213+
fields += sep;
212214
fields += ks;
213215
fields += convert(v[j], encode);
214-
if (j < vlast)
215-
fields += sep;
216216
}
217217
} else {
218+
if (fields)
219+
fields += sep;
218220
fields += ks;
219221
fields += convert(v, encode);
220222
}
221-
222-
if (i < flast)
223-
fields += sep;
224223
}
225224
return fields;
226225
}

‎test/parallel/test-querystring.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ const qsWeirdObjects = [
144144
[{ n: null }, 'n=', { 'n': '' }],
145145
[{ nan: NaN }, 'nan=', { 'nan': '' }],
146146
[{ inf: Infinity }, 'inf=', { 'inf': '' }],
147-
[{ a: [], b: [] }, '', {}]
147+
[{ a: [], b: [] }, '', {}],
148+
[{ a: 1, b: [] }, 'a=1', { 'a': '1' }]
148149
];
149150
// }}}
150151

0 commit comments

Comments
 (0)
Please sign in to comment.