Skip to content

Instantly share code, notes, and snippets.

@jmoyers
Created May 8, 2011 03:40
Show Gist options
  • Save jmoyers/961078 to your computer and use it in GitHub Desktop.
Save jmoyers/961078 to your computer and use it in GitHub Desktop.
function parseCookie(str){
var obj = {}
, pairs = str.split(/[;,] */);
for (var i = 0, len = pairs.length; i < len; ++i) {
var pair = pairs[i]
, eqlIndex = pair.indexOf('=')
, key = pair.substr(0, eqlIndex).trim().toLowerCase()
, val = pair.substr(++eqlIndex, pair.length).trim();
// Quoted values
if (val[0] === '"') {
val = val.slice(1, -1);
}
// Only assign once
if (obj[key] === undefined) {
obj[key] = decodeURIComponent(val.replace(/\+/g, ' '));
}
}
return obj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment