UNPKG

crumbsjs

Version:

A lightweight, intuitive, Vanilla ES6 fueled JS cookie library

1 lines 3.65 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):e.crumbs=r()}(this,function(){"use strict";const e={debug:!1,setDebug:function(e){try{this.debug=e}catch(e){this.throwError(e)}},isLsAvailable:function(){try{return localStorage.setItem("test","test"),localStorage.removeItem("test"),!0}catch(e){return!1}},throwError:function(e,r="error"){console[r](`[crumbsJS] An error has occurred: ${e}`)},set:function(e,r,t,o){try{if(Array.isArray(e)){var a=e;return a.forEach(e=>{if(!e.hasOwnProperty("name")||!e.hasOwnProperty("value"))throw"Mass cookie set failed, on or more object properties are incorrect."}),a.map(e=>!!this.set(e.name,e.value,e.expires,e.domain)&&e).filter(e=>e)}var i="";if(null!=t){var s=new Date,n=864e5;if("object"==typeof t){switch(t.type.toLowerCase()){case"minute":n=6e4;break;case"hour":n=36e5;break;case"day":n=864e5;break;case"week":n=6048e5;break;case"month":n=24192e5;break;default:throw"Not a valid time type format (use minute, hour, day, week or month only)"}t=t.value}s.setTime(s.getTime()+t*n),s.toUTCString(),i=`expires=${s}`}let l=""+`${e}=${r};${i}ף${null!=o?`path=${o};`:o}`;return document.cookie=l,!0}catch(e){return this.throwError(e),!1}},get:function(e){try{var r=decodeURIComponent(document.cookie),t=(r=r.split("; ")).filter(r=>{return(r=r.split("="))[0]===e?1:0});return t.length>0?t[0].split("=")[1]:null}catch(e){return this.throwError(e),!1}},getAll:function(){try{var e=decodeURIComponent(document.cookie);return!!(e=e.split("; "))[0]&&e.map(e=>{return{name:(e=e.split("="))[0],value:e[1]}})}catch(e){return this.throwError(e),!1}},delete:function(e){try{return Array.isArray(e)?(e.forEach(e=>{this.delete(e)}),!0):(document.cookie=`${e}=''; expires=Thu, 01 Jan 1970 00:00:01 GMT`,!0)}catch(e){this.throwError(e)}},deleteAll:function(){try{var e=decodeURIComponent(document.cookie);return e=e.split("; ").map(e=>{e=e.split("=");return this.delete(e[0])}),!0}catch(e){this.throwError(e)}},ls:{throwError:(r,t="error")=>{e.throwError(r,t)},ls:window.localStorage,set:function(r,t){if(!e.isLsAvailable())return this.throwError("Local Storage is not available, action was completed using cookies","warn"),e.set(r,t);try{return Array.isArray(r)?(r.forEach(e=>{if(!e.hasOwnProperty("key")||!e.hasOwnProperty("value"))throw"Mass key-value pair set failed, on or more object properties are incorrect."}),r.map(e=>{this.set(e.key,e.value)}).filter(e=>e)):(this.ls.setItem(r,JSON.stringify(t)),!0)}catch(e){return this.throwError(e),!1}},get:function(r,t=!0){if(!e.isLsAvailable())return this.throwError("Local Storage is not available, action was completed using cookies","warn"),e.get(r);try{return Array.isArray(r)?r.map(e=>({key:e,value:this.get(e)})).filter(e=>e):t?JSON.parse(this.ls.getItem(r)):this.ls.getItem(r)}catch(e){return this.throwError(e),!1}},getAll:function(r=!0){if(!e.isLsAvailable())return this.throwError("Local Storage is not available, action was completed using cookies"),e.getAll();try{let e=[];for(let t in this.ls)"key"!=t&&"getItem"!=t&&"setItem"!=t&&"removeItem"!=t&&"clear"!=t&&"length"!=t&&e.push({key:t,value:r?JSON.parse(this.ls[t]):this.ls[t]});return e}catch(e){return this.throwError(e),!1}},delete:function(r){if(!e.isLsAvailable())return this.throwError("Local Storage is not available, action was aborted"),!1;try{return this.ls.removeItem(r),!0}catch(e){return this.throwError(e),!1}},deleteAll:function(){if(!e.isLsAvailable())return this.throwError("Local Storage is not available, action was aborted"),!1;try{return this.ls.clear(),!0}catch(e){return this.throwError(e),!1}}}};return e});