Skip to content

Instantly share code, notes, and snippets.

@Pan-Maciek
Created February 10, 2017 20:51
Show Gist options
  • Select an option

  • Save Pan-Maciek/55ec1cd571e0ffb2711d1f22920bf17e to your computer and use it in GitHub Desktop.

Select an option

Save Pan-Maciek/55ec1cd571e0ffb2711d1f22920bf17e to your computer and use it in GitHub Desktop.
/*! easyCookies v1.1.0 | (c) Maciej Kozieja | https://github.com/koziejka/easyCookies */
const cookie=new Proxy({},{get(a,b){const c=RegExp(`${b}=(.*?);`).exec(document.cookie+";");return null==c?null:JSON.parse(decodeURIComponent(c[1]))},set(a,b,c){let d="";c.isCookie&&(c.expires&&(d+=`expires=${c.expires};`),c.path&&(d+=`path=${c.path};`),c=c.value),"object"==typeof c?c=JSON.stringify(c):"string"==typeof c&&(c=`"${c}"`),document.cookie=`${b}=${encodeURIComponent(c)};${d}`},deleteProperty(a,b){document.cookie=`${b}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;`}});Object.defineProperty(window,"cookies",{get(){const a={},b=RegExp(/(\w)+=(.*?);/g),c=document.cookie+";";let d;for(;d=b.exec(c);)a[d[1]]=JSON.parse(decodeURIComponent(d[2]));return a}});class Cookie{constructor(value,expires,path){this.value=value||value;if(expires){if(typeof expires=="string")this.expires=expires;else this.expires=expires.toUTCString()}if(path)this.path=path;this.isCookie=true}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment