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

rjstry

v1.0.9

Published

Modular object matching and extension

Downloads

6

Readme

Rjstry

Rjstry logo

modular pattern matcher designed to reduce boilerplate when matching between objects

the motivation was to create a system to handle mapping between identifiable object instances to new instances (that may be extensions of the original instance)

Similar to rust match, but with modular building blocks at runtime

Common use cases

  • Backend sends a list of ids that need to be mapped to react elements
  • Serialized game data that needs to be instantiated as game objects

Installation

npm:

npm install rjstry

yarn:

yarn add rjstry

pnpm:

pnpm add rjstry

Usage

Defining matches

the registry matches items via matcher functions, a function that receives a parameter and returns a new object if it had matched, if there is no match the matcher should return undefined so the next matcher may be activated, can also use any object with match member as matcher, see composition

import Registry from 'rjstry';

// === registry creation ===
const reg = new Registry(
	(i) => i === 42 && 'meaning of life',
	(i) => i % 2 === 0 && 'even',
	(i) => {
		return { message: 'unknown handle', origin: i };
	}
);

// === registry invocation ===
reg.match(42); // -> 'meaning of life'
reg.match(4); // -> 'even'
reg.match('test'); // -> { message: 'unknown handle', origin: 'test' }

Composition

any object with the match member may also be used as a matcher function. this enables us to compose multiple sub-registries into one root registry.

this is very powerful if you need modular registries, and also allows to segment your registry to prioritized chunks

import Registry from 'rjstry';

const reg1 = new Registry((i) => i === 5 && 'five');
const reg2 = new Registry((i) => 'unknown');

const root = new Registry(reg1, reg2);

root.match(5); // -> 'five'
root.match(6); // -> 'unknown'

reg1.add((i) => i === 6 && 'six');

root.match(6); // -> 'six'

Lookup

a utility matcher aimed to reduce boiler if need to match the same single key to identify an object

import Registry, { Lookup } from 'rjstry';

const lookup = new Lookup(x => x.id, {
	a: 'aa',
	b: 'bb',
	c: (item) => { ...item, c: 'cc' },
	d: (item, context, ...parameters) => { ...item, d: 'dd', onClick: () => { context.doSomething() }, parameters },
});

lookup.match({ id: 'a' })
// -> 'aa'
lookup.match({ id: 'c' })
// -> { id: 'c', c: 'cc' }

// lookup can also be used as a matcher in the registry
const root = new Registry(lookup);

root.match({ id: 'b' });
// -> 'bb'

Modularity

registries are built to be modular, you can dynamically add or remove registry matchers, the following example is in react but can be used with the same lifecycle methodology

registry.js:

import Registry from 'rjstry';

// this will be the root of the registry
// modular registries will mount onto it like building blocks
const registry = new Registry();

export default registry;

RegistryProvider.jsx:

import Registry, { Lookup } from 'rjstry';
import registry from './registry';

const RegistryProvider = ({ children }) => {
	// on mount
	useEffect(() => {
		const undo = registry.add(
			(i) => i > 10 && 'big',
			new Registry(
				(i) => i < 0 && 'negative',
				(i) => i === 0 && 'zero'
			)
		);

		// cleanup on unmount
		return () => {
			undo();
		};
	}, []);

	return children;
};

Demo