midge
Version:
Mini Injector
101 lines (65 loc) • 1.95 kB
Markdown
Mini Injector
[](https://travis-ci.org/platdesign/midge)
`npm install --save midge`
```javascript
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(a + b);
expect(d).to.equal(b + 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(a + b);
expect(d).to.equal(b + 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(a + b);
expect(d).to.equal(b + c);
});
```
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](https://twitter.com/platdesign)