Skip to content

Instantly share code, notes, and snippets.

@mgd020
Last active April 4, 2020 15:54
Show Gist options
  • Save mgd020/79187006eda38b249c3d5d4c71563a90 to your computer and use it in GitHub Desktop.
Save mgd020/79187006eda38b249c3d5d4c71563a90 to your computer and use it in GitHub Desktop.
cookie functions
/*
cookies.get() gets all cookies by name
cookies.get('name') gets single cookie
cookies.set('name', 'value', {max_age, expires, path, domain})
cookies.del('name', {path, domain})
https://javascript-minifier.com/ = 731 bytes
window.cookies=function(){function e(e){return e.replace(/[;,\s]+/g,e=>escape(e))}var n=unescape,c=document,i="; ",o="",u="path",s="domain",p="max",t="age",r="expires",a="secure",h="samesite",f="=",g=p+"_"+t;function m(n,m,d){if(n){var l=[e(n),f,e(m||o)];(d=d||{})[u]&&l.push(i,u,f,d[u]),d[s]&&l.push(i,s,f,d[s]),d[g]?l.push(i,p,"-",t,f,d[g]):d[r]&&l.push(i,r,f,d[r].toUTCString()),d[a]&&l.push(i,a),d[h]&&l.push(i,h,f,d[h]),c.cookie=l.join(o)}}return{get:function(i){return i?new RegExp("\\b"+e(i).replace(/\W/g,"\\$&")+"=([^;]*)").exec(c.cookie)[1]:c.cookie.split("; ").map(e=>/([^=]+)=?(.*)/.exec(e).slice(1,3)).map(e=>[n(e[0]),n(e[1])]).reduce((e,n)=>(e[n[0]]=n[1],e),{})},set:m,del:function(e,n){(n=n||{})[g]=-1,m(e,o,n)}}}();
*/
window.cookies = (function(){
function escape_cookie(value) {
return value.replace(/[;,\s]+/g, s => escape(s));
}
var unescape_cookie = unescape,
doc = document,
sep = '; ',
empty = '',
path = 'path',
domain = 'domain',
max = 'max',
age = 'age',
expires = 'expires',
secure = 'secure',
samesite = 'samesite',
eq = '=',
max_age = max + '_' + age;
function get(name) {
return name
? (new RegExp('\\b' + escape_cookie(name).replace(/\W/g, '\\$&') + '=([^;]*)')).exec(doc.cookie)[1]
: doc.cookie.split(
'; '
).map(
// 'name=value' => ['name', 'value']
c => /([^=]+)=?(.*)/.exec(c).slice(1, 3)
).map(
// ['name', 'value'] => [unescape('name'), unescape('value')]
c => [unescape_cookie(c[0]), unescape_cookie(c[1])]
).reduce(
// [['name', 'value']...] => {'name': 'value'...}
(d, c) => {
d[c[0]] = c[1];
return d;
},
{}
);
}
function set(name, value, options) {
if (!name) return;
var directives = [
escape_cookie(name), eq, escape_cookie(value || empty)
];
options = options || {};
if (options[path]) directives.push(sep, path, eq, options[path]);
if (options[domain]) directives.push(sep, domain, eq, options[domain]);
if (options[max_age]) {
directives.push(sep, max, '-', age, eq, options[max_age]);
} else if (options[expires]) {
directives.push(sep, expires, eq, options[expires].toUTCString());
}
if (options[secure]) directives.push(sep, secure)
if (options[samesite]) directives.push(sep, samesite, eq, options[samesite]);
doc.cookie = directives.join(empty);
}
function del(name, options) {
options = options || {};
options[max_age] = -1;
set(name, empty, options);
}
return {
'get': get,
'set': set,
'del': del,
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment