Skip to content

Instantly share code, notes, and snippets.

@atk
Forked from 140bytes/LICENSE.txt
Created July 1, 2011 14:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atk/1058674 to your computer and use it in GitHub Desktop.
Save atk/1058674 to your computer and use it in GitHub Desktop.
Cookie helper

Cookie helper

A small function to get the escaped value of a named cookie. There's also a bigger function that works as a cookie setting preparer, though it does only get the escaped value and does not yet fit into 140 bytes at the moment (2 bytes golfing, anyone?):

var C=function(c,d,e){e='';for(d in c)c.hasOwnProperty(d)&&(e+=(e?'; ':e)+d+'='+c[d]);return''+c!==c?e:(document.cookie.match(c+'=(.+?);')||0)[1]}
// setting a Cookie:
document.cookie = C({cookiename: 'testcookie', expires: (new Date(new Date()*1+6E10)).toGMTString()});
C('cookiename') // -> returns the still escaped value of the Cookie "cookiename"
function(
c // cookie name
){
// unescape cookie value
return unescape(
// coerces to RegExp /name=([^;]+)/ within match
(document.cookie.match(c+'=(.+?);')||0)[1]||''
)
}
// other version:
function(
c, // cookie name or cookie object
d, // placeholder
e // result for object parameterisation
){
// init result
e='';
// try to parameterize input
for (d in c)
// only unprototypical stuff is added to result in the format key=value[; ]
c.hasOwnProperty(d) && (e+=(e?'; ':e)+d+'='+c[d]);
// input is not a string?
return ''+c !== c ?
// return parameterized input
e :
// or coerce the cookiename to a regexp to match its unescaped value
(document.cookie.match(c+'=(.+?);')||0)[1]
}
function(c){return unescape((document.cookie.match(c+'=(.+?);')||0)[1]||'')}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "cookie",
"description": "Gets a cookie value by name (setting in an extra ungolfed version)",
"keywords": [
"cookie",
"value",
"by",
"name"
]
}
<!DOCTYPE html>
<title>Cookie</title>
<div>Expected value: <b>http[s]://github.com/[your username]</b> (on github content of your github tracker cookie)</div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(c){return unescape((document.cookie.match(c+'=(.+?);')||0)[1]||'')}
document.getElementById( "ret" ).innerHTML = myFunction('tracker')
</script>
@jed
Copy link

jed commented Jul 3, 2011

would love to see a cookie getter/setter like the one for local storage. you still have a lot of room!

@atk
Copy link
Author

atk commented Jul 3, 2011

If you had an idea to golf the parameterisation of the cookie object down, it'd be really helpful; doing this without hasOwnProperty is neglienty... I already tried to use JSON.stringify + replace, still too long.

@atk
Copy link
Author

atk commented Jul 4, 2011

If we don't handle prototypical overloading, we could use

function(c,d,e){e='';for(d in c)(e+=(e?'; ':e)+d+'='+c[d]);return''+c===c?(document.cookie.match(c+'=([^;]+)')||0)[1]:(document.cookie=e)}

This would not only prepare cookies from objects, but also set them.

@jed
Copy link

jed commented Jul 4, 2011

yeah, i wouldn't worry to much about prototypes. i'd imagine almost all implementations would use vanilla objects.

i like the above implementation, but not sure about the API... you pass an object to set and a string to get? seems a bit ambitious, since setting multiple cookies at the same time is a luxury i'm not sure you can afford.

i'd much rather use something like:

{get:function(){...},set:function(){...}}

or if you insist on a single function, have it set on two args and get on one, like jQuery's attr.

@atk
Copy link
Author

atk commented Jul 4, 2011

Its not setting multiple cookies at a time (or at least it is not meant to), but receives either a string or an object with the information for 1 (one) cookie:

C=function(c,d,e){e='';for(d in c)(e+=(e?'; ':e)+d+'='+c[d]);return''+c===c?(document.cookie.match(c+'=([^;]+)')||0)[1]:(document.cookie=e)};
C({test: 'value', expires: (new Date(new Date()*1+6E10)).toGMTString(), path: '/', domain: location.host });
C('test') // -> value

this way you can set every cookie attribute, not only name and value. Otherwise, you are restricted to the current session/path/domain.

@jed
Copy link

jed commented Jul 4, 2011

ah, my bad. personally, i'm a fan of the pattern i use for node.js, where the signature is (key, value, options), since it's easier to use for the simple case, but this is pretty nice.

@atk
Copy link
Author

atk commented Jul 4, 2011

I made a habit of writing functions with less than 4 arguments, since they can be confused so easily otherwise. A colleague of mine wrote a cookie setter function that had up to 6 arguments and always had to look up the correct order; whereas the properties of an input object can be easily spotted by its keys.

Your pattern has its merits, anyway. Usually I try to create an interface which is usable in the most obvious ways (so the "perfect" cookie function would allow for C(key, value[, object]) too), but haven't yet found a way to do so within 140 bytes.

@tsaniel
Copy link

tsaniel commented Jul 13, 2011

Just another byte, let c+'=([^;]+)' be replaced by c+'=(.+?);'.

@atk
Copy link
Author

atk commented Jul 13, 2011

thanks, you're right.

@louisremi
Copy link

There are important bugs affecting both versions of the script:

  • C("cker"); will also return the content of the "tracker" cookie.
  • last cookie in alphabetical order won't be returned because it isn't followed by a ";"

You should use: ('; '+document.cookie+';').match('; '+c+'=(.+?);')

And your getter/setter function isn't really useful IMHO. I'd prefer a longer version that (un)escapes strings properly and doesn't require me to write document.cookie = C({...});

@atk
Copy link
Author

atk commented Dec 2, 2011

I'd like that too, but the byte restriction is too hard on that one...

@louisremi
Copy link

I mean you can keep the "getter only" version under 140 bytes and add a smallest possible "getter/setter" script.

@azproduction
Copy link

Regexp should be this:

c + '=(.+?)(:?;|$)'

in case of cookie is last document.cookie === "key=value; _id=value"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment