From 04eac8db77b8b9b11a48c7cd32e21d3587add624 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 19 Apr 2021 08:35:28 -0700 Subject: [PATCH] [Fix] `stringify`: avoid encoding arrayformat comma when `encodeValuesOnly = true` (#424) Fixes #410 --- lib/stringify.js | 18 ++++++++++++++---- test/stringify.js | 18 ++++-------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/stringify.js b/lib/stringify.js index 415a2411..3c76e5fb 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -18,6 +18,7 @@ var arrayPrefixGenerators = { }; var isArray = Array.isArray; +var split = String.prototype.split; var push = Array.prototype.push; var pushToArray = function (arr, valueOrArray) { push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]); @@ -81,6 +82,14 @@ var stringify = function stringify( if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || utils.isBuffer(obj)) { if (encoder) { var keyValue = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset); + if (generateArrayPrefix === 'comma' && encodeValuesOnly) { + var valuesArray = split.call(String(obj), ','); + var valuesJoined = ''; + for (var i = 0; i < valuesArray.length; ++i) { + valuesJoined += (i === 0 ? '' : ',') + formatter(encoder(valuesArray[i], defaults.encoder, charset)); + } + return [formatter(keyValue) + '=' + valuesJoined]; + } return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder, charset))]; } return [formatter(prefix) + '=' + formatter(String(obj))]; @@ -100,8 +109,9 @@ var stringify = function stringify( objKeys = sort ? keys.sort(sort) : keys; } - for (var i = 0; i < objKeys.length; ++i) { - var key = objKeys[i]; + for (var j = 0; j < objKeys.length; ++j) { + var key = objKeys[j]; + var value = typeof key === 'object' && key.value !== undefined ? key.value : obj[key]; if (skipNulls && obj[key] === null) { continue; @@ -109,7 +119,7 @@ var stringify = function stringify( if (isArray(obj)) { pushToArray(values, stringify( - obj[key], + value, typeof generateArrayPrefix === 'function' ? generateArrayPrefix(prefix, key) : prefix, generateArrayPrefix, strictNullHandling, @@ -125,7 +135,7 @@ var stringify = function stringify( )); } else { pushToArray(values, stringify( - obj[key], + value, prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, diff --git a/test/stringify.js b/test/stringify.js index 47e7addb..b69bc8f1 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -99,8 +99,7 @@ test('stringify()', function (t) { t.test('stringifies a nested array value', function (st) { st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[b][0]=c&a[b][1]=d'); st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[b][]=c&a[b][]=d'); - st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[b]=c%2Cd'); - st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[b]=c,d', '(pending issue #378)', { skip: true }); + st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[b]=c,d'); st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true }), 'a[b][0]=c&a[b][1]=d'); st.end(); }); @@ -122,22 +121,13 @@ test('stringify()', function (t) { 'a.b[]=c&a.b[]=d', 'brackets: stringifies with dots + brackets' ); - st.equal( - qs.stringify( - { a: { b: ['c', 'd'] } }, - { allowDots: true, encodeValuesOnly: true, arrayFormat: 'comma' } - ), - 'a.b=c%2Cd', - 'comma: stringifies with dots + comma' - ); st.equal( qs.stringify( { a: { b: ['c', 'd'] } }, { allowDots: true, encodeValuesOnly: true, arrayFormat: 'comma' } ), 'a.b=c,d', - 'comma: stringifies with dots + comma (pending issue #378)', - { skip: true } + 'comma: stringifies with dots + comma' ); st.equal( qs.stringify( @@ -202,8 +192,8 @@ test('stringify()', function (t) { st.equal( qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), '???', - 'brackets => brackets (pending issue #378)', - { skip: true } + 'brackets => brackets', + { skip: 'TODO: figure out what this should do' } ); st.equal( qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true }),