Sitelet https://github.com/ljharb/qs/commit/9220d18fd0a3c73d76c586729565ec77bcd1d7b4
Skip to content

Commit 9220d18

Browse files
daggerjamesljharb
authored andcommitted
minor: refactor for clear code structure
use another approach to achieve comma with more robust tests covered. minor: update test cases fix impacts with obj potential properties
1 parent 83d1e9a commit 9220d18

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎lib/stringify.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ var stringify = function stringify(
106106
}
107107

108108
var objKeys;
109-
if (isArray(filter)) {
109+
if (generateArrayPrefix === 'comma' && isArray(obj)) {
110+
// we need to join elements in
111+
objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : undefined }];
112+
} else if (isArray(filter)) {
110113
objKeys = filter;
111114
} else {
112115
var keys = Object.keys(obj);
@@ -115,7 +118,7 @@ var stringify = function stringify(
115118

116119
for (var i = 0; i < objKeys.length; ++i) {
117120
var key = objKeys[i];
118-
var value = obj[key];
121+
var value = typeof key === 'object' && key.value !== undefined ? key.value : obj[key];
119122

120123
if (skipNulls && value === null) {
121124
continue;

‎test/stringify.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,27 @@ test('stringify()', function (t) {
336336
st.end();
337337
});
338338

339+
t.test('stringifies an empty array in different arrayFormat', function (st) {
340+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false }), 'b[0]=&c=c');
341+
// arrayFormat default
342+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices' }), 'b[0]=&c=c');
343+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets' }), 'b[]=&c=c');
344+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat' }), 'b=&c=c');
345+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma' }), 'b=&c=c');
346+
// with strictNullHandling
347+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', strictNullHandling: true }), 'b[0]&c=c');
348+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', strictNullHandling: true }), 'b[]&c=c');
349+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', strictNullHandling: true }), 'b&c=c');
350+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', strictNullHandling: true }), 'b&c=c');
351+
// with skipNulls
352+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', skipNulls: true }), 'c=c');
353+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', skipNulls: true }), 'c=c');
354+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', skipNulls: true }), 'c=c');
355+
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', skipNulls: true }), 'c=c');
356+
357+
st.end();
358+
});
359+
339360
t.test('stringifies a null object', { skip: !Object.create }, function (st) {
340361
var obj = Object.create(null);
341362
obj.a = 'b';

0 commit comments

Comments
 (0)