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

jetter-set

v0.1.7

Published

Reactive little objects

Downloads

10

Readme

JetterSet

JetterSet is a micro-library for making JavaScript objects reactive. It's (naturally!) declarative, memoizes derived/computed values, and provides callback hooks for handling property changes.

Effectively, this:

myObj.apples = 3;
myObj.bananas = 1;

// assuming `fruit` is a computed property of apples + bananas …
console.log(myObj.fruit); // 4

myObj.bananas = 3;

console.log(myObj.fruit); // 6

JetterSet is < 1k (and < 500 bytes Gzipped) with 0 dependencies.

Installation and Usage

npm install jetter-set
import { jetterSet } from 'jetter-set';

…

const newReactiveObject = jetterSet({ ...defaultValues })

Setting Values

Treat your handily reactive JetterSet object as a standard JavaScript object. Properties get accessed and assigned normally -- no special getter or setter methods.

const obj = jetterSet({
  apples: 3,
  pears: 2,
});

obj.oranges = 5;
obj.bananas = 2;
delete obj.apples;

console.log(Object.keys(obj)); // pears, oranges, bananas

Deriving Values | derive

One of JetterSet's neatest features is deriving values from existing properties. Do this via the derive method.

const obj = jetterSet({ apples: 3, pears: 2 });
obj.derive('fruit', (obj) => obj.apples + obj.pears);

console.log(obj.fruit); // 5

Derived (or computed, if you prefer) values change when upstream values change.

const obj = jetterSet({ apples: 3, pears: 2 });
obj.derive('fruit', (obj) => obj.apples + obj.pears);

console.log(obj.fruit); // 5

obj.apples = 100;

console.log(obj.fruit); // 102

Memoization of Derived/Computed Values

When upstream signals change, JetterSet memoizes derived values. Reactive functions only runs once -- even when deriving values from other derived values -- and the result gets stashed in an intermediary cache until signaling values change again.

Watching Values | onChange

You can also watch for updates on your JetterSet's properties. This is done via the onChange method.

const obj = jetterSet({ apples: 3, pears: 2 });
obj.onChange('apples', (newVal, oldVal, updatedObj) => {
  if (newVal < 2) {
    console.log(`Buy more apples! You had ${oldVal} but are now down to ${newVal}`);
  }
});

obj.apples = 1; // "Buy more apples! You had 3 but are now down to 1"

Naturally, you can watch for changes on derived values, too.

const obj = jetterSet({ apples: 3, pears: 2 });

obj.derive('fruit', (obj) => obj.apples + obj.pears);
obj.onChange('fruit', (newVal, oldVal, updatedObj) => {
  console.log(`You had ${oldVal} apples and pears; now you have ${newVal}`);
});

obj.apples = 1; // "You had 5 apples and pears; now you have 3"

Unwatching Values | offChange

To remove a properties watcher, use the offChange method. As with all handlers/listeners of this nature, passing named or referenced functions is gonna help.

const obj = jetterSet({ apples: 3, pears: 2 });
const changeHandler = () => console.log('Changed!');

obj.onChange('apples', changeHandler);
obj.apples = 99; // "Changed!"

obj.offChange('apples', changeHandler);
obj.apples = 0; //

Inspiration

This project modernizes my original JetSet micro-library, which I wrote as a code golf state management solution. JetterSet (a better JetSet) trades a few bytes for robustness and an improved feature set.