Skip to content

Commit

Permalink
Merge pull request #19 from wpreul/master
Browse files Browse the repository at this point in the history
Don't overwrite null values
  • Loading branch information
Eran Hammer committed Aug 14, 2014
2 parents 8e8baa5 + dbfd805 commit 27ec1c0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 0 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,3 @@ module.exports = function (str, depth, delimiter) {

return Utils.compact(obj);
};


2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ exports.decode = function (str) {

exports.compact = function (obj) {

if (typeof obj !== 'object') {
if (typeof obj !== 'object' || obj === null) {
return obj;
}

Expand Down
44 changes: 42 additions & 2 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('#parse', function () {
done();
});

it('should compact sparse arrays', function (done) {
it('compacts sparse arrays', function (done) {

expect(Qs.parse('a[10]=1&a[2]=2')).to.deep.equal({ a: ['2', '1'] });
done();
Expand Down Expand Up @@ -253,9 +253,49 @@ describe('#parse', function () {
done();
});

it('should not use non-string objects as delimiters', function (done) {
it('does not use non-string objects as delimiters', function (done) {

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

it('parses an object', function (done) {

var input = {
"user[name]": {"pop[bob]": 3},
"user[email]": null
};

var expected = {
"user": {
"name": {"pop[bob]": 3},
"email": null
}
};

var result = Qs.parse(input);

expect(result).to.deep.equal(expected);
done();
});

it('parses an object and not child values', function (done) {

var input = {
"user[name]": {"pop[bob]": { "test": 3 }},
"user[email]": null
};

var expected = {
"user": {
"name": {"pop[bob]": { "test": 3 }},
"email": null
}
};

var result = Qs.parse(input);

expect(result).to.deep.equal(expected);
done();
});
});

0 comments on commit 27ec1c0

Please sign in to comment.