Skip to content

Commit

Permalink
Merge pull request tj#31 from aheckmann/objects
Browse files Browse the repository at this point in the history
ensure objects are created when appropo
  • Loading branch information
tj committed Feb 8, 2012
2 parents 1f2bac6 + eddf9f7 commit 18f06b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/querystring.js
Expand Up @@ -21,7 +21,7 @@ var toString = Object.prototype.toString;
* Cache non-integer test regexp.
*/

var notint = /^[^0-9]+$/;
var isint = /^[0-9]+$/;

function promote(parent, key) {
if (parent[key].length == 0) return parent[key] = {};
Expand Down Expand Up @@ -58,11 +58,11 @@ function parse(parts, parent, key, val) {
// prop
} else if (~part.indexOf(']')) {
part = part.substr(0, part.length - 1);
if(notint.test(part) && Array.isArray(obj)) obj = promote(parent, key);
if (!isint.test(part) && Array.isArray(obj)) obj = promote(parent, key);
parse(parts, obj, part, val);
// key
} else {
if(notint.test(part) && Array.isArray(obj)) obj = promote(parent, key);
if (!isint.test(part) && Array.isArray(obj)) obj = promote(parent, key);
parse(parts, obj, part, val);
}
}
Expand All @@ -80,7 +80,7 @@ function merge(parent, key, val){
parse(parts, parent, 'base', val);
// optimize
} else {
if (notint.test(key) && Array.isArray(parent.base)) {
if (!isint.test(key) && Array.isArray(parent.base)) {
var t = {};
for (var k in parent.base) t[k] = parent.base[k];
parent.base = t;
Expand Down
6 changes: 5 additions & 1 deletion test/parse.js
Expand Up @@ -91,8 +91,12 @@ module.exports = {

qs.parse('user[name][first]=tj&user[name][first]=TJ')
.should.eql({ user: { name: { first: ['tj', 'TJ'] }}});

var o = qs.parse('existing[fcbaebfecc][name][last]=tj')
o.should.eql({ existing: { 'fcbaebfecc': { name: { last: 'tj' }}}})
Array.isArray(o.existing).should.be.false;
},

'test right-hand brackets': function(){
qs.parse('pets=["tobi"]')
.should.eql({ pets: '["tobi"]' });
Expand Down

0 comments on commit 18f06b0

Please sign in to comment.