New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

js-store

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-store

simple store creation utils

0.0.11
latest
Source
npm
Version published
Weekly downloads
1
-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

js-store

Build Status

utilities for simple javascript stores

install

npm install --save js-store

usage

Simple Store

var jsStore = require('js-store');

var myStore = jsStore.createStore({foo: 'bar'});
myStore.setState({baz: 'qux'});
myStore.getState(); // {foo: 'bar', baz: 'qux'});

var cb = function() {
  // pull data from store using getState
}

myStore.addChangeListener(cb);
myStore.setState({other: 'value'}); // cb will fire
myStore.removeChangeListener(cb);
myStore.setState({other: 'another value'}); // cb will not fire

Store Collections

var jsStore = require('js-store');

var TeamsStore = jsStore.createCollectionStore('teams');

TeamsStore.getState(); // returns {teams: []}
TeamsStore.setState({teams: [
  {id: 1, name: 'a'},
  {id: 2, name: 'b'},
  {id: 3, name: 'c'}
]});

TeamsStore.findById(2); // returns {id: 2, name: 'b'}

Store collections have a few helper methods to make it easier to work with the collection:

// returns the object with an id of 2, or undefined
// pass an optional second arg with a true to convert the first arg to an integer)
TeamStore.findById(2)
// where obj.id is a unique identifier, or obj is the value of an id
// returns the index of the object, or -1 if not found
TeamStore.findIndex(obj)
// adds an item to the collection
TeamStore.add(item)
// replaces existing item (by team.id)
// returns the updated item or undefined if the item.id doesn't match
TeamStore.replace(item)
// add or replaces existing item (by team.id)
// returns the new item
TeamStore.upsert(item)
// replace the existing item that has the given id
// returns the id if successfully destroy, or undefined
TeamStore.destory(id)

Stores that sync with localStorage

var jsStore = require('js-store');

var TeamsStore = jsStore.createCollectionStore('teams', true);
// changes to TeamsStore's state will now write to localStorage
// TeamStore's state will also start with state from localstorage

Syncing with local storage is also available for simple stores. To do so pass along a string key to the createStore method.

var jsStore = require('js-store');

var myStore = jsStore.createStore({}, 'myStore');

To make singleton stores for your client side app, simply export a creaetd store from a module

var jsStore = require('js-store');

var UserStore = jsStore.createStore({}, 'user');
module.exports = UserStore;

Keywords

store

FAQs

Package last updated on 08 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts