Skip to content

Commit

Permalink
allow merging a string into an object
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Oct 28, 2014
1 parent a1c7cca commit fccc8e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ exports.merge = function (target, source) {
}

if (typeof source !== 'object') {
target.push(source);
if (Array.isArray(target)) {
target.push(source);
}
else {
target[source] = true;
}

return target;
}

Expand Down
8 changes: 7 additions & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ describe('parse()', function () {
done();
});

it('can add keys to objects', function (done) {

expect(Qs.parse('a[b]=c&a=d')).to.deep.equal({ a: { b: 'c', d: true } });
done();
});

it('correctly prunes undefined values when converting an array to an object', function (done) {

expect(Qs.parse('a[2]=b&a[99999999]=c')).to.deep.equal({ a: { '2': 'b', '99999999': 'c' } });
Expand Down Expand Up @@ -378,7 +384,7 @@ describe('parse()', function () {

var a = Object.create(null);
a.b = 'c';

expect(Qs.parse(a)).to.deep.equal({ b: 'c' });
expect(Qs.parse({ a: a })).to.deep.equal({ a: { b: 'c' } });
done();
Expand Down

0 comments on commit fccc8e0

Please sign in to comment.