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

cheapstate

v1.0.0

Published

A localStorage pubSub pattern for the masses

Downloads

97

Readme

CheapState

A namespaceable localStorage pub/sub utility for the masses.

Event-driven, persistent state management built on top of localStorage and sessionStorage.

Examples

Instantiantiating:

const pointsStorage = new CheapState('points');
pointsStorage.set('paceaux', 10);

Optionally, instantiate with sessionStorage:

const pointsStorage = new CheapState('points', 'session');

Saving & Getting items


pointsStorage.set('frank', 10);
pointsStorage.get('frank');// 10

Saving an object


pointsStorage.setObject({
    'frank': 10,
    'joe': 20,
    'sally': 30
});
pointsStorage.get('frank'); // 10

Saving a map

const points = new Map([
    ['frank', 10],
    ['joe', 20],
    ['sally', 30]
]);

pointsStorage.setObject(points);
pointsStorage.get('frank'); // 10

Deleting and Clearing

Delete an item

pointsStorage.delete('sally');

Clearing the storage

pointsStorage.clear();

Adding & subscribing

const badgeEls = document.querySelectorAll('.badge');
badgeEls.forEach((badgeEl) => {
    updateBadge(badgeEl)

    // add a subscriber
    pointsStorage.subscribe((payload) => {
        // does the key match this element's key?
        if (payload.key === badgeEl.dataset.key) {
            // update it!
            updateBadge(badgeEl);
        }
    });
});

API

CheapState

Global Class

new CheapState(namespace, type)

Parameters

| name | type | Description | | --- |--- | --- | | namespace | string | the namespaces that goes with the CheapState class | | type | string | local or session | either local or session storage. Defaults to local. |

Static Methods

getNameSpacedKeyName(namespace, keyname)

Parameters

| name | type | Description | | --- |--- | --- | | namespace | string | the namespaces that goes with the CheapState class | | keyname | string | the keyname to be namespaced |

Returns

string with with <namespace>.<keyname>.

convertValue(value)

Makes a value safe to be stored in localStorage.

Parameters

| name | type | Description | | --- |--- | --- | | value | any | the value to be converted to a string |

Returns

string version of the value.

unconvertValue(value)

Converts a string into a JavaScript value;

Parameters

| name | type | Description | | --- |--- | --- | | value | any | the value to be converted from a string |

Returns

any version of the value.

registerNamespace(namespace, storage)

Adds a namespace to storage

| name | type | Description | | --- |--- | --- | | namespace | string | Adds a namespace to storage | | storage | Storage | either the localStorage or sessionStorage object |

Instance Members

namespace

string the namespace for the CheapState instance.

observers

Function[] a list of the observers for the CheapState instance.

namespaces

string[] a list of the namespaces in storage.

storage

Storage either localStorage or sessionStorage.

items

Map<'string', any> all of the items in storage for the given namespace.

size

number the number of items in storage for the given namespace.

length

number the number of items in storage for the given namespace. (alias for size)

Instance Methods

hasNamespace(namespace)

Determines if a namespace already exists

Parameters

| name | type | Description | | --- |--- | --- | | namespace | string | the namespace to check |

Returns

boolean if the namespace exists.

set(key, value)

Sets an item into storage

Parameters

| name | type | Description | | --- |--- | --- | | key | string | the key to set | | value | any | the value to set |

setObject(dataObject)

Sets an object's keys and values into storage.

Parameters

| name | type | Description | | --- |--- | --- | | dataObject | Object|Map|Set|Array | an object to be serialized and stored |

get(key)

Gets an item from storage.

Parameters

| name | type | Description | | --- |--- | --- | | key | string | an unnamespaced key name |

Returns

any the value of the key.

has(key)

Determines if an item exists in storage.

Parameters

| name | type | Description | | --- |--- | --- | | key | string | an unnamespaced key name |

Returns

boolean whether the key exists.

delete(key)

Deletes an item from storage.

Parameters

| name | type | Description | | --- |--- | --- | | key | string | an unnamespaced key name |

clear()

Deletes all items in the namespaced storage.

Parameters

| name | type | Description | | --- |--- | --- | | key | string | an unnamespaced key name |

subscribe(observable)

Adds a function to observables; allows it to receive a payload when storage changes

Parameters

| name | type | Description | | --- |--- | --- | | observable | Function | a function that should fire when a change happens to storage |

unsubscribe(observable)

Removes a function from observables

| name | type | Description | | --- |--- | --- | | observable | Function | a function to remove |

notify(data)

Sends a payload to the observer

| name | type | Description | | --- |--- | --- | | data | any | a message to send when a change happens. It's sent to all observers. |