Skip to content

Instantly share code, notes, and snippets.

@ZauberNerd
Created January 24, 2016 21:26
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 ZauberNerd/c50a05f85a5f6ea09cbb to your computer and use it in GitHub Desktop.
Save ZauberNerd/c50a05f85a5f6ea09cbb to your computer and use it in GitHub Desktop.
Cookie parser in ~ 3 lines
const input = 'name=value; noname; novalue=; multieq=foo=bar';
const jar = input.split(';').reduceRight((jar, cookie) => {
const [_, name = '', value = ''] = cookie.trim().match(/^(?:([^=]+)=)?(?:(.*)|$)/);
return Object.assign({ [name]: value, }, jar);
}, {});
console.log(JSON.stringify(jar, null, 2));
// {
// "name": "value",
// "": "noname",
// "novalue": "",
// "multieq": "foo=bar"
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment