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

@basd/registry

v0.0.9

Published

A simple yet powerful utility to manage and create class instances in JS applications.

Downloads

50

Readme

Registry

npm pipeline license downloads

Gitlab Github Twitter Discord

A simple yet powerful utility to manage and create class instances in JavaScript applications. This registry allows you to define, override, and retrieve classes with ease. It also comes with built-in validation checks to ensure the integrity of the classes you're working with.

Features

  • Define classes once and access them globally.
  • Create instances of classes with runtime validation checks.
  • Support for deep-nesting of classes.
  • Override existing class definitions.

Installation

Install the package with:

npm install @basd/registry

Usage

First, import the Registry library.

import Registry from '@basd/registry'

or

const Registry = require('@basd/registry')

Then use the registry to set, validate and instantiate some class:

const Registry = require('@basd/registry')
const MyClass = require('./MyClass')

const registry = new Registry()

// Add MyClass to the registry
registry.set('MyClass', MyClass)

// Create a new instance
const instance = registry.createInstance('MyClass')

// Validate class and instance
if (registry.isValidClass(MyClass) && registry.isValidInstance(instance)) {
  console.log('Valid')
}

Documentation

API

constructor(config)

Initialize the Registry class with an optional configuration.

const Registry = require('@basd/registry')
const registry = new Registry({
  classes: {
    'MyClass': MyClassDefinition
  }
})

createInstance(name, args, baseClass)

Create an instance of a class with a given name. Optionally, specify a baseClass to validate against.

const instance = registry.createInstance('MyClass', [arg1, arg2], MyBaseClass)

isValidClass(targetClass, baseClass)

Validate whether targetClass is a valid class optionally against a baseClass.

isValidInstance(instance, baseClass)

Check if instance is a valid object and optionally belongs to a class derived from baseClass.

ingest(source)

Ingest classes from another source.

registry.ingest(anotherRegistry)

setWith(name, targetClass, baseClass, override = false)

Set a class with additional validation checks. Optionally, specify a baseClass and if the class can be overridden.

registry.setWith('MyClass', MyClass, MyBaseClass, true)

set(name, targetClass, override = true)

Set a class in the registry. Optionally, allow it to be overridden.

registry.set('MyClass', MyClass)

setMany(classes, override = false)

Set multiple classes at once.

registry.setMany({
  'MyClass1': MyClass1,
  'MyClass2': MyClass2
})

get(name, defaultValue = null)

Get a class from the registry by its name. If not found, return defaultValue.

const MyClass = registry.get('MyClass')

has(name)

Check if the registry contains a class with the specified name.

const exists = registry.has('MyClass')

static get(config)

A static method to get an existing registry instance or create a new one.

const registry = Registry.get(existingConfig)

Tests

In order to run the test suite, simply clone the repository and install its dependencies:

git clone https://gitlab.com/frenware/utils/registry.git
cd registry
npm install

To run the tests:

npm test

Contributing

Thank you! Please see our contributing guidelines for details.

Donations

If you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you!

Bitcoin (BTC):

1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdF

Monero (XMR):

46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQ

License

@basd/registry is MIT licensed.