localStorage implementation for node.js
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
AJ ONeal 8c3ac848f2 v1.0.4: update links and license 6 years ago
lib moved root so that README and tests are included 11 years ago
tests should not return ls.prototype.key 11 years ago
LICENSE v1.0.4: update links and license 6 years ago
README.md v1.0.4: update links and license 6 years ago
package.json v1.0.4: update links and license 6 years ago

README.md

DOMStorage

See https://git.coolaj86.com/coolaj86/dom-storage.js for a slightly better version of the same thing.

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

null === localStorage.getItem('key');

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