Created
December 4, 2020 14:31
-
-
Save fernandocamargo/221115c43a415d7e87a7b2dcfebe9c6d to your computer and use it in GitHub Desktop.
a e s t h e t i c
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 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