Skip to content

Commit

Permalink
add proto restoration. Closes #61
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed May 3, 2013
1 parent 0311084 commit 3d4e0e0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
22 changes: 20 additions & 2 deletions index.js
Expand Up @@ -142,7 +142,7 @@ function merge(parent, key, val){
}

/**
* Compact sparse arrays
* Compact sparse arrays.
*/

function compact(obj) {
Expand All @@ -161,13 +161,31 @@ function compact(obj) {
return obj;
}

/**
* Restore Object.prototype.
* see pull-request #58
*/

function restoreProto(obj) {
if (!Object.create) return obj;
if (isArray(obj)) return obj;
if (obj && 'object' != typeof obj) return obj;

for (var key in obj) {
obj[key] = restoreProto(obj[key]);
}

obj.__proto__ = Object.prototype;
return obj;
}

/**
* Parse the given obj.
*/

function parseObject(obj){
var ret = { base: {} };

forEach(objectKeys(obj), function(name){
merge(ret, name, obj[name]);
});
Expand All @@ -194,7 +212,7 @@ function parseString(str){
return merge(ret, decode(key), decode(val));
}, { base: createObject() }).base;

return compact(ret);
return restoreProto(compact(ret));
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/parse.js
Expand Up @@ -9,6 +9,7 @@ if (require.register) {
describe('qs.parse()', function(){
it('should support the basics', function(){
qs.parse('foo=bar').hasOwnProperty('foo');
qs.parse('foo[bar]=baz').foo.hasOwnProperty('foo');

expect(qs.parse('0=foo')).to.eql({ '0': 'foo' });

Expand Down

0 comments on commit 3d4e0e0

Please sign in to comment.