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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@halfbit/di

v1.1.0

Published

Simple but extensible dependency injection for Javascript

Downloads

11

Readme

DI.js

npm (tag)

DI.js is a simple yet extensible parameter dependency injection written in vanilla Javascript.

Usage example

Providable resources are called Parameter, an evaluation context is called Context. A Context can evaluate either a parameter name or the function which it will try to provide with all its parameters. A most basic usage would look like this:

import di from 'di';

let context = new di.Context();
context.addParameter('dog', { name: "Wooffers", bark: console.log.bind(window, 'Woof!') });

// No need for ugly ['dependency', function (dependency) {}] syntax
// Outputs 'Woof!' on the console!
context.evaluate(dog => dog.bark());

Under the hood, DI.js uses the functions toString method and some RegExp trickery to parse out all parameters. It works with named, anonymous and arrow functions, and supports default as well as rest parameters.

A caveat is that parameters with functions with parameters themselves as default parameter will not work.

Typing and custom adapters

DI.js offers type conversion via Adapters. An example would look like this:

context.addAdapter('json', JSON.stringify);

// Returns '{"name":"Woofers"}'
context.evaluate("dog_json");

An Adapter has a type that it produces and at least one transforming function.

It can also have multiple transforming functions for different input types:

context.addAdapter('str', String);
// Returns '[object Object]'
context.evaluate("dog_str");

// The third parameter is a fallback that is called if no matching input type is found
context.addAdapter('str', { 'object': JSON.stringify }, String);

// Returns '{"name":"Woofers"}'
context.evaluate("dog_str");

You might have noticed that the input type used here is 'object' and that we never provided a type together with the dog Parameter. We can actually specify our own type for Parameters to enable for even fancier Adapters:

context.addParameter('dog', { name: "Wooffers", bark: console.log.bind(window, 'Woof!') }, 'dog');
context.addParameter('neighboursCat', { name: "Spotty", meow: console.log.bind(window, 'meow~') }, 'cat');

context.addAdapter('call', { 'dog': d => d.bark, 'cat': c => c.meow });

// 'Woof!'
context.evaluate("dog_call")();
// 'meow~'
context.evaluate("neighboursCat_call")();

The default type name that is used if no type conversion is specified (f.i. context.evaluate('dog')) is 'def'. A newly created Context will always have an Adapter defined for this type that simply returns the value, i.e. a => a. However, if we override this Adapter then we can apply our own conversion, unbeknownst to the evaluated parameter or function:

context.addAdapter('def', { 'cat': a => a.name, 'dog': a => a.name }, a => a);

// 'Wooffers'
context.evaluate("dog");