sessionStorage & localStorage for NodeJS
| dom-storage | atob | btoa | unibabel.js | Sponsored by ppl
An inefficient, but as W3C-compliant as possible using only pure JavaScript, DOMStorage
implementation.
Purpose
This is meant for the purpose of being able to run unit-tests and such for browser-y modules in node.
Usage
var Storage = ; // in-file, doesn't call `String(val)` on values (default)var localStorage = './db.json' strict: false ws: ' ' ; // in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'`var sessionStorage = null strict: true ; var myValue = foo: 'bar' baz: 'quux' ; localStorage;myValue = localStorage; // use JSON to stringify / parse when using strict w3c compliancesessionStorage;myValue = JSON;
API
- getItem(key)
- setItem(key, value)
- removeItem(key)
- clear()
- key(n)
- length
Options
- strict - whether to stringify strictly as text
[Object object]
or as json{ foo: bar }
. - ws - the whitespace to use saving json to disk. Defaults to
' '
.
Tests
0 === localStoragelength;null === localStorage;undefined === localStorage'doesn\'t exist'; localStorage;'undefined' === localStorage;1 === localStoragelength; localStorage;'0' === localStorage; localStorage;0 === localStoragelength; localStorageclear;0 === localStoragelength;
Notes
- db is read in synchronously
- No callback when db is saved
- Doesn't not emit
Storage
events (not sure how to do)
License
Code copyright 2012-2018 AJ ONeal
Dual-licensed MIT and Apache-2.0
Docs copyright 2012-2018 AJ ONeal
Docs released under Creative Commons.