lmaccherone / node-localstorage

A drop-in substitute for the browser native localStorage API that runs on node.js.
MIT License
450 stars 36 forks source link

CI

Code QL

node-localstorage sponsor

Copyright (c) 2012, Lawrence S. Maccherone, Jr.

A drop-in substitute for the browser native localStorage API that runs on node.js.

Fully implements the localStorage specfication including:

Limitations:

Credits

Author: Larry Maccherone

Usage

CoffeeScript

unless localStorage?
  {LocalStorage} = require('../')  # require('node-localstorage') for you
  localStorage = new LocalStorage('./scratch')

localStorage.setItem('myFirstKey', 'myFirstValue')
console.log(localStorage.getItem('myFirstKey'))
# myFirstValue

localStorage._deleteLocation()  # cleans up ./scratch created during doctest

ReactJs

Open or create src/setupTests.js and add these two lines:

// /src/setupTests.js
import { LocalStorage } from "node-localstorage";

global.localStorage = new LocalStorage('./scratch');

JavaScript

if (typeof localStorage === "undefined" || localStorage === null) {
  var LocalStorage = require('node-localstorage').LocalStorage;
  localStorage = new LocalStorage('./scratch');
}

localStorage.setItem('myFirstKey', 'myFirstValue');
console.log(localStorage.getItem('myFirstKey'));

Polyfill on Node.js

Polyfil your node.js environment with this as the global localStorage when launching your own code

node -r node-localstorage/register my-code.js

Installation

npm install node-localstorage

Changelog