2013-01-26 09:07:18 +00:00
|
|
|
# DOMStorage
|
|
|
|
|
2018-10-10 22:51:23 +00:00
|
|
|
See https://git.coolaj86.com/coolaj86/dom-storage.js for a slightly better version of the same thing.
|
2013-01-26 09:07:18 +00:00
|
|
|
|
|
|
|
# localStorage
|
2011-07-22 19:21:23 +00:00
|
|
|
|
2012-08-16 19:18:48 +00:00
|
|
|
An inefficient, but as W3C-compliant as possible using only pure JavaScript, `localStorage` implementation.
|
2011-07-22 19:21:23 +00:00
|
|
|
|
2013-01-26 09:07:18 +00:00
|
|
|
## Purpose
|
2011-07-22 21:17:18 +00:00
|
|
|
|
|
|
|
This is meant for the purpose of being able to run unit-tests and such for browser-y modules in node.
|
|
|
|
|
2013-01-26 09:07:18 +00:00
|
|
|
## Usage
|
2011-07-22 21:17:18 +00:00
|
|
|
|
2012-08-16 19:18:48 +00:00
|
|
|
var localStorage = require('localStorage')
|
2011-07-22 21:17:18 +00:00
|
|
|
, myValue = { foo: 'bar', baz: 'quux' }
|
|
|
|
;
|
|
|
|
|
|
|
|
localStorage.setItem('myKey', JSON.stringify(myValue));
|
|
|
|
myValue = localStorage.getItem('myKey');
|
2011-07-22 19:21:23 +00:00
|
|
|
|
2013-01-26 09:07:18 +00:00
|
|
|
## API
|
2011-07-22 19:21:23 +00:00
|
|
|
|
|
|
|
* getItem(key)
|
|
|
|
* setItem(key, value)
|
|
|
|
* removeItem(key)
|
|
|
|
* clear()
|
|
|
|
* key(n)
|
|
|
|
* length
|
|
|
|
|
2013-01-26 09:07:18 +00:00
|
|
|
## Tests
|
|
|
|
|
|
|
|
null === localStorage.getItem('key');
|
2011-07-22 19:21:23 +00:00
|
|
|
|
|
|
|
0 === localStorage.length;
|
|
|
|
null === localStorage.getItem('doesn't exist');
|
|
|
|
undefined === localStorage['doesn't exist'];
|
|
|
|
|
|
|
|
localStorage.setItem('myItem');
|
|
|
|
"undefined" === localStorage.getItem('myItem');
|
|
|
|
1 === localStorage.length;
|
|
|
|
|
|
|
|
localStorage.setItem('myItem', 0);
|
|
|
|
"0" === localStorage.getItem('myItem');
|
|
|
|
|
|
|
|
localStorage.removeItem('myItem', 0);
|
|
|
|
0 === localStorage.length;
|
|
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
0 === localStorage.length;
|
|
|
|
|
2012-08-16 19:18:48 +00:00
|
|
|
TODO / Bugs
|
2011-07-22 19:21:23 +00:00
|
|
|
---
|
|
|
|
|
2012-08-16 19:18:48 +00:00
|
|
|
* Does not persist.
|
|
|
|
* could use `fs.readFileSync` at load and an occasional `fs.writeFile` to write-out localStorage.json
|
|
|
|
* Doesn't not emit `Storage` events
|