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 🙏

© 2025 – Pkg Stats / Ryan Hefner

simpledi

v1.2.0

Published

A simple dependency injector

Readme

SimpleDi

build dependencies

SimpleDi is a very simple dependency injector.

There's also an async version available: p7s1digital/scorpion

Features

  • Simple API
  • Fully tested
  • Tiny (1.3kb gzipped)
  • Works in any ES3 compliant environment
  • Helps you find unresolved dependencies with getResolvedDependencyCount
  • Battle-tested (used in real-world projects)

Installation

npm install simpledi

Compatibility

SimpleDi is compatible with ES3 compliant browsers

Api

di.register(dependencyName, factoryFunction[, dependencies[, overwrite]])

Parameters:

Name | Type | Description -----|------|------------ dependencyName | string | The name of the dependency factoryFunction | function | A function that gets called when get is called dependencies | array<string> | optional An array of depdendency names overwrite | boolean | optional This allows to explicitly overwrite dependencies

Adds a dependency to the registry. It throws an exception when a dependency with the same name already exists and overwrite true is not passed.

di.registerBulk(dependencies)

Parameters:

Name | Type | Description -----|------|------------ dependencies | array | An array of dependencies containing arrays each containing parameters for di.register

Example:

di.registerBulk([
    ['foo', this.always({foo: true})],
    ['Bar', this.withNew(Bar), ['foo']],
]);

di.get(name[, arg1[, arg2[, ...]]])

Parameters:

Name | Type | Description -----|------|------------ dependencyName | string | The name of the dependency arg1, arg2, ... | mixed | All args defined after the name will also be injected

Returns a previously registered dependency and resolves all dependencies.

di.getResolvedDependencyCount()

Returns an object with numbers that state how often each dependency got resolved.


// Example return value
{ Foo: 1, Bar: 1, Baz: 1 }

Built-in factory functions

SimpleDi.always(objectOrFunction)

Name | Type | Description -----|------|------------ objectOrFunction | mixed | Always returns this argument

A factory function that always returns the first argument when di.get is called.

SimpleDi.withNew(Constructor)

Name | Type | Description -----|------|------------ Constructor | function | A constructor function

When di.get is called this factory function will initialize the given constructor with new.

SimpleDi.withNewOnce(Constructor)

Name | Type | Description -----|------|------------ Constructor | function | A constructor function

When di.get is called the first time this factory function will initialize the given constructor and for all upcoming calls it will always return the same instance. You can think of it as a singleton factory.

SimpleDi.once(factory)

Name | Type | Description -----|------|------------ factory | function | A factory function

When di.get is called the first time this factory function will call the given factory and for all upcoming calls it will always return the same factory return value. You can think of it as a singleton factory.

Example 1

var SimpleDi = require('simpledi');
var di = new SimpleDi();

function Engine(config) {
    this.hp = config.hp;
    this.maxSpeed = config.maxSpeed;
}

function Car(engine) {
    this.text = 'This car has ' + engine.hp + 'hp!';
}

di.register('Engine', SimpleDi.withNew(Engine), ['engineConfig']);
di.register('engineConfig', SimpleDi.always({
    hp: 120,
    maxSpeed: 200
}));
di.register('Car', SimpleDi.withNew(Car), ['Engine']);

var car = di.get('Car');

console.log(car.text);

Example: Circular Dependency

di.register('foo', SimpleDi.always({
    foo: true
}), ['bar']);

di.register('bar', SimpleDi.always({
    bar: true
}), ['foo']);

di.get('foo'); // throws "Circular Dependency detected: foo => bar => foo"

License

The MIT License (MIT)

Copyright (c) 2015 Julian Hollmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.