-
Notifications
You must be signed in to change notification settings - Fork 0
/
argon.min.js
1 lines (1 loc) · 2.01 KB
/
argon.min.js
1
"use strict";(function(window){var typeFlags={"Number()":"n","String()":"s","Boolean()":"b","Object()":"o","Array()":"a","Date()":"d","Function()":"f",NaN:"z",undefined:"u","null":"l"};var convertFlag={s:function(val){return val},n:function(val){return val*1},z:function(){return NaN},b:function(val){return val==="true"?true:false},o:function(val){return JSON.parse(val)},a:function(val){return JSON.parse(val)},d:function(val){return new Date(val*1)},f:function(val){var fnName=val.slice(8,val.indexOf("("));var fnArgs=val.slice(val.indexOf("(")+1,val.indexOf(")")).replace(" ","").split(",");var fnBody=val.slice(val.indexOf("{")+1,val.lastIndexOf("}"));var fn=new Function("return function "+fnName+"("+fnArgs.toString()+"){"+fnBody+"}");return fn()},u:function(){return undefined},l:function(){return null}};var storeBase=function(storeType){return{get:function(key){if(!key){return new Error("Key is required")}else{var data=storeType.getItem(key);if(data!==null){var typeHint=data.slice(0,1);data=convertFlag[typeHint](data.slice(1))}return data}},set:function(key,val){if(arguments.length!==2){return new Error("Key and value are required")}else{var typeKey;var typeFlag;if(val===null){typeKey="null"}if(val===undefined){typeKey="undefined"}if(typeof val==="number"&&val!==val){typeKey="NaN"}if(!typeKey){typeKey=val.constructor.toString().split(" ")[1]}typeFlag=typeFlags[typeKey];if(typeFlag==="a"||typeFlag==="o"){val=JSON.stringify(val)}if(typeFlags[typeKey]==="d"){val=val.getTime()}try{storeType.setItem(key,typeFlags[typeKey]+val)}catch(err){return err}}},remove:function(key){if(key===undefined){return new Error("Key is required")}else{storeType.removeItem(key)}},clear:function(){storeType.clear()}}};var hasStorage=false;try{var mod="modernizr";window.localStorage.setItem(mod,mod);window.localStorage.removeItem(mod);hasStorage=true}catch(e){}window.argon=function(){if(!hasStorage){return new Error("localStorage/sessionStorage not available")}else{return{local:storeBase(localStorage),session:storeBase(sessionStorage)}}}()})(window);