Skip to content

Instantly share code, notes, and snippets.

@btoo
Created March 15, 2019 04:08
Show Gist options
  • Select an option

  • Save btoo/cb24c88ddf5eaf26b02453146c33d8f1 to your computer and use it in GitHub Desktop.

Select an option

Save btoo/cb24c88ddf5eaf26b02453146c33d8f1 to your computer and use it in GitHub Desktop.
add query parameters to a url
module.exports = (endpoint, query = {}) => {
const queryEntries = Object.entries(query || {}).filter(([key, value]) => value || typeof value === 'boolean');
return queryEntries.length
? `${endpoint}?${queryEntries
.map(pair => pair.map(encodeURIComponent).join('='))
.join('&')}`
: endpoint;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment