From bf38994b5a8bf668dd763b6fe52fe9b0d1db4ca9 Mon Sep 17 00:00:00 2001 From: Nick Graham Date: Fri, 28 Jul 2023 10:16:06 -0500 Subject: [PATCH 1/2] [New] `parse`: call decoder when parsing objects Fixes #284. --- lib/parse.js | 24 ++++++++++++++++- test/parse.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/lib/parse.js b/lib/parse.js index ae194fb5..cf216d44 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -237,6 +237,28 @@ var normalizeParseOptions = function normalizeParseOptions(opts) { }; }; +var decodeObject = function (obj, opts) { + if (opts.decoder === defaults.decoder) { + return obj; + } + if (typeof obj !== 'object') { + return obj; + } + var decodedObj = {}; + var keys = Object.keys(obj); + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; + var val = obj[key]; + var decodedKey = opts.decoder(key, defaults.decoder, opts.charset, 'key'); + var decodedVal = opts.decoder(val, defaults.decoder, opts.charset, 'value'); + decodedVal = utils.maybeMap(decodedVal, function (mapped) { + return decodeObject(mapped, opts); + }); + decodedObj[decodedKey] = decodedVal; + } + return decodedObj; +}; + module.exports = function (str, opts) { var options = normalizeParseOptions(opts); @@ -244,7 +266,7 @@ module.exports = function (str, opts) { return options.plainObjects ? Object.create(null) : {}; } - var tempObj = typeof str === 'string' ? parseValues(str, options) : str; + var tempObj = typeof str === 'string' ? parseValues(str, options) : decodeObject(str, options); var obj = options.plainObjects ? Object.create(null) : {}; // Iterate over the keys and setup the new object diff --git a/test/parse.js b/test/parse.js index d4a213a2..57817448 100644 --- a/test/parse.js +++ b/test/parse.js @@ -767,6 +767,77 @@ test('parse()', function (t) { st.end(); }); + t.test('calls decoder when parsing objects', function (st) { + var decoded = []; + st.deepEqual(qs.parse({ + one: 1, + two: 'two', + three: true + }, { + decoder: function (str, _, __, type) { + decoded.push({ val: str, type: type }); + if (type === 'key') { + return str + '$'; + } + return str; + } + }), { + one$: 1, + two$: 'two', + three$: true + }); + st.deepEqual(decoded, [ + { val: 'one', type: 'key' }, + { val: 1, type: 'value' }, + { val: 'two', type: 'key' }, + { val: 'two', type: 'value' }, + { val: 'three', type: 'key' }, + { val: true, type: 'value' } + ]); + st.end(); + }); + + t.test('can parse object with custom encoding', function (st) { + var keywords = { + 'true': true + }; + st.deepEqual(qs.parse({ + maintYear: '2018', + maintMonth: '5', + fields: [ + 'id', + 'regname', + 'price' + ], + notRenewed: 'true' + }, { + decoder: function (str, _, __, type) { + if (str in keywords) { + return keywords[str]; + } + if (type === 'value') { + var parsed = parseFloat(str); + if (isNaN(parsed)) { + return str; + } + return parsed; + + } + return str; + } + }), { + maintYear: 2018, + maintMonth: 5, + fields: [ + 'id', + 'regname', + 'price' + ], + notRenewed: true + }); + st.end(); + }); + t.test('receives the default decoder as a second argument', function (st) { st.plan(1); qs.parse('a', { From eb72cf48e5e79c7ed32b5a2209cb800604378d81 Mon Sep 17 00:00:00 2001 From: Nick Graham Date: Tue, 15 Aug 2023 09:17:30 -0500 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Jordan Harband --- lib/parse.js | 5 +---- test/parse.js | 9 ++++++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index cf216d44..9be1b2f6 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -238,13 +238,10 @@ var normalizeParseOptions = function normalizeParseOptions(opts) { }; var decodeObject = function (obj, opts) { - if (opts.decoder === defaults.decoder) { - return obj; - } if (typeof obj !== 'object') { return obj; } - var decodedObj = {}; + var decodedObj = opts.plainObjects ? Object.create(null) : {}; var keys = Object.keys(obj); for (var i = 0; i < keys.length; ++i) { var key = keys[i]; diff --git a/test/parse.js b/test/parse.js index 57817448..3c6094cf 100644 --- a/test/parse.js +++ b/test/parse.js @@ -801,7 +801,8 @@ test('parse()', function (t) { var keywords = { 'true': true }; - st.deepEqual(qs.parse({ + st.deepEqual( + qs.parse({ maintYear: '2018', maintMonth: '5', fields: [ @@ -810,7 +811,8 @@ test('parse()', function (t) { 'price' ], notRenewed: 'true' - }, { + }, + { decoder: function (str, _, __, type) { if (str in keywords) { return keywords[str]; @@ -825,7 +827,8 @@ test('parse()', function (t) { } return str; } - }), { + }), + { maintYear: 2018, maintMonth: 5, fields: [