UNPKG

@likg/storage

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