forked from justindeguzman/locstor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locstor.min.js
7 lines (7 loc) · 2.96 KB
/
locstor.min.js
1
2
3
4
5
6
7
/*Locstor v1.0.5 http://github.com/justindeguzman/locstor*/var Locstor=function(){};
(function(){var c=function(){for(var a=3,b=document.createElement("div");b.innerHTML="\x3c!--[if gt IE "+ ++a+"]><i></i>< ![endif]--\x3e",b.getElementsByTagName("i")[0];);return 4<a?a:void 0}();(function(){var a=!1;try{localStorage.setItem("test","test"),localStorage.removeItem("test"),a=!0}catch(b){a=!1}a||(window.localStorage={getItem:function(a){return a&&this.hasOwnProperty(a)?unescape(document.cookie.replace(RegExp("(?:^|.*;\\s*)"+escape(a).replace(/[\-\.\+\*]/g,"\\$&")+"\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"),
"$1")):null},key:function(a){return unescape(document.cookie.replace(/\s*\=(?:.(?!;))*$/,"").split(/\s*\=(?:[^;](?!;))*[^;]?;\s*/)[a])},setItem:function(a,b){a&&(document.cookie=escape(a)+"="+escape(b)+"; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/",this.length=document.cookie.match(/\=/g).length)},length:0,removeItem:function(a){a&&this.hasOwnProperty(a)&&(document.cookie=escape(a)+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/",this.length--)},hasOwnProperty:function(a){return RegExp("(?:^|;\\s*)"+
escape(a).replace(/[\-\.\+\*]/g,"\\$&")+"\\s*\\=").test(document.cookie)}},window.localStorage.length=(document.cookie.match(/\=/g)||window.localStorage).length)})();Locstor.clear=function(){localStorage.clear()};Locstor.contains=function(a){if("string"!=typeof a)throw Error("Key must be a string for function contains(key)");return-1!==this.getKeys().indexOf(a)};Locstor.get=function(a){if("string"!=typeof a)throw Error("Key must be a string for function get(key)");a=localStorage.getItem(a);var b=
parseFloat(a);if(isNaN(b)){if("true"===a.toLowerCase()||"false"===a.toLowerCase())return"true"===a;if("null"===a)return null;try{return a=JSON.parse(a)}catch(d){return a}}else return b};Locstor.getKeys=function(){for(var a=[],b=0;b<localStorage.length;b++)a.push(localStorage.key(b));return a};Locstor.getRemainingSpace=function(){return c&&7<c?localStorage.remainingSpace:5242880-this.getSize()};Locstor.getSize=function(){return JSON.stringify(localStorage).length};Locstor.isEmpty=function(){return 0===
this.getKeys().length};Locstor.remove=function(a){if("string"==typeof a)localStorage.removeItem(a);else if(a instanceof Array)for(var b=0;b<a.length;b++)if("string"==typeof a[b])localStorage.removeItem(a[b]);else throw Error("Key in index "+b+" is not a string");else throw Error("Key must be a string or array for function remove(key || array)");};Locstor.set=function(a,b){if(1==arguments.length)this.store(a);else if("string"==typeof a)"object"===typeof b&&(b=JSON.stringify(b)),localStorage.setItem(a,
b);else throw Error("Invalid arguments for function set(key, value) or function set(object)");};Locstor.store=function(a){if("object"!=typeof a||a instanceof Array)throw Error("Argument for function set(object) must be an object");for(var b in a)localStorage.setItem(b,a[b])};Locstor.toObject=function(){for(var a={},b=this.getKeys(),d=0;d<b.length;d++){var c=b[d];a[c]=this.get(c)}return a}})();