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

minidi

v1.1.0

Published

Minimal, convention-based, inversion-of-control/dependency injection for Node

Downloads

7

Readme

minidi

Minimal, convention-based, inversion-of-control/dependency injection for Node.js. Originally written for Appraise.

Designed to work with minimal impact on client code and make unit testing easy.

Features

  • loads components lazily/on demand
  • easy, convention based access to components -- just ask for a property passed into the constructor
  • minimal impact on your code -- requires components only to have a specific constructor/function parameters
  • no external dependencies
  • production-ready, tested
  • supports any Node module as a component, including NPM/Node packages and local files
  • supports primitive objects as components

Limitations

  • all components are initialised at most once
  • does not support cyclic dependencies

Install from NPM

npm inistall minidi

Usage

For a quick example, check out the Example Project. Run node main.js in the example directory to see it in action.

Component syntax

Each component should have a constructor with a single argument, a key-value map for the configuration. The component can access other components or any generic configuration passed to the component builder simply by accessing properties of the configuration. MiniDi will dynamically instantiate components as they are needed.

For unit testing purposes, instantiate the component class directly and pass mock collaborators or test stubs into the second argument.

module.exports = class Sender {
  // constructor for components receives all other components
	constructor(config) { 
    // just ask for a component by name, it will be lazily-initialised if required
		this.receiver = config.receiver; 
	}
	send(message) {
		this.receiver.receive(message);
	}
};

Initialising the container

Create a new instance of minidi and pass in two arguments: component objects and component modules.

  • component objects are literal values (eg config properties, strings, arrays or objects you already initialised)
  • component modules are a key-value map of component names to module names. You can use Node module names or file paths. If you want to use local files instead of package dependencies/Node module names, remember to use __dirname to ensure that local paths are resolved based on the current file, for example path.join(__dirname, 'sender').

Just access any component directly as a property of the container instance, using the name from the components map.

const MiniDi = require('minidi'),
  path = require('path'),
  // static properties to pass into components
  config = { 
    name: 'Tom' 
  },
  // dynamic components to instantiate
  modules = {
    //keys are component names
    //values are module names or local file paths with
    sender: 'some-node-module',
    receiver: path.join(__dirname, 'receiver')
  },
  minidi = new MiniDi(config, modules);

minidi.singer.sing(/*... */); // just access a component by property name

Author

Gojko Adzic

License

MIT, see the License