Skip to content

Instantly share code, notes, and snippets.

@Shuumatsu
Created September 21, 2017 07:57
Show Gist options
  • Save Shuumatsu/73434a1b607d78741150f88fe860fb14 to your computer and use it in GitHub Desktop.
Save Shuumatsu/73434a1b607d78741150f88fe860fb14 to your computer and use it in GitHub Desktop.
const ensureStartsWith = (start, str) => str.startsWith(start) ? str : start + str
const setSearchParam = searchStr =>
(key, value) => {
if (!value) return searchStr
const searchParams = new URLSearchParams(searchStr)
searchParams.set(key, value)
const newSearchStr =
ensureStartsWith('?', searchParams.toString())
return newSearchStr
}
export default setSearchParam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment