Permalink
Browse files

add proto restoration. Closes #61

  • Loading branch information...
1 parent 0311084 commit 3d4e0e0832aa5073d6395defeb3e82aabc0de629 @tj committed May 3, 2013
Showing with 21 additions and 2 deletions.
  1. +20 −2 index.js
  2. +1 −0 test/parse.js
View
@@ -142,7 +142,7 @@ function merge(parent, key, val){
}
/**
- * Compact sparse arrays
+ * Compact sparse arrays.
*/
function compact(obj) {
@@ -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]);
});
@@ -194,7 +212,7 @@ function parseString(str){
return merge(ret, decode(key), decode(val));
}, { base: createObject() }).base;
- return compact(ret);
+ return restoreProto(compact(ret));
}
/**
View
@@ -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' });

0 comments on commit 3d4e0e0

Please sign in to comment.