forked from marcuswestin/store.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore+json2.min.js
11 lines (11 loc) · 4.49 KB
/
store+json2.min.js
1
2
3
4
5
6
7
8
9
10
11
/* Copyright (c) 2010-2011 Marcus Westin */
var store=function(){var b={},g=window,m=g.document,f;b.disabled=false;b.set=function(){};b.get=function(){};b.remove=function(){};b.clear=function(){};b.transact=function(a,c){var d=b.get(a);if(typeof d=="undefined")d={};c(d);b.set(a,d)};b.serialize=function(a){return JSON.stringify(a)};b.deserialize=function(a){if(typeof a=="string")return JSON.parse(a)};var p;try{p="localStorage"in g&&g.localStorage}catch(i){p=false}if(p){f=g.localStorage;b.set=function(a,c){f.setItem(a,b.serialize(c))};b.get=
function(a){return b.deserialize(f.getItem(a))};b.remove=function(a){f.removeItem(a)};b.clear=function(){f.clear()}}else{var n;try{n="globalStorage"in g&&g.globalStorage&&g.globalStorage[g.location.hostname]}catch(r){n=false}if(n){f=g.globalStorage[g.location.hostname];b.set=function(a,c){f[a]=b.serialize(c)};b.get=function(a){return b.deserialize(f[a]&&f[a].value)};b.remove=function(a){delete f[a]};b.clear=function(){for(var a in f)delete f[a]}}else if(m.documentElement.addBehavior){f=m.createElement("div");
g=function(a){return function(){var c=Array.prototype.slice.call(arguments,0);c.unshift(f);m.body.appendChild(f);f.addBehavior("#default#userData");f.load("localStorage");c=a.apply(b,c);m.body.removeChild(f);return c}};b.set=g(function(a,c,d){a.setAttribute(c,b.serialize(d));a.save("localStorage")});b.get=g(function(a,c){return b.deserialize(a.getAttribute(c))});b.remove=g(function(a,c){a.removeAttribute(c);a.save("localStorage")});b.clear=g(function(a){var c=a.XMLDocument.documentElement.attributes;
a.load("localStorage");for(var d=0,h;h=c[d];d++)a.removeAttribute(h.name);a.save("localStorage")})}}try{b.set("__storejs__","__storejs__");if(b.get("__storejs__")!="__storejs__")b.disabled=true;b.remove("__storejs__")}catch(o){b.disabled=true}return b}();if(!this.JSON)this.JSON={};
(function(){function b(a){return a<10?"0"+a:a}function g(a){p.lastIndex=0;return p.test(a)?'"'+a.replace(p,function(c){var d=r[c];return typeof d==="string"?d:"\\u"+("0000"+c.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function m(a,c){var d,h,k,q,l=i,j,e=c[a];if(e&&typeof e==="object"&&typeof e.toJSON==="function")e=e.toJSON(a);if(typeof o==="function")e=o.call(c,a,e);switch(typeof e){case "string":return g(e);case "number":return isFinite(e)?String(e):"null";case "boolean":case "null":return String(e);
case "object":if(!e)return"null";i+=n;j=[];if(Object.prototype.toString.apply(e)==="[object Array]"){q=e.length;for(d=0;d<q;d+=1)j[d]=m(d,e)||"null";k=j.length===0?"[]":i?"[\n"+i+j.join(",\n"+i)+"\n"+l+"]":"["+j.join(",")+"]";i=l;return k}if(o&&typeof o==="object"){q=o.length;for(d=0;d<q;d+=1){h=o[d];if(typeof h==="string")if(k=m(h,e))j.push(g(h)+(i?": ":":")+k)}}else for(h in e)if(Object.hasOwnProperty.call(e,h))if(k=m(h,e))j.push(g(h)+(i?": ":":")+k);k=j.length===0?"{}":i?"{\n"+i+j.join(",\n"+i)+
"\n"+l+"}":"{"+j.join(",")+"}";i=l;return k}}if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+b(this.getUTCMonth()+1)+"-"+b(this.getUTCDate())+"T"+b(this.getUTCHours())+":"+b(this.getUTCMinutes())+":"+b(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()}}var f=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
p=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,i,n,r={"\u0008":"\\b","\t":"\\t","\n":"\\n","\u000c":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},o;if(typeof JSON.stringify!=="function")JSON.stringify=function(a,c,d){var h;n=i="";if(typeof d==="number")for(h=0;h<d;h+=1)n+=" ";else if(typeof d==="string")n=d;if((o=c)&&typeof c!=="function"&&(typeof c!=="object"||typeof c.length!=="number"))throw Error("JSON.stringify");return m("",
{"":a})};if(typeof JSON.parse!=="function")JSON.parse=function(a,c){function d(k,q){var l,j,e=k[q];if(e&&typeof e==="object")for(l in e)if(Object.hasOwnProperty.call(e,l)){j=d(e,l);if(j!==undefined)e[l]=j;else delete e[l]}return c.call(k,q,e)}var h;a=String(a);f.lastIndex=0;if(f.test(a))a=a.replace(f,function(k){return"\\u"+("0000"+k.charCodeAt(0).toString(16)).slice(-4)});if(/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){h=eval("("+a+")");return typeof c==="function"?d({"":h},""):h}throw new SyntaxError("JSON.parse");}})();