0.2.0 • Published 5 months ago

typed-ls v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

:sparkles: Features

A tiny helper library that creates type-safe get and set functions for working with local storage

Why?

  • The key is only defined once
  • Ensures the same type is used when both getting and setting the value
  • Reduces boilerplate

How to use

import { createStoredValue } from 'typed-ls'

const defaultValue = 'en'

export const language = createStoredValue('language', defaultValue)
// language.get() => 'en'
// language.set('jp')
// language.remove()

The type will be inferred from the default value. If this is not possible (for example if the default value is undefined or []) you can explicitly set the type instead:

export const language = createStoredValue<string[]>('languages', [])

:newspaper: API

createStoredValue<T>(key: string, defaultPayload: T): StoredValue
type StoredValue = {
  get: () => T
  set: (payload: T) => void
  remove: () => void
}

key

The local storage key

defaultPayload

If there is no value in local storage, get will return the defaultPayload instead


:package: Install

npm install typed-ls

Local storage not available

If local storage is not available then:

  • get always returns default values
  • set and remove are no-ops

This can happen if the user has turned off local storage in the privacy setting of their browser