Created
May 8, 2011 03:40
-
-
Save jmoyers/961078 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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