From bf38994b5a8bf668dd763b6fe52fe9b0d1db4ca9 Mon Sep 17 00:00:00 2001 From: Nick Graham Date: Fri, 28 Jul 2023 10:16:06 -0500 Subject: [PATCH] [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', {