another-di

3.0.0 • Public • Published

another-di

This is another dependency injector, because there aren't enough.

Usage

// lib/hello-message.js
 
module.exports = function () {
  return 'hello world!';
}
 
// lib/boomer.js
 
module.exports = function ({helloMessage}) {
  return {
    boom: function () {
      console.log(helloMessage);
    }
  }
}
 
// lib/index.js
 
var di = require('another-di');
 
module.exports = di.create()
.singleton('helloMessage', require('./hello-message')) // dashes converted to camelcase
.singleton('boomer', require('./boomer'))
 
// main.js
 
var lib = require('./lib');
 
lib.run(function ({boomer}) {
  boomer.boom(); // logs out 'hello world'
});
 
 
// test.js
 
var lib = require('./lib');
var boomer;
 
beforeEach(function () {
  testContainer = lib.clone();
  testContainer.singleton('helloMessage', function () {
    return 'hello from test land';
  });
 
  testContainer.run(function ({_boomer}) { // underscore is striped before dependency lookup
    boomer = _boomer;
  })
})
 
it('should boom!', () => {
  boomer.boom(); // logs 'hello from test land'
});
 

/another-di/

    Package Sidebar

    Install

    npm i another-di

    Weekly Downloads

    61

    Version

    3.0.0

    License

    MIT

    Unpacked Size

    5.42 kB

    Total Files

    4

    Last publish

    Collaborators

    • loke