Giter Site home page Giter Site logo

localstory's Introduction

localstory

NPM version Build status

A lightweight (0.8kb) wrapper around browser storage APIs (localStorage / sessionStorage).

Features

  • namespaces to avoid key collisions
  • per-value TTL expires
  • safe API (avoids throwing errors)
  • zero dependencies
npm install localstory

Usage

const store = require('localstory')(window.localStorage, 'myNamespace', { ttl: '45m' })

store.set('foo', 'bar')
store.get('foo') // 'bar'
store.unset('foo')

store.keys() // ['foo']
store.clear()

Namespacing

Use namespaces to freely use IDs without fear of collisions between different applications/stores in the same domain.

const horses = localstory(window.localStorage, 'horses')
const ponies = localstory(window.localStorage, 'ponies')

horses.set('name', 'Thunder')
ponies.get('name') // undefined

ponies.set('name', 'Sparkle')

horses.get('name') // 'Thunder'
ponies.get('name') // 'Sparkle'

Expiry / TTL

Automatically expire values. Expiry is validated on reads and once on load (can be disabled with vacuum: false). Each key can have it's own expiry time.

To enable, provide { ttl: [milliseconds] } as second parameter to the set() method:

store.set('foo', 'bar', { ttl: 1000 * 60 * 60 /* 1 hour */ })

store.get('foo') // 'bar'
store.get('foo') // one hour later... undefined

// time parameter takes human readable strings: [s]econds, [m]inutes, [h]ours, [d]ays
store.set('foo', 'bar', { ttl: '1h' })

store.vacuum() // removes all expired keys

// disable automatic vacuum on startup
const store = localstory(localStorage, 'namespace', { vacuum: false })

// set custom delay (in ms) for vacuum on startup
const store = localstory(localStorage, 'namespace', { vacuum: 15000 })

Tests

Uses the tape test runner. Run tests with npm test after installing dependencies.

Contributing

https://github.com/ricardobeat/localstory

Made by Ricardo Tomasi

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.