dom-storage.js/README.md

94 lines
2.1 KiB
Markdown
Raw Permalink Normal View History

2013-07-28 05:13:26 +00:00
sessionStorage & localStorage for NodeJS
2011-07-22 19:21:23 +00:00
===
2018-03-28 05:15:41 +00:00
| **dom-storage**
| [atob](https://git.coolaj86.com/coolaj86/atob.js)
| [btoa](https://git.coolaj86.com/coolaj86/btoa.js)
| [unibabel.js](https://git.coolaj86.com/coolaj86/unibabel.js)
| Sponsored by [ppl](https://ppl.family)
2012-08-16 19:15:28 +00:00
An inefficient, but as W3C-compliant as possible using only pure JavaScript, `DOMStorage` implementation.
2011-07-22 19:21:23 +00:00
Purpose
----
This is meant for the purpose of being able to run unit-tests and such for browser-y modules in node.
Usage
----
2014-03-01 16:19:38 +00:00
```javascript
2018-03-28 05:15:41 +00:00
var Storage = require('dom-storage');
2018-03-28 05:15:41 +00:00
// in-file, doesn't call `String(val)` on values (default)
var localStorage = new Storage('./db.json', { strict: false, ws: ' ' });
2018-03-28 05:15:41 +00:00
// in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'`
var sessionStorage = new Storage(null, { strict: true });
2018-03-28 05:15:41 +00:00
var myValue = { foo: 'bar', baz: 'quux' };
2014-03-01 16:19:38 +00:00
localStorage.setItem('myKey', myValue);
myValue = localStorage.getItem('myKey');
2011-07-22 19:21:23 +00:00
2014-03-01 16:19:38 +00:00
// use JSON to stringify / parse when using strict w3c compliance
sessionStorage.setItem('myKey', JSON.stringify(myValue));
myValue = JSON.parse(localStorage.getItem('myKey'));
```
2011-07-22 19:21:23 +00:00
API
---
2011-07-22 19:21:23 +00:00
* 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 `' '`.
2011-07-22 19:21:23 +00:00
Tests
---
2011-07-22 19:21:23 +00:00
2014-03-01 16:19:38 +00:00
```javascript
0 === localStorage.length;
2016-03-03 15:51:10 +00:00
null === localStorage.getItem('doesn\'t exist');
undefined === localStorage['doesn\'t exist'];
2011-07-22 19:21:23 +00:00
2014-03-01 16:19:38 +00:00
localStorage.setItem('myItem');
2016-03-03 15:51:10 +00:00
'undefined' === localStorage.getItem('myItem');
2014-03-01 16:19:38 +00:00
1 === localStorage.length;
2011-07-22 19:21:23 +00:00
2014-03-01 16:19:38 +00:00
localStorage.setItem('myItem', 0);
2016-03-03 15:51:10 +00:00
'0' === localStorage.getItem('myItem');
2011-07-22 19:21:23 +00:00
2014-03-01 16:19:38 +00:00
localStorage.removeItem('myItem', 0);
0 === localStorage.length;
2011-07-22 19:21:23 +00:00
2014-03-01 16:19:38 +00:00
localStorage.clear();
0 === localStorage.length;
```
2011-07-22 19:21:23 +00:00
2012-08-16 19:15:28 +00:00
Notes
2011-07-22 19:21:23 +00:00
---
2012-08-16 19:15:28 +00:00
* db is read in synchronously
* No callback when db is saved
* Doesn't not emit `Storage` events (not sure how to do)
2015-02-08 15:30:48 +00:00
License
-------
2018-03-28 05:15:41 +00:00
Code copyright 2012-2018 AJ ONeal
Dual-licensed MIT and Apache-2.0
Docs copyright 2012-2018 AJ ONeal
Docs released under Creative Commons.