UNPKG

cookiejs

Version:

A simple, lightweight JavaScript API for handling browser cookies.

3 lines (2 loc) 1.97 kB
/*! cookiejs v2.1.4 | MIT © 2026 kenny wong https://jaywcjlove.github.io/cookie.js/ */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).cookie=t()}(this,function(){"use strict";function e(e){return"string"==typeof(e=JSON.stringify(e))&&!!/^\{[\s\S]*\}$/.test(e)}function t(){if(!(this instanceof t))return new t}t.prototype={get:function(e){for(var t=e+"=",n=document.cookie.split(";"),o=0;o<n.length;o++){for(var i=n[o];" "==i.charAt(0);)i=i.substring(1,i.length);if(0==i.indexOf(t))return decodeURI(i.substring(t.length,i.length))}return!1},set:function(t,n,o){if(e(t))for(const e in t)this.set(e,t[e],n,o);else if("string"==typeof t){const i=e(o)?o:{expires:o},r=void 0!==i.path?`;path=${i.path};path=/`:";path=/",s=i.domain?`;domain=${i.domain}`:"",f=i.secure?";secure":"";let c=void 0!==i.expires?i.expires:"";"string"==typeof c&&""!==c?c=new Date(c):"number"==typeof c&&(c=new Date(+new Date+864e5*c)),""!==c&&"toGMTString"in c&&(c=`;expires=${c.toGMTString()}`);const u=i.sameSite?`;SameSite=${i.sameSite}`:"";document.cookie=`${t}=${encodeURI(n)+c+r+s+f+u}`}},remove:function(e){for(var t=0,n=(e=e instanceof Array?e:function(e){return Array.prototype.slice.call(e)}(arguments)).length;t<n;t++)this.set(e[t],"",-1);return e},clear:function(e){return e?this.remove(e):this.remove(function(e){var t=[],n="";for(n in e)t.push(n);return t}(this.all()))},all:function(){if(""===document.cookie)return{};for(var e=document.cookie.split("; "),t={},n=0,o=e.length;n<o;n++){var i=e[n].split("=");t[decodeURI(i[0])]=decodeURI(i[1])}return t}};let n=null;const o=function(o,i,r){const s=arguments;return n||(n=t()),0===s.length?n.all():1===s.length&&null===o?n.clear():2!==s.length||i?"string"!=typeof o||i?"string"==typeof o&&i||e(o)?n.set(o,i,r):void 0:n.get(o):n.clear(o)};for(const e in t.prototype)o[e]=t.prototype[e];return o});