Universal LocalStorage ponyfill library for Browser and Node.js.
Install with npm:
npm install localstorage-ponyfill
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");
Native localStorage.
import { createLocalStorage } from "localstorage-ponyfill";
const localStorage = createLocalStorage({ mode : "browser" });
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 localStorage work on Node.js and Browser.
import { createLocalStorage } from "localstorage-ponyfill";
const localStorage = createLocalStorage({ mode : "memory" });
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;
}
If you want to get IndexedDB storage, please use azu/kvs.
See Releases page.
Install devDependencies and Run npm test
:
npm i -d && npm test
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
MIT © azu