npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

di-fu

v0.1.14

Published

A simple dependency injector

Downloads

12

Readme

DI Fu

A small dependency injection framework.

Features

  • Simple fluent interface for binding to objects, modules and 'glob' paths of modules.
  • Loads configuration from a 'difile.js' module in the current folder or higher. Cascades from higher level files down.
  • Paths are relative to where the container is accessed.
  • 'get' components (also module default), 'invoke' functions or 'create' object instances.
  • 'rebind' calls can be used for overriding bindings for use cases such as mocking.
  • A 'require' method provides a get-or-require mode for an injection style closer to require.
  • Detects circular dependencies.

Installation

npm install di-fu

Example 1 - Bind to object

// in difile.js
var di = require('di-fu')();

di.bind('hello').to({
	hello: function() { return "hello world"; }
});

// in module
var di = require('di-fu')();

var hello = di('hello');
console.log(hello.hello()); // hello world

Example 2 - bind to file.

// in difile.js
var di = require('di-fu')();

di.bind('hello').file('./lib/hello-service', 'service'); // chain .load() after this to load it here.

// in module
var di = require('di-fu')();

var hello = di('hello');
console.log(hello.hello());

Example 3 - bind to path.

// in difile.js
var di = require('di-fu')();

di.bind('model:*').path('./model/model-*.js'); // chain .load() after this to load them here.

// in module
var di = require('di-fu')();

var user = di.create('model:user');

Example 4 - bind, rebind and unbind

var di = require('di-fu')();
console.log(di.has('foo')); // false

// A basic bind call to register a component.
di.bind('foo').to({
	hello: function() { return "hello world"; }
});
console.log(di.has('foo')); // true

// The module defaults to a 'get' function. You can 
// call di.get explicitly if you prefer.
var foo = di('foo');
console.log(foo.hello()); // hello world

// Call rebind to register a component without
// causing an error - e.g. for mocking.
di.rebind('foo').to({
	hello: function() { return "goodbye"; }
});
var foo = di.get('foo');
console.log(foo.hello()); // goodbye

// Components can be unbound if desired.
di.unbind('foo');
console.log(di.has('foo')); // false

Example 5 - invoking

var di = require('di-fu')();
console.log(di.has('foo')); // false

// Bind a simple function.
di.bind('foo').to(function(name) {
	return "Hello " + name; 
});
console.log(di.has('foo')); // true

// Invoke the function.
console.log(di.invoke('foo', ['Bob'])); // Hello Bob

// Bind a simple class.
di.bind('Bar').to(function(name) {
	var _this = this;
	var _name = name;

	_this.hello = function() {
		return "Hello " + _name;
	}
});
console.log(di.has('Bar')); // true

// Create an instance of the class and call it.
var bar = di.create('Bar', ['Bob']);
console.log(bar.hello()); // Hello Bob

Example 5 - require

var di = require('di-fu')();

// Use the require method to get a component if a binding
// is found but fallback to loading a module if it is not.
var svc = di.require('./foo').service;
console.log(svc.hello()); // hello world

// If you have a binding, it is returned instead of loading
// a module. This is a slightly lighter way of using the 
// dependency injector, however it requires using module
// paths as keys.
di.bind('./foo').to({
    service : {
        hello : function() { return 'hello interceptor'; }
    }
});
var svc = di.require('./foo').service;
console.log(svc.hello()); // hello interceptor

Example 6 - pre-configure required modules.

// in difile.js
var di = require('di-fu')();

// Initialise promises (bluebird).
var Promise = require('bluebird');
Promise.longStackTraces();
di.bind('bluebird').to(Promise);

// Initialise mongoose (MongoDB).
var mongoose = require('mongoose');
Promise.promisifyAll(mongoose);
di.bind('mongoose').to(mongoose);


// in some module...
var di = require('di-fu')();

var Promise = di('bluebird');

// away we go...

Tests

npm test