Skip to content

Instantly share code, notes, and snippets.

@deguchi
Created March 20, 2018 06:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deguchi/8d7ca27fa740d202e0b2c8697b44dce7 to your computer and use it in GitHub Desktop.
Save deguchi/8d7ca27fa740d202e0b2c8697b44dce7 to your computer and use it in GitHub Desktop.
const buildQueryString = (params) => {
let parts = [];
let add = (key, value) => {
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
}
for (let key in params) {
let param = params[key];
if (Array.isArray(param)) {
param.map((value) => {
add(key, value);
});
}
else {
add(key, param);
}
}
return '?' + parts.join('&').replace(/%20/g, '+');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment