Skip to content

Instantly share code, notes, and snippets.

@JamesTheAwesomeDude
Last active December 12, 2019 23:17
Show Gist options
  • Save JamesTheAwesomeDude/54628764c1f33cc1fbcf76e941ff3a8f to your computer and use it in GitHub Desktop.
Save JamesTheAwesomeDude/54628764c1f33cc1fbcf76e941ff3a8f to your computer and use it in GitHub Desktop.
One-statement parsing location.parameters
decodeURIParam=p=>//<223 bytes w/o whitespace and comments
Object.assign(//Create a new object,
...//the union of the following objects:
p//location.parameters
.match(/^\??(.*)$/)//has a leading '?'
[1]//(which we don't want)
.split('&')//split the rest into '&'-delimited chunks,
.filter(S=>S)//remove null chunks (from duplicate or trailing ampersands)
.map(S=>S//take each chunk
.match(/^([^=]*)=?(.*)$/))//and split it via key=value
.map(([S,k,v])=>(//take each of those key,value pairs
{//put them into objects
[decodeURIComponent(k.replace(/\+/g,' '))]//(of course, only after
:decodeURIComponent(v.replace(/\+/g,' '))})))// decoding them)
;//call via decodeURIParam(location.parameters)
decodeURIParam=p=>Object.assign(...p.match(/^\??(.*)$/)[1].split('&').filter(S=>S).map(S=>S.match(/^([^=]*)=?(.*)$/)).map(([S,k,v])=>({[decodeURIComponent(k.replace(/\+/g,' '))]:decodeURIComponent(v.replace(/\+/g,' '))})));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment