GithubHelp home page GithubHelp logo

localstorage-ponyfill's Introduction

localstorage-ponyfill Actions Status: test

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;
}

IndexedDB

If you want to get IndexedDB storage, please use azu/kvs.

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

localstorage-ponyfill's People

Contributors

azu avatar greggb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

localstorage-ponyfill's Issues

TypeError: Path must be a string. Received undefined

When i try to use localstorage in my isomorphic app:

const localStoraged = createLocalStorage();
localStorage.setItem("key", "12321321");

In sever console is:


path.js:7
    throw new TypeError('Path must be a string. Received ' + inspect(path));
    ^

TypeError: Path must be a string. Received undefined
    at assertPath (path.js:7:11)
    at Object.dirname (path.js:695:5)
    at resolve (path\to\project\server.js:65280:22)
    at module.exports.module.exports (path\to\project\server.js:6
5187:20)
    at Object.<anonymous> (path\to\project\server.js:65174:18)
    at Object.<anonymous> (path\to\project\server.js:65175:30)
    at __webpack_require__ (path\to\project\server.js:21:30)
    at createLocalStorage (path\to\project\server.js:65147:23)
    at Object.<anonymous> (path\to\project\server.js:6184:102)
    at __webpack_require__ (path\to\project\server.js:21:30)

Searching for this issue shows widespread problems with different libraries where path strings are being asserted instead of returning empty string if null. Suggestions were to downgrade to Node 5 or 4 from 6 (I do not wish to so this).

NPM version:
3.10.8
Node version:
v6.9.1
Node architecture:
x64
OS:
Windows 7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.