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)

35 lines (34 loc) 887 B
"use strict"; const type = require("./type.js"); class LocalStoragePersist extends type.Persistent { get serialized() { return window.localStorage.getItem(this.key); } get value() { const storageValue = window.localStorage.getItem(this.key); if (!type.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 { } } } module.exports = LocalStoragePersist;