UNPKG

@naverpay/vanilla-store

Version:

![NPM Version](https://img.shields.io/npm/v/%40naverpay%2Fvanilla-store) ![NPM bundle size](https://img.shields.io/bundlephobia/min/%40naverpay%2Fvanilla-store) ![NPM Downloads](https://img.shields.io/npm/dw/%40naverpay%2Fvanilla-store)

36 lines (35 loc) 894 B
import { Persistent, isSerializeValue } from "./type.mjs"; class LocalStoragePersist extends Persistent { get serialized() { return window.localStorage.getItem(this.key); } get value() { const storageValue = window.localStorage.getItem(this.key); if (!isSerializeValue(storageValue)) { return this._value; } const parsedValue = storageValue ? JSON.parse(storageValue) : null; if (!this.typeAssertion(parsedValue)) { return this._value; } if (this._value !== parsedValue) { this._value = parsedValue; } return this._value; } set value(value) { if (this.typeAssertion(value)) { this._value = value; window.localStorage.setItem(this.key, JSON.stringify(value)); } } clear() { try { window.localStorage.removeItem(this.key); } catch { } } } export { LocalStoragePersist as default };