shelf.js
Version:
A modular, powerful wrapper library for persistent objects in the browser and Node.js
9 lines • 4.6 kB
JavaScript
/**
* # 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)