Skip to content

Commit

Permalink
[Fix] follow allowPrototypes option during merge
Browse files Browse the repository at this point in the history
Fixes #200.
  • Loading branch information
dead-horse authored and ljharb committed Mar 6, 2017
1 parent 3b60616 commit 62153c2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/utils.js
Expand Up @@ -29,7 +29,9 @@ exports.merge = function (target, source, options) {
if (Array.isArray(target)) {
target.push(source);
} else if (typeof target === 'object') {
target[source] = true;
if (options.plainObjects || options.allowPrototypes || !has.call(Object.prototype, source)) {
target[source] = true;
}
} else {
return [target, source];
}
Expand Down
42 changes: 39 additions & 3 deletions test/parse.js
Expand Up @@ -142,8 +142,6 @@ test('parse()', function (t) {
st.end();
});

t.deepEqual(qs.parse('a[b]=c&a=d'), { a: { b: 'c', d: true } }, 'can add keys to objects');

t.test('correctly prunes undefined values when converting an array to an object', function (st) {
st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { '2': 'b', '99999999': 'c' } });
st.end();
Expand Down Expand Up @@ -404,7 +402,45 @@ test('parse()', function (t) {
st.end();
});

t.test('can return plain objects', function (st) {
t.test('params starting with a starting bracket', function (st) {
st.deepEqual(qs.parse('[=toString'), {});
st.end();
});

t.test('params starting with a starting bracket', function (st) {
st.deepEqual(qs.parse('[=toString'), {});
st.end();
});

t.test('add keys to objects', function (st) {
st.deepEqual(
qs.parse('a[b]=c&a=d'),
{ a: { b: 'c', d: true } },
'can add keys to objects'
);

st.deepEqual(
qs.parse('a[b]=c&a=toString'),
{ a: { b: 'c' } },
'can not overwrite prototype'
);

st.deepEqual(
qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
{ a: { b: 'c', toString: true } },
'can overwrite prototype with allowPrototypes true'
);

st.deepEqual(
qs.parse('a[b]=c&a=toString', { plainObjects: true }),
{ a: { b: 'c', toString: true } },
'can overwrite prototype with plainObjects true'
);

st.end();
});

t.test('can return null objects', { skip: !Object.create }, function (st) {
var expected = Object.create(null);
expected.a = Object.create(null);
expected.a.b = 'c';
Expand Down

0 comments on commit 62153c2

Please sign in to comment.