-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
1 lines (1 loc) · 2.63 KB
/
index.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vue-demi")):"function"==typeof define&&define.amd?define(["exports","vue-demi"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).VueStorage={},e.VueDemi)}(this,(function(e,t){"use strict";function r(){return Object.create(null)}class n{constructor(){this.clear()}getItem(e){return this._items[e]??null}setItem(e,t){this._items[e]=`${t}`}removeItem(e){delete this._items[e]}clear(){this._items=r()}}function i(e=new n){let i=r(),o=e=>{let r=i[e];r||(r=t.shallowRef(),i[e]=r),r.value},l=e=>{let t=i[e];t&&(t.value=r())};return{trackItem:o,triggerItem:l,getItem(t){let r=e.getItem(t);return o(t),r},setItem(t,r){let n=e.getItem(t);e.setItem(t,r),n!==e.getItem(t)&&l(t)},removeItem(t){let r=e.getItem(t);e.removeItem(t),null!==r&&l(t)},clear(){let t=Object.keys(i).filter((()=>null!==e.getItem(key)));e.clear(),t.forEach((e=>{l(e)}))}}}var o=(()=>{let{window:e}=globalThis;if(e){let{localStorage:t}=e;if(t){let r=i(t);return e.addEventListener("storage",(({key:e})=>{r.triggerItem(e)})),r}}return i()})(),l=(()=>{let{window:e}=globalThis;if(e){let{sessionStorage:t}=e;if(t)return i(t)}return i()})(),s={parse:e=>Boolean(JSON.parse(e)),stringify:e=>String(Boolean(e))},u={parse:e=>Number(JSON.parse(e)),stringify:e=>String(Number(e))},a={parse:String,stringify:String};function f(e){return"function"==typeof e}function c(e){return f(e)?e:t.isRef(e)?()=>e.value:()=>e}function m(e,{default:r=null,session:n=!1,type:i=JSON}={}){let f=c(e),m=c(r),g=c(n),d=()=>g()?l:o,{parse:p,stringify:I}=(e=>{switch(e){case Boolean:return s;case Number:return u;case String:return a}return e})(i);return t.computed({get(){let e=d(),t=f(),r=e.getItem(t);return r=null==r?m():p(r),r},set(e){let t=d(),r=f();null==e?t.removeItem(r):(e=I(e),t.setItem(r,e))}})}function g(e,t){return{...e,...t}}function d(e,{optionName:t="stored",prefix:r}={}){e.config.optionMergeStrategies[t]=g,e.mixin(function({optionName:e="stored",prefix:t=""}={}){return{computed:{},beforeCreate(){let{$options:r}=this,n=r[e];void 0!==n&&function(e){if(e){let t=typeof e;return"object"===t||"function"===t}return!1}(n)&&Object.entries(n).forEach((([e,n])=>{let{key:i=e,...o}=(()=>{let e={...n};return["default","key","session"].forEach((t=>{let r=e[t];f(r)&&(r=r.bind(this),e[t]=r)})),e})(),l=c(i),s=m((()=>`${t}${l()}`),o);r.computed[e]={get:()=>s.value,set(e){s.value=e}}}))}}}({optionName:t,prefix:r}))}let p={install:d};var I,h;t.isVue2&&(null===(I=globalThis.window)||void 0===I||null===(h=I.Vue)||void 0===h||h.use(p));e.default=p,e.install=d,e.localStorage=o,e.sessionStorage=l,e.stored=m,Object.defineProperty(e,"__esModule",{value:!0})}));