Universal LocalStorage for browser and Node.js.
Clone or download
Latest commit 5251a3a Dec 29, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src refactor(pkg): split module to "browser" and "node" Dec 23, 2018
test refactor(pkg): split module to "browser" and "node" Dec 23, 2018
.gitignore initial commit Aug 18, 2017
.travis.yml initial commit Aug 18, 2017
LICENSE initial commit Aug 18, 2017
README.md Update README.md Dec 29, 2018
package.json 1.0.2 Dec 23, 2018
tsconfig.json initial commit Aug 18, 2017
yarn.lock refactor(pkg): split module to "browser" and "node" Dec 23, 2018

README.md

localstorage-ponyfill Build Status

Universal LocalStorage ponyfill library for Browser and Node.js.

Install

Install with npm:

npm install localstorage-ponyfill

Usage

Auto(Browser or Node.js)

Automatically select mode.

  • If the environment defined window.localStorage -> "browser"
  • else -> "node"
import { createLocalStorage } from "localstorage-ponyfill";
const localStorage = createLocalStorage();
localStorage.setItem("key", "value");
const value = localStorage.getItem("key");
assert.strictEqual(value, "value");        

Browser

Native localStorage.

import { createLocalStorage } from "localstorage-ponyfill";
const localStorage = createLocalStorage({ mode : "browser" });

Node.js

Use node-localstorage

Store data to <app-root>/.cache/localstorage-ponyfill/* by default.

import { createLocalStorage } from "localstorage-ponyfill";
const localStorage = createLocalStorage({ mode : "node" });

You can setting the path by storeFilePath option.

import { createLocalStorage } from "localstorage-ponyfill";
const localStorage = createLocalStorage({ mode : "node", storeFilePath: "./path/to/dir" });

InMemory

InMemory localStorage work on Node.js and Browser.

Use localstorage-memory

import { createLocalStorage } from "localstorage-ponyfill";
const localStorage = createLocalStorage({ mode : "memory" });

API

Same with Window.localStorage

export interface LocalStoragePonyfill {
    readonly length: number;

    clear(): void;

    getItem(key: string): string | null;

    key(index: number): string | null;

    removeItem(key: string): void;

    setItem(key: string, data: string): void;

    [key: string]: any;

    [index: number]: string;
}

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm i -d && npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu