midge

0.2.0 • Public • Published

midge

Mini Injector

Build Status

Install

npm install --save midge

Usage

const Midge = require('midge');
 
const cache = {
    a: 123,
    b: 321
};
 
const factories = {
    c: function(a, b) {
        return a + b;
    },
    d: function(b, c) {
        return b + c;
    }
};
 
// Create injector instance
const injector = Midge(cache, function factory(name) {
    return injector.invoke(factories[name]);
});
 
 
// Get results
let a = injector.get('a');
let b = injector.get('b');
let c = injector.get('c');
let d = injector.get('d');
 
expect(a).to.equal(123);
expect(b).to.equal(321);
expect(c).to.equal(+ b);
expect(d).to.equal(+ c);
 
 
// Injecting into functions
injector.invoke(function(a,b,c,d) {
    expect(a).to.equal(123);
    expect(b).to.equal(321);
    expect(c).to.equal(+ b);
    expect(d).to.equal(+ c);
});
 
 
// Injecting into constructors
injector.instantiate(function(a,b,c,d) {
    expect(this).to.be.an.object();
    expect(a).to.equal(123);
    expect(b).to.equal(321);
    expect(c).to.equal(+ b);
    expect(d).to.equal(+ c);
});
 

API

Injector methods

injector.get(name)

Returns instance(s) from cache or creates it/them by executing the injector factory.

  • name - A string or an array of strings with service-names.

injector.invoke(fn, [self], [locals])

Injects the given function while executing it with apply.

  • fn - Function to inject.
  • self - Object which will be bound to fn as this.
  • locals - Optional object containing values, which will be used before before cache and factory.

injector.instantiate(ctor, [locals])

Injects the given constructor while instantiating it with new

  • ctor - Constructor to inject.
  • locals - Optional object containing values, which will be used before before cache and factory.

Author

Twitter: @platdesign

Package Sidebar

Install

npm i midge

Weekly Downloads

4

Version

0.2.0

License

none

Last publish

Collaborators

  • platdesign