Skip to content

Instantly share code, notes, and snippets.

@crshmk
Last active June 12, 2020 23:20
Show Gist options
  • Select an option

  • Save crshmk/5ce55d60c646d4a60c0dd59fbe67d5e1 to your computer and use it in GitHub Desktop.

Select an option

Save crshmk/5ce55d60c646d4a60c0dd59fbe67d5e1 to your computer and use it in GitHub Desktop.
ramda pipelines to get or set query params
// takes the window
// returns params as an object
export const getQueryParams = pipe(
path(['location', 'search']),
tail,
split('&'),
map(split('=')),
fromPairs,
map(decodeURI)
);
export const getUrlParamsAsPairs = pipe(
path(['location', 'search']),
tail,
split('&'),
map(split('=')),
map(([k, v]) => [k, decodeURI(v)])
);
export const makeUrlQueryString = pipe(
mapObjIndexed(encodeURIComponent),
toPairs,
map(join('=')),
join('&'),
concat('?')
)
@crshmk
Copy link
Copy Markdown
Author

crshmk commented Jun 12, 2020

www.site.com?one=first%20thing&two=second

getQueryParams(window)
{
  one: 'first thing',
  two: 'second'
}
getUrlParamsAsPairs(window)
[
  ['one', 'first thing],
  ['two', 'second']
]

@crshmk
Copy link
Copy Markdown
Author

crshmk commented Jun 12, 2020

let params = {
  one: 'first thing',
  two: 'second'
}

let queryString = makeUrlQueryString(params)
"?one=first%20thing&two=second"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment