📦 stoor
Local and Session storage wrapper with support for namespacing and multi get/set and remove
Table of Contents
About
This module is a small wrapper (Less than 700 bytes) around the localStorage and session that provides:
- Parsing and stringification of values
- Plugable fallback (defaults to in memory)
- Namespacing
- Multi get, set & remove of values
Install
With package manager:
$ npm install --save stoor
# OR
$ yarn add stoor
Or with CDN:
<script src="https://unpkg.com/stoor/dist/stoor.umd.js"></script>
Usage
This example shows the entire API in use:
var things = new Stoor({ namespace: 'things' }) // Namespaced to things and uses local storage
var otherThings = new Stoor({ namespace: 'otherThings', storage: 'session' }) // Namespaced to other things and uses Session storage
things.set('foo', 1)
things.set('bar', 2)
things.set('baz', { foo: 4, baz: 4 })
console.log(things.get('baz')) // {foo: 4, baz: 4}
console.log(otherThings.get('baz')) // null
console.log(things.get(['foo', 'bar'])) // [1, 2]
things.remove(['foo', 'bar'])
console.log(things.get(['foo', 'bar'])) // [null, null]
otherThings.set([['bar', 5], ['foo', 6]]) // Array of key value pairs to multi set
console.log(otherThings.get(['foo', 'bar'])) // [6, 5]
things.clear()
You can configure any module that conforms to the the localStorage/sessionStorage API to be the fallback, for example using cookie-session-storage:
new Stoor({fallback: cookieSessionStorage})
// Use as normal
Contribute
Contributions are welcome. Please open up an issue or create PR if you would like to help out.
License
Licensed under the MIT License.