Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created April 9, 2009 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/92356 to your computer and use it in GitHub Desktop.
Save isaacs/92356 to your computer and use it in GitHub Desktop.
var parseQueryString = (function (overlap) {
return function (qs) {
var kv = qs.split('&');
var obj = {};
for (var i = 0, l = kv.length; i < l; i ++) {
kv[i] = kv[i].split('=');
var key = decodeURIComponent(kv[i].shift());
var val = decodeURIComponent(kv[i].join('='));
var keyparts = key.match(/^([^\[]+)(.*)$/);
if (!keyparts) continue;
keyparts.shift();
if (keyparts.length === 1) {
// easy. simple key/value pair
obj[keyparts[0]] = val;
} else {
// more complex.
// array or object.
// create the "tmpobj", and then merge it in.
var subkeys = keyparts[1].replace(/^\[/, '').replace(/\]$/, '').split('][');
var tmpobj, tmpkey;
while (undefined !== (tmpkey = subkeys.pop())) {
if (tmpkey === '') {
// array
tmpobj = [val];
} else {
tmpobj = {};
tmpobj[tmpkey] = val;
}
val = tmpobj;
}
if (!obj.hasOwnProperty(keyparts[0])) {
obj[keyparts[0]] = val;
} else {
tmpobj = {};
tmpobj[keyparts[0]] = val;
obj = overlap(obj, tmpobj);
}
}
}
return obj;
};
})(function (into, from) {
if (Y.Lang.isObject(into) && Y.Lang.isObject(from)) {
for (var i in from) if (from.hasOwnProperty(i) && i.slice(-3) !== '___') {
into[i] = arguments.callee(into[i], from[i]);
}
return into;
} else if (Y.Lang.isArray(into) && Y.Lang.isArray(from)) {
return into.concat(from);
} else {
// type mismatch, just overwrite.
return from;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment