Skip to content

Instantly share code, notes, and snippets.

@pirey
Last active November 11, 2018 05:08
Show Gist options
  • Save pirey/0928925174e51853dfd1418a54f15486 to your computer and use it in GitHub Desktop.
Save pirey/0928925174e51853dfd1418a54f15486 to your computer and use it in GitHub Desktop.
/*
* convert ojbect into query params format
* used for request with content-type application/x-www-form-urlencoded
*
* e.g
* { param: "someVal", param2: "otherVal" }
*
* is converted into
*
* param1=someVal&param2=otherVal
*/
function buildQuery (params) {
const notNull = x => x !== null
const esc = encodeURIComponent
const queryString = Object.keys(params).map((k) =>
params[k] !== null || params[k] !== undefined ? esc(k) + '=' + esc(params[k]) : null
).filter(notNull)
return queryString.length >= 1
? queryString.join('&')
: ''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment