easy-inject

0.0.4 • Public • Published

Easy Inject

A very simple dependency injection library, inspired and mostly borrowed from Angular.

Installation

npm install --save easy-inject

Usage

Just like in Angular, there are two main ways to use the injection service. One is by declaring them as arguments in your function call. Easy Inject will then parse the argument names and supply the desired dependencies.

var inject = require('easy-inject');
 
inject(
    function(foo, bar) {
        console.log(foo, bar); // outputs "Hello world!"
    },
    {
        foo: 'Hello ',
        bar: 'world!'
    }
);

Or, you may supply an explicit array of dependencies, and they'll be passed to the function in the same order as specified.

inject(
   ['foo', 'bar', function(a, b) {
       console.log(a, b); // outputs "Hello world!"
   }],
   {
       foo: 'Hello ',
       bar: 'world!'
   }
);

API

Easy Inject exports one simple function which takes two arguments. The first is the function descriptor, which can be either a function, or an explicit array with values of the dependencies and the last element as a function.

The second argument is a dictionary of injectable dependencies.

When using inject, it will annotate the function with it's required dependencies, and it will resolve them by looking up the name in the dictionary of injectable dependencies.

If the dependency cannot be found, an error will be thrown.

Todo

  • Unit tests

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.4
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.0.4
    1
  • 0.0.3
    0
  • 0.0.2
    0
  • 0.0.1
    0

Package Sidebar

Install

npm i easy-inject

Weekly Downloads

1

Version

0.0.4

License

ISC

Last publish

Collaborators

  • leahcimic