W3C DOM Storage (localStorage and sessionStorage) for node.js
Go to file
AJ ONeal 36e19f1e78 fixed same prototype bug as in localStorage 2013-01-26 02:04:32 -07:00
lib fixed same prototype bug as in localStorage 2013-01-26 02:04:32 -07:00
tests separated from localStorage 2012-08-16 13:15:28 -06:00
README.md separated from localStorage 2012-08-16 13:15:28 -06:00

README.md

DOMStorage

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 = require('dom-storage')
  , localStorage = new Storage('./db.json') // in-file
  , sessionStorage = new Storage()                // in-memory
  , 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;

Notes

  • db is read in synchronously
  • No callback when db is saved
  • Doesn't not emit Storage events (not sure how to do)