Skip to content

Instantly share code, notes, and snippets.

@foriacus
Last active August 19, 2021 18:44
Show Gist options
  • Select an option

  • Save foriacus/7745513 to your computer and use it in GitHub Desktop.

Select an option

Save foriacus/7745513 to your computer and use it in GitHub Desktop.
querystring
var trim = require('trim');
exports.parse = function(str){
if ('string' != typeof str) return {};
str = trim(str);
if ('' == str) return {};
var obj = {};
var pairs = str.split('&');
for (var i = 0; i < pairs.length; i++) {
var parts = pairs[i].split('=');
obj[parts[0]] = null == parts[1]
? ''
: decodeURIComponent(parts[1]);
}
return obj;
};
exports.stringify = function(obj){
if (!obj) return '';
var pairs = [];
for (var key in obj) {
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
}
return pairs.join('&');
};
var querystring = require('querystring');
querystring.parse(window.location.search.slice(1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment