Skip to content

Instantly share code, notes, and snippets.

@fernandocamargo
Created December 4, 2020 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandocamargo/221115c43a415d7e87a7b2dcfebe9c6d to your computer and use it in GitHub Desktop.
Save fernandocamargo/221115c43a415d7e87a7b2dcfebe9c6d to your computer and use it in GitHub Desktop.
a e s t h e t i c
const URL = 'domain.com/x/y/z'
const PARAMS = {
firstname: 'firstName',
lastname: 'lastName',
phone: 'phone',
company: 'company',
}
const concat = ({
join = ({ length }) => (length === 1 ? '?' : '&'),
sanitize = window.encodeURIComponent,
assignment = '=',
param,
url,
value,
}) => url.concat(join(url), param, assignment, sanitize(value))
const check = (url, [key, value], { param = PARAMS[key] }) =>
param ? concat({ url, param, value }) : url
const urlify = props =>
Object.entries(props)
.reduce(check, [URL])
.join('')
urlify({
firstname: 'Nelson',
lastname: 'Amorim',
phone: '+4915777133734',
company: 'Google',
who: 'cares',
}) // domain.com/x/y/z?firstName=Nelson&lastName=Amorim&phone=%2B4915777133734&company=Google
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment