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

js-di

v0.2.0

Published

Simple Dependency Injection for Javascript. Supports multiple containers with container inheritance

Downloads

6

Readme

js-di

A simple Dependency-Injection system which supports multiple containers and container inheritance

getting the code

install from npm: npm install js-di

use in your project: var injector = require('js-di');

running the tests

Install the dependencies using npm install

Run the tests using npm test

Transpile to ES5 using npm run build

Build the docs using npm run doc

Generate the docs for the readme with npm run build-readme

API Reference

Injector

injector.register(modules, container) ⇒ Container

Registers the modules in the specified container. If no container is specified, uses the root container.

Kind: instance method of injector
Returns: Container - the container

| Param | Type | Description | | --- | --- | --- | | modules | Object | a key-value pair of modules to register | | container | Container | optional, a container instance in which the modules should be registered. |

injector.invoke(module, container) ⇒ *

Invoke a function through the DI system. Dependencies will automatically be applied as arguments to the function. Will attempt to resolve dependencies from the specified container (if provided) and will recursively look through parent containers if the dependencies cannot be met

Kind: instance method of injector
Returns: * - The return value of the invoked function

| Param | Type | Description | | --- | --- | --- | | module | function | a function to invoke. | | container | Container | optional, a container instance from which the dependencies should be injected |

injector.getInstance(key, container) ⇒ *

Manually get an registered instance from the specified container (if provided) or from the root container.

Kind: instance method of injector
Returns: * - the registered module

| Param | Type | Description | | --- | --- | --- | | key | String | the key of the registered module to get | | container | Container | optional, a container instance in which to look. If unspecified, will use the root container. |

injector.getContainer(id) ⇒ Container

if an id is passed, returns the matching container, or null if no id is passed, returns the root container

Kind: instance method of injector
Returns: Container - the specified container

| Param | Type | Description | | --- | --- | --- | | id | String | optional the id of the container to get |

injector.createContainer(id, parent) ⇒ Container

Creates a new Container with the specified ID. If parent is a string, it will use the container corresponding to that id. If the id does not match any containers, it will throw an exception. If parent is not a string, it checks to make sure the specified argument has an id property and that a container with this id already exists. Otherwise, it will throw an exception. If parent is unspecified, the root container will be set as the parent

Kind: instance method of injector
Returns: Container - the new container

| Param | Type | Description | | --- | --- | --- | | id | String | the id of the container to create | | parent | Container | String | the container to set as the new container's parent, or an id corresponding to the container to set as the parent |

Container

Kind: global class

container.register(modules) ⇒ Container

Registers one or more modules within the current container. Modules can be specified in any order. If a module being registered in a child container requires a module that does not exist in the current container, it will recursively look through parent containers until it finds it UNLESS a new instance of that dependency is being registered as part of this call to .register()

Circular dependencies are caught and an exception is thrown.

Kind: instance method of Container
Returns: Container - the container (for chaining)

| Param | Type | Description | | --- | --- | --- | | modules | Object | a map of modules to register, where the key is the id with which to register the module, and the value is the invocable function which will return the instance to store |

container.get(key)

Get a module from this container with the specified key. If it does not exist in the container, recursively search through parent containers. If it has not been registered in any ancestor, return null

Kind: instance method of Container

| Param | Type | Description | | --- | --- | --- | | key | String | the key corresponding to the dependency to get |

container.invoke(module) ⇒ *

Invoke a function through the DI system. Dependencies will automatically be applied as arguments to the function. Will attempt to resolve dependencies from the current container (if provided) and recursively look through parent containers if the dependencies cannot be met

Kind: instance method of Container
Returns: * - The return value of the invoked function

| Param | Type | Description | | --- | --- | --- | | module | function | a function to invoke. |

Container.id

the id of the container

Kind: static property of Container
Properties

| Name | Description | | --- | --- | | id | String |