3.0.0 • Published 6 years ago

another-di v3.0.0

Weekly downloads
25
License
MIT
Repository
-
Last release
6 years ago

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'
});
3.0.0

6 years ago

2.0.0

6 years ago

1.0.0

9 years ago