npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-store

v0.0.11

Published

simple store creation utils

Downloads

9

Readme

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;