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

diss

v1.0.1

Published

Dependency Injection Super Simple

Downloads

21

Readme

DI+KISS

Build Status Coverage Status

DISS is simple and convinient dependency injector for node.js. It usees similar pattern to AngularJS dependency injector.

Usage

Provider/factory/forge pattern

To use DISS you should use a provider pattern for your code.

Instead of:

var d1 = require('d1'),
    d2 = require('d2');

module.exports = {
    method: m1
}    

Use:

module.exports = function( d1, d2 ) {
    return {
        method: m1
    }
}

Quick start

main.js

var diss = require('diss')(),
    pkg = require('./package.json');
    
// automaticly load all dependencies defined in package.json and register them under their own names    
diss.loadDependencies(pkg);  

// load providers from files and register them under same names: 
diss.loadProviders(['cfg', 'logger', 'worker', 'rest']);

// register pkg as 'pkg', so it can be used by our modules
diss.register.module('pkg',pkg);

// start actual application.
diss.resolve(function(rest) {
   rest.startServer(); 
});

API

require('diss') gives you a injector provider. Call it to get an instance of incejctor. Register all your dependencies and call it's resolve on your main application provider.

resolve(provider)

  • provider: provider to resolve.

Takes a provider, examines it's signature using reflection and injects all it's depencencies. If any of said dependencies are providers themselves, it will resolve their dependencies recursively.

diss.resolve(function(mysql, logger, http) {
    // [...] 
});

require(name,[main])

  • name: string representing a name of module to load and register.
  • main: module to call require from. Defaults to require.main, which is the entry pointof application.

A convinience method, shortcut to:

diss.register.module('name', require('name') );
diss.register.module('name', main.require('name') );

loadDepencencies(pkg,[main])

  • pkg: object representing package.json, ie. require('package.json')
  • main: module to call require from. Defaults to require.main, which is the entry pointof application.

A convinience method, equivalent to iterating over all dependencies listed in package.json and calling diss.require on all of them.

diss.loadDependencies(require('package.json'));

loadProviders(providers, [main], [directory])

  • providers: array of string names of your providers.
  • main: module to call require from. Defaults to require.main, which is the entry pointof application.
  • directory: directory to load from. Defaults to current directory.

A convinience method, equivalent to call of diss.register.provider on all supplied files.

diss.loadProviders(['mod1','mod2','mod3','foo/mod4'])
diss.loadProviders(['bar/a','bar/b'],module,'./src/examlpe');

is equivalent to:

diss.register.provider('mod1', require('./mod1'));
diss.register.provider('mod2', require('./mod2'));
diss.register.provider('mod3', require('./mod3'));
diss.register.provider('fooMod4', require('./foo/mod3'));
diss.register.provider('barA', require('./src/example/bar/a'));
diss.register.provider('barB', require('./src/example/bar/b'));

register.module(name, module)

  • name: string containing name to register.
  • module: object to register under that sting.

Module is a object provied as-is to providers when resolving them. Use modules for non-DI dependencies, ie:

diss.register.module('Promise', require('bluebird') );
///[...]
diss.resolve(function( Promise ) {
    // Promise contains bluebird module
});

register.provider(name, provider)

  • name: string containing name to register.
  • provider: provider to register.

Provider is a function that has dependencies as parameters and returns an object, which is passed as dependency to other providers.

diss.register.provider('myLogger', function(genericLogger, pkg) {
    return genericLogger.createInstance({
        name: pkg.name,
        level: 'info'
    });
});

diss.resolve(function( myLogger ) {
    // myLogger here will be the result of .createInstance
});

Auto-generated names

When names are auto-generated using convinience methods, they are based on passed name and camelcased: some-module becomes someModule in dependencies.