Created
April 9, 2009 16:02
-
-
Save furf/92546 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* parseQueryParameters & parseFragmentIdentifiers */ | |
var setValue = function(obj, prop, val) { | |
if (!(prop in obj)) { | |
obj[prop] = val; | |
} else { | |
if (!(obj[prop] instanceof Array)) { | |
obj[prop] = [obj[prop]]; | |
} | |
obj[prop].push(val); | |
} | |
}; | |
// Note: This setDeepValue function has been modified from the original | |
// function for performance enhancement available in this context | |
var setDeepValue = function (obj, props, val) { | |
for (var i = 0, n = props.length - 1; i < n; ++i) { | |
obj = obj[props[i]] = obj[props[i]] || {}; | |
} | |
setValue(obj, props[i], val); | |
}; | |
var parseAmpersandDelimitedKeyValuePairs = function(str /*, typeVals */) { | |
if (!str) { | |
return {}; | |
} | |
var params = {}, | |
pairs = str.split('&'), | |
typeVals = (arguments[1] !== false), | |
re = /^([^\[]+)(?:[\[](.*)[\]])?$/, | |
pair, | |
key, | |
val, | |
num, | |
matches; | |
for (var i = 0, n = pairs.length; i < n; ++i) { | |
pair = pairs[i].split('='); | |
key = pair[0]; | |
val = pair[1]; | |
if (typeof val === 'string') { | |
val = unescape(val); | |
// Type non-String data types | |
if (typeVals === true && val !== '') { | |
// Number | |
if (!isNaN(val)) { | |
val = Number(val); | |
// Boolean true | |
} else if (val.toLowerCase() === 'true') { | |
val = true; | |
// Boolean false | |
} else if (val.toLowerCase() === 'false') { | |
val = false; | |
// null | |
} else if (val.toLowerCase() === 'null') { | |
val = null; | |
// undefined (explicit) | |
} else if (val.toLowerCase() === 'undefined') { | |
val = undefined; | |
} | |
} | |
// undefined (implicit) | |
} else { | |
val = undefined; | |
} | |
// Look for complex data types (arrays, objects) | |
key.replace(re, function(match, key, deepProp) { | |
// Complex | |
if (typeof deepProp !== 'undefined' && deepProp !== '') { | |
setDeepValue(params, [key].concat(deepProp.split(/\]\[/)), val); | |
// Primitive | |
} else { | |
setValue(params, key, val); | |
} | |
}); | |
} | |
return params; | |
}; | |
var parseQueryParameters = function(/* url, typeVals */) { | |
var url = arguments[0] || window.location.search, | |
str = url.substring(url.indexOf('?') + 1); | |
return parseAmpersandDelimitedKeyValuePairs(str, arguments[1]); | |
}; | |
var parseFragmentIdentifiers = function(/* url, typeVals */) { | |
var url = arguments[0] || window.location.hash, | |
str = url.substring(url.indexOf('#') + 1); | |
return parseAmpersandDelimitedKeyValuePairs(str, arguments[1]); | |
}; | |
var params = parseQueryParameters(); | |
console.dir(params); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment