Skip to content

Commit

Permalink
[Fix] when (encode|decode)DotInKeys is false, behavior should be same…
Browse files Browse the repository at this point in the history
… as allowDots
  • Loading branch information
aks- committed Feb 2, 2024
1 parent 914a23c commit 867fc7b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
9 changes: 4 additions & 5 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ var parseObject = function (chain, val, options, valuesParsed) {
} else {
obj = options.plainObjects ? Object.create(null) : {};
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
var decodedRoot = options.allowDots && options.decodeDotInKeys ? cleanRoot.replace(/%2E/g, '.') : cleanRoot;
var decodedRoot = cleanRoot;
if (options.decodeDotInKeys) {
decodedRoot = cleanRoot.replace(/%2E/g, '.');
}
var index = parseInt(decodedRoot, 10);
if (!options.parseArrays && decodedRoot === '') {
obj = { 0: leaf };
Expand Down Expand Up @@ -218,10 +221,6 @@ var normalizeParseOptions = function normalizeParseOptions(opts) {
throw new TypeError('`decodeDotInKeys` option can only be `true` or `false`, when provided');
}

if (opts.allowDots === false && opts.decodeDotInKeys === true) {
throw new TypeError('Provided combination is not valid: allowDots=false & decodeDotInKeys=true');
}

if (opts.decoder !== null && typeof opts.decoder !== 'undefined' && typeof opts.decoder !== 'function') {
throw new TypeError('Decoder has to be a function.');
}
Expand Down
4 changes: 0 additions & 4 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,6 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
throw new TypeError('Encoder has to be a function.');
}

if (opts.allowDots === true && opts.encodeDotInKeys === false) {
throw new TypeError('Provided combination is not valid: allowDots=true & encodeDotInKeys=false');
}

var charset = opts.charset || defaults.charset;
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
Expand Down
26 changes: 7 additions & 19 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ test('parse()', function (t) {
{ 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },
'with allowDots false and decodeDotInKeys false'
);

st.deepEqual(
qs.parse('name.obj.first=John&name.obj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
{ name: { obj: { first: 'John', last: 'Doe' } } },
'with allowDots false and decodeDotInKeys false'
);
st.deepEqual(
qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
{ 'name%2Eobj': { first: 'John', last: 'Doe' } },
Expand All @@ -104,10 +108,10 @@ test('parse()', function (t) {
);
st.deepEqual(
qs.parse(
'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe',
{ allowDots: true, decodeDotInKeys: false }
),
{ 'name%2Eobj%2Esubobject': { 'first%2Egodly%2Ename': 'John', last: 'Doe' } },
{ name: { obj: { subobject: { first: { godly: { name: "John" } }, last: "Doe" } } } },
'with allowDots true and decodeDotInKeys false'
);
st.deepEqual(
Expand Down Expand Up @@ -158,22 +162,6 @@ test('parse()', function (t) {
st.end();
});

t.test('should throw when allowDots is false and decodeDotInKeys is true', function (st) {
st['throws'](
function () {
qs.parse(
'name%252Eobj.first=John&name%252Eobj.last=Doe',
{ allowDots: false, decodeDotInKeys: true }
);
}, {
name: 'TypeError',
message: 'Provided combination is not valid: allowDots=false & decodeDotInKeys=true'
}
);

st.end();
});

t.test('allows empty arrays in obj values', function (st) {
st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: true }), { foo: [], bar: 'baz' });
st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: false }), { foo: [''], bar: 'baz' });
Expand Down
32 changes: 16 additions & 16 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ test('stringify()', function (t) {
'name.obj%5Bfirst%5D=John&name.obj%5Blast%5D=Doe',
'with allowDots false and encodeDotInKeys false'
);
st.equal(
qs.stringify(
{ 'name.obj': { first: 'John', last: 'Doe' } },
{ allowDots: true, encodeDotInKeys: false }
),
'name.obj.first=John&name.obj.last=Doe',
'with allowDots true and encodeDotInKeys false'
);
st.equal(
qs.stringify(
{ 'name.obj': { first: 'John', last: 'Doe' } },
Expand All @@ -98,6 +106,14 @@ test('stringify()', function (t) {
'name.obj.subobject%5Bfirst.godly.name%5D=John&name.obj.subobject%5Blast%5D=Doe',
'with allowDots false and encodeDotInKeys false'
);
st.equal(
qs.stringify(
{ 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
{ allowDots: true, encodeDotInKeys: false }
),
'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe',
'with allowDots false and encodeDotInKeys false'
);
st.equal(
qs.stringify(
{ 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
Expand Down Expand Up @@ -170,22 +186,6 @@ test('stringify()', function (t) {
st.end();
});

t.test('should throw when allowDots is true and encodeDotInKeys is false', function (st) {
st['throws'](
function () {
qs.stringify(
{ 'name.obj': { first: 'John', last: 'Doe' } },
{ allowDots: true, encodeDotInKeys: false }
);
}, {
message: 'Provided combination is not valid: allowDots=true & encodeDotInKeys=false',
name: 'TypeError'
}
);

st.end();
});

t.test('adds query prefix', function (st) {
st.equal(qs.stringify({ a: 'b' }, { addQueryPrefix: true }), '?a=b');
st.end();
Expand Down

0 comments on commit 867fc7b

Please sign in to comment.