Created
February 13, 2019 22:38
-
-
Save ragmha/75e82a0dc30921cbfc0106684ffa4bce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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