Coder Social home page Coder Social logo

es6-store's Introduction

es6-store

A simple localstorage api with uses ES6 classes. This library saves data from localstorage in local variable and syncs this variable with localstorage. When you try to get data, library doesn't read localstorage it just returns data from local variable.

Install

npm i es6-store --save

How to use

Just import Store from Store.js if you use ES6

Example

import Store from 'node_module/es6-store/src/Store.js'

Just require es6-store if you use browserify

Example

var Store = require(es6-store);

Create instance of Store

Example

var store = new Store('NameOfYourStore')

API

store.set

Persists value under key in local storage. Key will be removed if value is undefined. You can use multi key!

Returns
  1. An error if key isn't string or set failed.
  2. Set value or remote value
Example
store.set('foo', 'bar');
-> 'bar'
store.set('bar.baz', 'bar');
-> 'bar'
store.getAll();
-> 
  {
    foo: 'bar',
    bar: {
      baz: 'bar'
    }
  }

store.get

Return value under key in local storage. Will be return defaultValue if key is not defined. You can use multi key!

Returns
  1. An error if key isn't string or set failed.
  2. value under key or defaultValue.
Example
store.get('foo');
-> 'bar'
store.get('bar.baz');
-> 'bar'
store.get('baz', 'bar'); // baz is not defined
-> 'bar'

store.getAll

Return local storage object.

Returns
  1. local storage object.
Example
store.getAll();
-> 
  {
    foo: 'bar',
    bar: {
      baz: 'bar'
    }
  }

store.remove

Removes key from local storage. You can use multi key!

Returns
  1. An error if key isn't string or remove failed.
  2. undefined if key is not defined.
  3. value of removed key.
Example
store.remove('foo');
-> 'bar'
store.remove('bar.baz');
-> 'bar'
store.get('baz', 'bar'); // baz is not defined
-> undefined

store.clear

Clear local storage object.

Returns
  1. undefined.

store.destroy

Remove event listener. If first argument is true, local storage item will be removed

Returns
  1. undefined.

Store.serialize

Static method. Convert from JSON to string.

Store.deserialize

Static method. Convert from string to JSON.

Store.clone

Static method. Clone object. Use JSON.stringify and JSON.parse.

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.