Skip to content

Instantly share code, notes, and snippets.

@rolfen
Last active February 23, 2016 17:46
Show Gist options
  • Save rolfen/16e1b6501f06f73b8330 to your computer and use it in GitHub Desktop.
Save rolfen/16e1b6501f06f73b8330 to your computer and use it in GitHub Desktop.
/**
* Parses a GET-style query string into a key/value object
* example:
* decodeQueryString("?sdf=ABC&xyz=jj%C3%BC");
* Object {sdf: "ABC", xyz: "jjü"}
*
* @param {String} s The query string
*/
function decodeQueryString(s) {
var decoded = {};
var parts = s.replace(/^[?/]*/,'').split('&');
parts.forEach(function(part){
var pair = part.split('=');
var key = pair[0];
var value = pair[1];
decoded[key] = decodeURIComponent(value);
});
return(decoded);
}
function safeStringify(o) {
// JSON stringify that does not break on circular references
// http://stackoverflow.com/a/11616993/370786
var cache = [];
var ret = JSON.stringify(o, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
}, "\t");
return ret;
// cache = null; // Enable garbage collection
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment