@likg/storage
Version:
3 lines (2 loc) • 1.38 kB
JavaScript
/* eslint-disable */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Storage=t()}(this,function(){"use strict";return class{static set(e,t){localStorage.setItem(e,JSON.stringify(t))}static get(e){const t=localStorage.getItem(e);if(t)return JSON.parse(t)}static del(e){const t=Object.prototype.toString.call(e).slice(8,-1).toLowerCase();if(e){if("string"===t)localStorage.removeItem(e);else if("array"===t){e.forEach(e=>{localStorage.removeItem(e)})}}else localStorage.clear()}static arrFind(e,t,r){if(!e||!t||!r)return;const o=localStorage.getItem(e);if(o){return JSON.parse(o).find(e=>e[t]===r)}}static arrSave(e,t,r){return new Promise((o,a)=>{if(!e||!t)return void a("参数不完整");let s=[];const i=localStorage.getItem(e);if(i&&(s=JSON.parse(i)),r){const e=s.findIndex(e=>e[r]===t[r]);-1!==e?s.splice(e,1,t):s.push(t)}else s.push(t);localStorage.setItem(e,JSON.stringify(s)),o(null)})}static arrGet(e){if(!e)return[];const t=localStorage.getItem(e);return t?JSON.parse(t):[]}static arrDel(e,t,r){if(!e)return;const o=localStorage.getItem(e);if(o){const a=JSON.parse(o);if(t&&r){const o=a.findIndex(e=>e[t]===r);-1!==o&&(a.splice(o,1),localStorage.setItem(e,JSON.stringify(a)))}else localStorage.setItem(e,JSON.stringify([]))}}}});