localStorage implementation for node.js
Go to file
AJ ONeal a0cd89a7c7 should not return ls.prototype.key 2013-01-25 21:12:55 -07:00
lib x in obj -> obj.hasOwnProperty(x) 2013-01-25 21:11:48 -07:00
tests should not return ls.prototype.key 2013-01-25 21:12:55 -07:00
README.md Revert "separated from localStorage" 2012-08-16 13:18:48 -06:00

README.md

localStorage

An inefficient, but as W3C-compliant as possible using only pure JavaScript, localStorage 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 localStorage = require('localStorage')
  , myValue = { foo: 'bar', baz: 'quux' }
  ;

localStorage.setItem('myKey', JSON.stringify(myValue));
myValue = localStorage.getItem('myKey');

API

  • getItem(key)
  • setItem(key, value)
  • removeItem(key)
  • clear()
  • key(n)
  • length

Tests

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;

TODO / Bugs

  • 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