From deada9450273f397a011f402b91a3a4216a62f0b Mon Sep 17 00:00:00 2001 From: Yipeng Zhao Date: Sun, 9 Jun 2019 22:52:01 +0800 Subject: [PATCH] [Fix] `stringify`: fix arrayFormat comma with empty array/objects --- lib/stringify.js | 9 ++++++--- test/stringify.js | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/stringify.js b/lib/stringify.js index 5d22260c..da5380a8 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -80,7 +80,7 @@ var stringify = function stringify( return serializeDate(value); } return value; - }).join(','); + }); } if (obj === null) { @@ -106,7 +106,10 @@ var stringify = function stringify( } var objKeys; - if (isArray(filter)) { + if (generateArrayPrefix === 'comma' && isArray(obj)) { + // we need to join elements in + objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : undefined }]; + } else if (isArray(filter)) { objKeys = filter; } else { var keys = Object.keys(obj); @@ -115,7 +118,7 @@ var stringify = function stringify( for (var i = 0; i < objKeys.length; ++i) { var key = objKeys[i]; - var value = obj[key]; + var value = typeof key === 'object' && key.value !== undefined ? key.value : obj[key]; if (skipNulls && value === null) { continue; diff --git a/test/stringify.js b/test/stringify.js index 49692184..cbd90b2c 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -336,6 +336,27 @@ test('stringify()', function (t) { st.end(); }); + t.test('stringifies an empty array in different arrayFormat', function (st) { + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false }), 'b[0]=&c=c'); + // arrayFormat default + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices' }), 'b[0]=&c=c'); + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets' }), 'b[]=&c=c'); + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat' }), 'b=&c=c'); + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma' }), 'b=&c=c'); + // with strictNullHandling + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', strictNullHandling: true }), 'b[0]&c=c'); + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', strictNullHandling: true }), 'b[]&c=c'); + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', strictNullHandling: true }), 'b&c=c'); + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', strictNullHandling: true }), 'b&c=c'); + // with skipNulls + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', skipNulls: true }), 'c=c'); + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', skipNulls: true }), 'c=c'); + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', skipNulls: true }), 'c=c'); + st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', skipNulls: true }), 'c=c'); + + st.end(); + }); + t.test('stringifies a null object', { skip: !Object.create }, function (st) { var obj = Object.create(null); obj.a = 'b';