UNPKG

shelf.js

Version:

A modular, powerful wrapper library for persistent objects in the browser and Node.js

9 lines 6.98 kB
/** * # Shelf.JS * Copyright 2014 Stefano Balietti * GPL licenses. * * Persistent Client-Side Storage * --- */ (function(e){var t="5.1",n,r;r="volatile",n=e.store=function(e,t,r,i){r=r||{},i=r.type&&r.type in n.types?r.type:n.type;if(!i||!n.types[i]){n.log("Cannot save/load value. Invalid storage type selected: "+i,"ERR");return}return n.log("Accessing "+i+" storage"),n.types[i](e,t,r)},n.prefix="__shelf__",n.verbosity=0,n.types={};try{Object.defineProperty(n,"type",{set:function(e){return"undefined"==typeof n.types[e]?(n.log("Cannot set store.type to an invalid type: "+e),!1):(r=e,e)},get:function(){return r},configurable:!1,enumerable:!0})}catch(i){n.type=r}n.addType=function(e,t){n.types[e]=t,n[e]=function(t,r,i){return i=i||{},i.type=e,n(t,r,i)};if(!n.type||n.type==="volatile")n.type=e},n.onquotaerror=undefined,n.error=function(){console.log("shelf quota exceeded"),"function"==typeof n.onquotaerror&&n.onquotaerror(null)},n.log=function(e){n.verbosity>0&&console.log("Shelf v."+t+": "+e)},n.isPersistent=function(){return n.types?n.type==="volatile"?!1:!0:!1};try{Object.defineProperty(n,"persistent",{set:function(){},get:n.isPersistent,configurable:!1})}catch(i){n.persistent=!1}n.decycle=function(e){return JSON&&JSON.decycle&&"function"==typeof JSON.decycle&&(e=JSON.decycle(e)),e},n.retrocycle=function(e){return JSON&&JSON.retrocycle&&"function"==typeof JSON.retrocycle&&(e=JSON.retrocycle(e)),e},n.stringify=function(e){if(!JSON||!JSON.stringify||"function"!=typeof JSON.stringify)throw new Error("JSON.stringify not found. Received non-stringvalue and could not serialize.");return e=n.decycle(e),JSON.stringify(e)},n.parse=function(e){if("undefined"==typeof e)return undefined;if(JSON&&JSON.parse&&"function"==typeof JSON.parse)try{e=JSON.parse(e)}catch(t){n.log("Error while parsing a value: "+t,"ERR"),n.log(e)}return e=n.retrocycle(e),e},function(){function r(e){return n.parse(n.stringify(e))}var e={},t={};n.addType("volatile",function(n,i,s){return n?i===undefined?r(e[n]):(t[n]&&(clearTimeout(t[n]),delete t[n]),i===null?(delete e[n],null):(e[n]=i,s.expires&&(t[n]=setTimeout(function(){delete e[n],delete t[n]},s.expires)),i)):r(e)})}()})("undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports:this),function(e){function r(e,r){t.addType(e,function(i,s,o){var u,a,f,l,c=s,h=(new Date).getTime();if(!i){c={},l=[],f=0;try{i=r.length;while(i=r.key(f++))n.test(i)&&(a=t.parse(r.getItem(i)),a.expires&&a.expires<=h?l.push(i):c[i.replace(rprefix,"")]=a.data);while(i=l.pop())r.removeItem(i)}catch(p){}return c}i=t.prefix+i;if(s===undefined){u=r.getItem(i),a=u?t.parse(u):{expires:-1};if(!(a.expires&&a.expires<=h))return a.data;r.removeItem(i)}else if(s===null)r.removeItem(i);else{a=t.stringify({data:s,expires:o.expires?h+o.expires:null});try{r.setItem(i,a)}catch(p){t[e]();try{r.setItem(i,a)}catch(p){throw t.error()}}}return c})}var t=e.store;if(!t)throw new Error("amplify.shelf.js: shelf.js core not found.");if("undefined"==typeof window)throw new Error("amplify.shelf.js: window object not found.");var n=new RegExp("^"+t.prefix);for(var i in{localStorage:1,sessionStorage:1})try{window[i].setItem(t.prefix,"x"),window[i].removeItem(t.prefix),r(i,window[i])}catch(s){}if(!t.types.localStorage&&window.globalStorage)try{r("globalStorage",window.globalStorage[window.location.hostname]),t.type==="sessionStorage"&&(t.type="globalStorage")}catch(s){}(function(){if(t.types.localStorage)return;var e=document.createElement("div"),n=t.prefix;e.style.display="none",document.getElementsByTagName("head")[0].appendChild(e);try{e.addBehavior("#default#userdata"),e.load(n)}catch(r){e.parentNode.removeChild(e);return}t.addType("userData",function(r,i,s){e.load(n);var o,u,a,f,l,c=i,h=(new Date).getTime();if(!r){c={},l=[],f=0;while(o=e.XMLDocument.documentElement.attributes[f++])u=t.parse(o.value),u.expires&&u.expires<=h?l.push(o.name):c[o.name]=u.data;while(r=l.pop())e.removeAttribute(r);return e.save(n),c}r=r.replace(/[^\-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u037f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/g,"-"),r=r.replace(/^-/,"_-");if(i===undefined){o=e.getAttribute(r),u=o?t.parse(o):{expires:-1};if(!(u.expires&&u.expires<=h))return u.data;e.removeAttribute(r)}else i===null?e.removeAttribute(r):(a=e.getAttribute(r),u=t.stringify({data:i,expires:s.expires?h+s.expires:null}),e.setAttribute(r,u));try{e.save(n)}catch(p){a===null?e.removeAttribute(r):e.setAttribute(r,a),t.userData();try{e.setAttribute(r,u),e.save(n)}catch(p){throw a===null?e.removeAttribute(r):e.setAttribute(r,a),t.error()}}return c})})()}(this),function(e){var t=e.store;if(!t)throw new Error("cookie.shelf.js: shelf.js core not found.");if("undefined"==typeof window)throw new Error("cookie.shelf.js: window object not found.");var n=function(){var e,n,r,i,s={expiresAt:null,path:"/",domain:null,secure:!1};return e=function(e){var t,n;return typeof e!="object"||e===null?t=s:(t={expiresAt:s.expiresAt,path:s.path,domain:s.domain,secure:s.secure},typeof e.expiresAt=="object"&&e.expiresAt instanceof Date?t.expiresAt=e.expiresAt:typeof e.hoursToLive=="number"&&e.hoursToLive!==0&&(n=new Date,n.setTime(n.getTime()+e.hoursToLive*60*60*1e3),t.expiresAt=n),typeof e.path=="string"&&e.path!==""&&(t.path=e.path),typeof e.domain=="string"&&e.domain!==""&&(t.domain=e.domain),e.secure===!0&&(t.secure=e.secure)),t},n=function(t){return t=e(t),(typeof t.expiresAt=="object"&&t.expiresAt instanceof Date?"; expires="+t.expiresAt.toGMTString():"")+"; path="+t.path+(typeof t.domain=="string"?"; domain="+t.domain:"")+(t.secure===!0?"; secure":"")},r=function(){var e={},n,r,i,s,o=document.cookie.split(";"),u;for(n=0;n<o.length;n+=1){r=o[n].split("="),i=r[0].replace(/^\s*/,"").replace(/\s*$/,"");try{s=decodeURIComponent(r[1])}catch(a){s=r[1]}e[i]=t.parse(s)}return e},i=function(){},i.prototype.get=function(e){var t,n,i=r();if(typeof e=="string")t=typeof i[e]!="undefined"?i[e]:null;else if(typeof e=="object"&&e!==null){t={};for(n in e)typeof i[e[n]]!="undefined"?t[e[n]]=i[e[n]]:t[e[n]]=null}else t=i;return t},i.prototype.filter=function(e){var t,n={},i=r();typeof e=="string"&&(e=new RegExp(e));for(t in i)t.match(e)&&(n[t]=i[t]);return n},i.prototype.set=function(e,r,i){if(typeof i!="object"||i===null)i={};typeof r=="undefined"||r===null?(r="",i.hoursToLive=-8760):typeof r!="string"&&(r=t.stringify(r));var s=n(i);document.cookie=e+"="+encodeURIComponent(r)+s},i.prototype.del=function(e,t){var n={},r;if(typeof t!="object"||t===null)t={};typeof e=="boolean"&&e===!0?n=this.get():typeof e=="string"&&(n[e]=!0);for(r in n)typeof r=="string"&&r!==""&&this.set(r,null,t)},i.prototype.test=function(){var e=!1,t="cT",n="data";return this.set(t,n),this.get(t)===n&&(this.del(t),e=!0),e},i.prototype.setOptions=function(t){typeof t!="object"&&(t=null),s=e(t)},new i}();n.test()&&t.addType("cookie",function(e,t,r){return"undefined"==typeof e?n.get():"undefined"==typeof t?n.get(e):t===null?(n.del(e),null):n.set(e,t,r)})}(this)