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

units

v5.3.0

Published

Module-like system with two-step initialization

Downloads

205

Readme

Units

NPM Version NPM Downloads

Simple, module-like dependency injection system with two-step initialization and definable namespaces for application modules, plugins, and extensions.

Example

const Units = require('units');
const units = new Units();

class Controller {
  constructor() {
    this.db = undefined;
  }

  init({ db }) {
    // getting components we're depended on
    this.db = db;
  }
}

units.add({
  controller: new Controller()
})

units.init(); // calls init() function of every unit internally

Unit

Unit is a simple interface

class Controller {
  constructor() {
    this.db = undefined;
  }

  init({ 'db.mysql': db }) {
    this.db = db;
  }
}

Interface methods and properties

init()

Initialize all the units.

initRequired = true

The unit with this property will be initialized when required.

instance / instance()

The property or function, If present it will be called when a unit is required and returned instead of unit class itself.

Units

Units is a single class that manages all the structure. The root Units contains all the units. Child units can be added as in this example:

// this is our root unit set
const units = new Units({
  resources: {
    user: {
      api: new Api(),
      controller: new Controller()
    },
    post: {
      api: new Api(),
      controller: new Controller()
    }
  }
});

This will create units resources as a container, user, post with units api and controller. From resources.post.api you have access to all units:

class Api {
  init({ controller, 'user.controller': user }) {
    this.ctrl = controller;
    this.user = user;
  }
}

Methods

constructor(units)

If units present passes it to add method.

add()

Adds units or units sets. You can add a plain object, not Units, and it will create Units automatically. Examples:

units.add({ user: {
  api: new Api(),
  controller: new Controller()
}})

//or

units.add({ user: () => ({
  api: new Api(),
  controller: new Controller()
})})

expose(obj)

like add(obj), but init will not be called on this unit (so, a unit may omit init implementation), used to expose constant or any object without init method as a unit. If you want to expose an object when you use the add method you can add the @expose property to the object you want to expose. Example:

const units = new Units({
  constants: {
    '@expose': true,
    a: 'a',
    b: 'b'
  }
})

extend(obj)

Like expose but if unit exist just extends it with obj. If you want to extend an object when you use add method you can add @extend property to the object you want to expose. Example:

const units = new Units({
  constants: {
    '@expose': true,
    a: 'a',
    b: 'b'
  }
});

units.add({
  constants: {
    '@extend': true,
    c: 'c'
  }
})

join(units)

Adds all the units from units to self, without any extra magic

alias(aliasKey, srcKey)

Sets the alias aliasKey for unit srcKey

isEmpty()

Returns true if unit does not have child units. Otherwise returns false.

has(key)

Returns true if units exist under the key. Otherwise returns false.

get(key)

Gets unit under key specified, tries parent if no unit found and parent is present. If key omitted and units instance has representation returns it.

require(key)

Calls get internally and returns a result if not null, otherwise, throws an error

match(regexp, function)

Calls the function for every unit name that matches regexp. The first argument in the function is always the matched unit. All others are matches from the regexp.

  //example from matter-in-motion lib
  units
    .get('resources')
    .match('^(.*)\.api$', (unit, name) => console.log(name));

forEach(function)

Calls the 'function' for every unit.

  units.forEach((unit, name) => console.log(name));

init()

Calls init method on all added units

Iterable

  for (let key of units) {
    console.log(key); // will print all units keys from this unit set
  }

License MIT