Nothing Special   »   [go: up one dir, main page]

Skip to content

zero-dependency/storage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@zero-dependency/storage

npm version npm bundle size (scoped) npm license

Installation

npm install @zero-dependency/storage
yarn add @zero-dependency/storage
pnpm add @zero-dependency/storage

Usage

import { LocalStorage } from '@zero-dependency/storage'

interface User {
  id: number
  name: string
}

const storageKey = 'users'
const storage = new LocalStorage<User[]>(storageKey, [])
console.log(storage.initialValue) // []

storage.write((prevValue) => [...prevValue, { id: 1, name: 'John' }])
console.log(storage.value) // [{ id: 1, name: 'John' }]

storage.reset()
console.log(storage.value) // []

// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event#examples
window.addEventListener('storage', (event) => {
  if (event.key === storageKey) {
    // do something
  }
})