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

javascript-data-store

v1.0.3

Published

JSDS is a small and fast data store.

Downloads

9

Readme

JSDS

JSDS is a small and fast data store.

var store = JSDS.create('store-1');

store.set('cars', {
    toyota: ['prius', 'avalanche'],
    honda: ['accord', 'odyssey'],
    ford: ['taurus', 'fairlane', 'F-150']
});

store.get('cars.toyota')[1];
// 'avalanche'

// you can add to an existing array using an update option
store.set('cars.toyota', ['matrix'], {update: true});
store.get('cars.toyota');
// ['prius', 'avalanche', 'matrix']

// add to the structure however you like
store.set('cars.stats', {
    sold: 242,
    inventory: 4456
});

// add to any existing branches on the fly
store.set('cars.stats.ordered', 30);
// { sold: 242, inventory: 4456, ordered: 30 }

It allows you to listen for storage events on any branch or leaf within the storage structure.

var handle = store.after('set', 'cars.stats.ordered', function(result) {
    // cars.stats.ordered was just set... what are you going to do about it?
};

store.set('cars.stats.ordered', 30);
store.get('cars.stats.ordered');
// the answer is 60 because the listener above doubled it

// remove the listener
handle.remove();

store.set('cars.stats.ordered', 30);
store.get('cars.stats.ordered');
// the answer is 30 again. that's better.

// (oh yeah, and you can use wildcards)
handle = store.after('set', 'cars.stats.ordered.*', function(k, v) {
    // will execute any time any property is set onto cars.ordered
});

store.set('cars.stats.blue', 345);
store.get('cars.stats.blue');
// undefined ;)
store.get('cars.stats.blue-foo');
// 6900

// back to normal
handler.remove();

API

JSDS has a few static functions for creating and managing stores, which are the only objects that get created by the JSDS library. Here are some of the static functions:

  • JSDS.create(id): Returns new instance of JSDS data store object with the given id. If id is omitted, a random id is assigned.
  • JSDS.get(id): Returns an existing JSDS data store with the given id. If a store with the given id does not exist, returns undefined
  • JSDS.clear(): Calls remove on all JSDS data stores in memory and resets to en empty list of stores
  • JSDS.count(): Returns the current number of JSDS data stores that have been created in memory

Each JSDS data store object created will have the following instance properties:

  • id: The id of the store
  • set(key, value): Stores the given value for the given key
  • get(key): Retrieves the value for given key, or undefined if it doesn't exist
  • after(event, [optional] key, callback, scope): Will call the function registered with the result of the action, after action has been taken. Returns an handler object with a remove() function, which will remove the listener.
  • clear(): Removes all stored data from the store
  • remove(): Removes all stored data from the store and deletes store reference within JSDS (for full deletion, any outside references must also be deleted)

Build

npm start

This will build a webpack bundle in docs/

Test

npm test

Hopefully they all pass.

AJAX Use Case

When you make an ajax call, no matter what JavaScript library you might be using, you will supply a callback function that gets executed when the HTTP response is received. Usually, the response object or the data within it is passed directly into your callback, where the callback logic can handle this data.

But sometimes you might not want to couple a response-handling function to logic that fires an ajax request. JSDS provides a generic way to simply cache the data returned in the response so code existing elsewhere can use it when it's ready.