UNPKG

fast-storage-js

Version:

一个简单易用的js的localstorage响应式操作库

4 lines (2 loc) 3.48 kB
// Minified by Rollup + Terser (function(){"use strict";class Storage{constructor(id){this._id=id+"_";this._keysCache=null;this._lengthCache=null;return new Proxy(this,{get:(target,prop,receiver)=>{if(prop in target)return Reflect.get(target,prop,receiver);if(prop===Symbol.iterator)return target[Symbol.iterator].bind(target);if(typeof prop==="symbol")return undefined;return target.get(prop)},set:(target,prop,value)=>{if(prop in target)return Reflect.set(target,prop,value);target.set(prop,value);return true},has:(target,prop)=>{if(prop in target)return true;return target._has(prop)},deleteProperty:(target,prop)=>{if(prop in target)return false;target.remove(prop);return true},ownKeys:target=>{if(!target._keysCache){const internalKeys=Reflect.ownKeys(target).filter(key=>typeof key!=="symbol"||key===Symbol.iterator);const storageKeys=target.keys();target._keysCache=[...new Set([...internalKeys,...storageKeys])]}return target._keysCache},getOwnPropertyDescriptor:(target,prop)=>{if(prop==="length"){return{get:()=>target._getLength(),enumerable:false,configurable:true}}if(prop in target)return Reflect.getOwnPropertyDescriptor(target,prop);const value=target.get(prop);if(value!==undefined){return{value:value,writable:true,enumerable:true,configurable:true}}return undefined}})}get(key){const fullKey=this._id+key;const valueWithType=localStorage.getItem(fullKey);if(valueWithType===null)return undefined;const prefixEnd=valueWithType.indexOf("_");if(prefixEnd===-1)return valueWithType;const prefix=valueWithType.substring(0,prefixEnd);const value=valueWithType.substring(prefixEnd+1);switch(prefix){case"number":return Number(value);case"boolean":return value==="true";case"obj":try{return JSON.parse(value)}catch(e){console.error("Failed to parse object:",e);return undefined}case"string":return value;case"null":return null;default:return valueWithType}}set(key,value){const fullKey=this._id+key;let prefix="";let valueToStore;if(value===null){prefix="null_";valueToStore=""}else{switch(typeof value){case"number":prefix="number_";break;case"boolean":prefix="boolean_";break;case"object":prefix="obj_";value=JSON.stringify(value);break;default:prefix="string_"}valueToStore=prefix+value}localStorage.setItem(fullKey,valueToStore);this._invalidateCache()}_has(key){return localStorage.getItem(this._id+key)!==null}remove(key){localStorage.removeItem(this._id+key);this._invalidateCache()}clear(){const prefix=this._id;prefix.length;for(let i=localStorage.length-1;i>=0;i--){const key=localStorage.key(i);if(key.startsWith(prefix)){localStorage.removeItem(key)}}this._invalidateCache()}keys(){if(!this._keysCache){const prefix=this._id;const prefixLength=prefix.length;const keys=[];for(let i=0;i<localStorage.length;i++){const key=localStorage.key(i);if(key.startsWith(prefix)){keys.push(key.substring(prefixLength))}}this._keysCache=keys}return this._keysCache}_getLength(){if(this._lengthCache===null){const prefix=this._id;let count=0;for(let i=0;i<localStorage.length;i++){if(localStorage.key(i).startsWith(prefix)){count++}}this._lengthCache=count}return this._lengthCache}get length(){return this._getLength()}*[Symbol.iterator](){const prefix=this._id;const prefixLength=prefix.length;for(let i=0;i<localStorage.length;i++){const key=localStorage.key(i);if(key.startsWith(prefix)){const shortKey=key.substring(prefixLength);yield[shortKey,this.get(shortKey)]}}}_invalidateCache(){this._keysCache=null;this._lengthCache=null}}window.Storage=Storage})();