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

adi

v1.0.1

Published

Another dependency injector (Or anything but)

Downloads

1,972

Readme

adi

Adi is a micro DI framework inspired by Angular, and implemented with the liberal abuse of Object.defineProperty

Features

  • Incredibly easy to use.
  • Object properties as providers.
  • Factories and services available for complex resolution.
  • Enforced constants. Optional object freeze.
  • Parses function arguments to inject providers.
  • Singleton creation by default.
  • Throws error on circular depedencies.
  • Pure javascript.
  • Method chaining.
  • Lazy.

Installation

npm install adi

How it works

Adi functions primarily by dynamically defining properties on it's own instance with getters for lazy dependency resolution. Once a depdendency is resolved, if it's a singleton, it replaces the getter so the resolution chain only needs to execute once. What this means for you? It couldn't be easier to use.

Resolution

Everything can be resolved via adi[name] or adi.name, and all properties on the instance are automatically injectable.

Yes, it's that easy.

Example:

var adi = require('adi')();
// More on factories below.
adi.factory('yourFactoryNameGoesHere', function() {
    return 4;
});
var a = adi.yourFactoryNameGoesHere; // a==4

Injection

Adi supports function argument injection. A call to adi.invoke(fn, ctx) will parse the expected arguments of fn and resolve them against the current instance before invocation.

var adi = require('adi')();
adi.someValue = 5;
adi.constant('aConstant', 6);
adi.invoke(function(someValue, aConstant) {
    // someValue == 5
    // aConstant == 6
});

Factories

Adi isn't limited to simple object properties. By defining getters, we can create complex resolvers. The following registers a function with dependencies. The singleton flag determines if the return value replaces the property accessor or if we'd like to invoke this factory each time it's resolved. The singleton flag is optional and defaults to true.

adi.factory('yourFactoryNameGoesHere', function(allYourDepedencies, commaSeperated) {
    return 4;
}, singletonFlag);

adi.invoke(function(yourFactoryNameGoesHere) {
    // yourFactoryNameGoesHere == 4
});

Services

Similar to factories, Adi provides a service method that instantiates a constructor when resolved. Similar to factory, the singleton flag determines if we replace the property accessor or create a new object on each resolution.

adi.service('yourServiceName', function(anyDepedencies) {
    this.x = 5;
}, singletonFlag);
console.log(adi.yourServiceName.x); // outputs 5.

Run blocks

Adi also provides a way to register code to run once wiring is complete. There is no guarantee on execution order, but it will be in the order they were registered.

adi.run(function(youKnowTheDrillByNow) {
    // do stuff
});
// call with no arguments to execute.
adi.run(); 

Special properties

Adi currently supports two special properties. module.exports.$adi and adi.$adi.

module.exports.$adi is a shared Adi instance on the module.

adi.$adi is a constant reference to the current instance and can be injected like any other property.

It's recommended you avoid these, but they're there if you need them.

Examples

var adi = require('adi')();
adi
	.value('value1', 1)
	.value('value2', {x: 2})
	.value('value3', 'three')
	.constant('const1', 1)
    .constant('const2', 'two')
    .constant('const3', {y: 'three'}, true) // freeze object
	.factory('exampleFactory', function(value1, const3) {
		return 'anything';
	})
	.service('exampleService', function(exampleFactory) {
		// exampleFactory === 'anything'
		this.y = 4;
	}, false) // Not a singleton.
	.run(function(exampleService, exampleFactory, $adi, suppliedAfter) {
		// suppliedAfter = 5
		// exampleFactory == anything
		console.log(exampleService, exampleFactory, suppliedAfter);
	});
adi.suppliedAfter = 5;

adi.run(); // Invoke registered run blocks (call run() with zero arguments)

Tests

npm test

Release History

  • 1.0.0