2012-08-16 19:15:28 +00:00
|
|
|
DOMStorage
|
2011-07-22 19:21:23 +00:00
|
|
|
===
|
|
|
|
|
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
|
|
|
|
2011-07-22 21:17:18 +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
|
|
|
|
----
|
|
|
|
|
2012-08-16 19:15:28 +00:00
|
|
|
var Storage = require('dom-storage')
|
|
|
|
, localStorage = new Storage('./db.json') // in-file
|
|
|
|
, sessionStorage = new Storage() // in-memory
|
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
|
|
|
|
|
|
|
API
|
2011-07-22 21:17:18 +00:00
|
|
|
---
|
2011-07-22 19:21:23 +00:00
|
|
|
|
|
|
|
* getItem(key)
|
|
|
|
* setItem(key, value)
|
|
|
|
* removeItem(key)
|
|
|
|
* clear()
|
|
|
|
* key(n)
|
|
|
|
* length
|
|
|
|
|
|
|
|
Tests
|
2011-07-22 21:17:18 +00:00
|
|
|
---
|
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: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)
|