Skip to content

Instantly share code, notes, and snippets.

@ragmha
Created February 13, 2019 22:38
Show Gist options
  • Save ragmha/75e82a0dc30921cbfc0106684ffa4bce to your computer and use it in GitHub Desktop.
Save ragmha/75e82a0dc30921cbfc0106684ffa4bce to your computer and use it in GitHub Desktop.
const variables = {
keywords: 'Hello',
limit: 1,
page: 5
}
const qs = Object.keys(variables).reduce(
(acc, key) => {
// Special condition for pagination
if (variables.page > 1 && variables.keywords !== '' || variables.keywords !== undefined || variables[key] === null ) {
return '?' + 'keywords' + '=' + encodeURIComponent(variables.keywords) + '&' + key + '=' + encodeURIComponent(String(1))
}
if (variables[key] === null || variables[key] === undefined || variables[key] === '') {
return acc
}
if (acc === '') {
return '?' + key + '=' + encodeURIComponent(String(variables[key]))
}
return(
acc + '&' + key + '=' + encodeURIComponent(String(variables[key]))
)
}, '')
qs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment