-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.min.js
1 lines (1 loc) · 1.13 KB
/
storage.min.js
1
function storage(key,value,expires){if(key===null){return localStorage.clear()}let sub_key='';if(key.indexOf('.')){let keys=key.split('.');key=keys[0];sub_key=keys[1]}let value_type=typeof value;if(value_type!=='undefined'){if(value===null&&!sub_key){localStorage.removeItem(key)}else{if(expires){let data={value:value,expires:new Date().getTime()+expires*1000,};localStorage.setItem(key,JSON.stringify(data))}else{let data=storage(key);let data_type=typeof data;if(sub_key&&value!==null){let temp_value={};temp_value[sub_key]=value;value=temp_value;value_type='object'}if(data){if(data_type==='object'){if(value===null){delete data[sub_key]}else if(value_type==='object'){Object.assign(data,value)}value=data}}let set_value=JSON.stringify(value);localStorage.setItem(key,set_value)}}}else{let read_item=localStorage.getItem(key);let item;try{item=JSON.parse(read_item)}catch(error){item=read_item}if(item&&item.expires){let date=new Date().getTime();if(date>item.expires){localStorage.removeItem(key);return false}else{return item.value}}else{if(sub_key&&typeof item==='object'){item=item[sub_key];if(typeof item==='undefined'){item=null}}return item}}}