Skip to content

Commit

Permalink
[New] parse: call decoder when parsing objects
Browse files Browse the repository at this point in the history
Fixes ljharb#284.
  • Loading branch information
nbgraham committed Jul 28, 2023
1 parent 410bdd3 commit bf38994
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,36 @@ 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);

if (str === '' || str === null || typeof str === 'undefined') {
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
Expand Down
71 changes: 71 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down

0 comments on commit bf38994

Please sign in to comment.