Giter Club home page Giter Club logo

storage-hook's Introduction

storage-hook

npm package build status


storage-hook ensures that the original type of js (String / Boolean / Number / Date / Object / Array) is stable in storage (localStorage, sessionStorage and more).

Installing

Using npm:

$ npm install use-type-store

Using yarn:

$ yarn add use-type-store

Using unpkg CDN:

<script src="https://unpkg.com/use-type-store/dist/storage-hook.iife.js"></script>

Example

useLocalStorage create a type stable localstorage instance.

interface User {
  name: string
}

const ls = useLocalStorage({
  key1: Boolean,
  key2: Number,
  key3: String,
  user: (Object as unknown) as User
})

we can use localstorage api and had the expect type.

const key1 = ls.getItem('key1')
// must be boolean or not will throw a promise error
key1

setting storage.

ls.setItem('key1', true)

remove storage key.

ls.removeItem('key1')

clear all storage keys.

ls.clear()

exact the options types.

type Options = ExactStorageOptions<typeof ls>

about deep object

But there is no way to ensure the type of Object after ts coercion (e.g. interface User in this example).

we don't traverse the object to make the object type stable.

const user = ls.getItem('key4')
// not necessarily exist and type not necessarily be string
user.name

Provide two ideas:

  1. flat user object by new storage.
const user = useLocalStorage({
  name: String
  age: Number
})
  1. validate by yourself.

storage-hook's People

Contributors

poyoho avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.