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

kioc

v1.0.4

Published

The simplest IOC container you will find for node. It let's you do the work of inventing patterns on top of it.

Downloads

10

Readme

build status

NPM

kioc

The simplest IOC container you will find for node.
It let's you do the work of inventing patterns on top of it.

Installation

npm install --save kioc

Methods

.set(key, value)

Registers a value on the container.

.use(component, singleton=false)

Registers a component on the container.

A component should be an object (of any kind) that contains the following properties:

  • attachKey The key wherewith you later retrieve the component from the container.
  • attach A function that returns the resolved component
    To this function, the container instance gets passed in as the first argument, from which you can take dependencies for the component.

.has(key)

Check for the existence of a value or component on the container.

.get(key)

Retrieve a value or component from the container.

.setup()

Call setup on every registered component (only if available). You can call this after registering all components, and give the components a chance to setup necesarry stuff. The return value of this method is an array of results from the calls to the setup methods.

Example

Examples can be found in the examples/ folder. To run the examples, execute node examples/main.js from this project's root.

Read the ... source!

It's so simple, you might as well read the source to see how it works.

'use strict';

/**
 * A super simple IOC container.
 *
 * @class Container
 * @param {Object} props
 */
function Container(props) {
  // add props passed in via the constructor to "this"
  Object.keys(props || {}).forEach(function (key) {
    this.set(key, props[key]);
  }.bind(this));

  // initialize the component map
  this.components = {};
}

/**
 * Register a component in the container.
 *
 * @param {*} component
 */
Container.prototype.use = function (component) {
  this.components[component.attachKey] = component;
};

/**
 * Set any key on the container.
 *
 * @param {String} key
 * @param {*} value
 */
Container.prototype.set = function (key, value) {
  this[key] = value;
};

/**
 * Check if key is registered in the container
 *
 * @param {String} key
 */
Container.prototype.has = function (key) {
  return this.hasOwnProperty(key) || this.components.hasOwnProperty(key);
};

/**
 * Get a component from the container.
 *
 * @param {String} key
 * @returns {*} The instatiated component
 */
Container.prototype.get = function (key) {
  // let the developer know what he is looking for is not available
  if ( ! this.has(key)) {
    throw new Error(key + ' component not available on the container!');
  }

  var component = this.components[key];
  return component ? component.attach(this) : this[key];
};

/**
 * Call setup (if available) on every registered component.
 *
 * @return {Array} An array of the setup call results
 */
Container.prototype.setup = function () {
  var app = this;

  var results = [];
  Object.keys(this.components).forEach(function (key) {
    var component = app.components[key];
    if (component.setup) {
      results.push(component.setup(app));
    }
  });
  return results;
};

module.exports = Container;