Git Product home page Git Product logo

local-store's Introduction

local-store

Helpers to make local storage easier to use and just like cookie, having expire time.

License Version Downloads Downloads

Usage

import localStore from '@hooshid/local-store'

Methods

  • set — Add item to local storage.
  • get — Get item from local storage
  • remove — Remove item from local storage.
  • clearAll — Removes all or just Prefixed items from local storage.
  • has — Check item exist in local storage.
  • getExpireTime — Get item expire time.
  • getRemainTime — Get remaining TTL of a item.

Set

Set an item in local storage.

set(key: string, value: string, ttl?: number | string): void
  • key - The key of the item to set.
  • value - The value to set.
  • ttl - The time life in seconds format for living cache in localStorage.
  • ttl - The time life in default is one year, if you want set time life you should set in seconds or set like below format.
m for minute
h for hour
d for day

examples

  • 60m -> 60 minute
  • 1h -> 1 hour (equal to 60m)
  • 24h -> 24 hours (equal to 1d)
  • 1d -> 1 day
  • 30d -> 30 days

just use 1 above method and don't mixed (1d12h does not work -> 36h work fine!)

Get

Get an item from local storage.

get(key: string): string | null
  • key — The key of the item to get.

Remove

Remove item from local storage.

remove(key: string): void
  • key — The key of the item to remove.

Clear All

Removes all or just Prefixed items from local storage.

clearAll(prefix?: string): void
  • prefix — if prefix provided, only keys start with prefix cleared and if prefix undefined all keys cleared.

Has

Check key is exist

has(key: string): boolean
  • key — The key of the item.

Get Expire Time

Get item expire time.

getExpireTime(key: string): null|number
  • key — The key of the item.

Get Remain Time

Get remaining time of a item.

getRemainTime(key: string): null|number
  • key — The key of the item.

Todo

  • method for clear expired caches
  • output js script and define types separately

local-store's People

Contributors

arkamali 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.