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

@alexey.kornilov/injector

v0.3.0

Published

Dependency Injector for JavaScript projects.

Downloads

4

Readme

Injector

npm version license

This is dependency injector for JavaScript with support for ES6/ES2015. It can be applied in both node and browser environments.

Table of Contents

Installation

Execute this command in your environment.

npm install @alexey.kornilov/injector --save

or

yarn add @alexey.kornilov/injector

Table of Contents

Usage

Constructor Registration

One is able to register constructor for a particular class and create instances of it:

import Injector from "@alexey.kornilov/injector";

class Foo {
    constructor() {
        console.log("Creating instance of Foo.");
    }
    
    get dependencies() {
        return [];
    }
}

const injector = new Injector();
injector.register("foo", Foo);

// Resolve class by its registered name
const foo1 = injector.resolve("foo");

// Resolve class by its constructor reference
const foo2 = injector.resolve(Foo);

Expected output will be:

[Tue Mar 28 2017 08:39:53 GMT+0300 (MSK)] [Injector] Registering 'foo' component.
Creating instance of Foo.
Creating instance of Foo.

Table of Contents

Object Registration

One is able to register pure object.

import Injector from "@alexey.kornilov/injector";

const foo = {
    name: "Foo object " + Math.round(Math.random() * 10)
};

const injector = new Injector();
injector.register("foo", foo);

// Resolve registered object
const foo1 = injector.resolve("foo");
console.log(foo1);

// Resolve registered object
const foo2 = injector.resolve("foo");
console.log(foo2);

// We are expecting same object
console.log(foo1 === foo2 ? 
    "Same object resolved." : 
    "WARNING: unexpected resolved object.")

Expected output will be:

[Tue Mar 28 2017 08:46:31 GMT+0300 (MSK)] [Injector] Registering 'foo' component.
{ name: 'Foo object 5' }
{ name: 'Foo object 5' }
Same object resolved.

Table of Contents

Dependency Injection

Now it's time to demonstrate dependency injection.

import Injector from "@alexey.kornilov/injector";

class Foo {
    constructor() {
        console.log("Creating instance of Foo.");
    }
    
    get dependencies() {
        return [];
    }
}

class Bar {
    constructor(dependencies) {
        console.log("Creating instance of Bar.");
        Object.assign(this, dependencies);
    }
    
    get dependencies() {
        return [
            "foo"
        ];
    }
}

const injector = new Injector();
injector.register("foo", Foo);
injector.register("bar", Bar);

// Resolve class by its registered name
const bar1 = injector.resolve("bar");

// Bar contains injected foo
console.log(bar1);

// Resolve class by its constructor reference
const bar2 = injector.resolve(Bar);

// Bar contains injected foo
console.log(bar2);

Expected output will be:

[Wed Mar 29 2017 15:27:08 GMT+0300 (MSK)] [Injector] Registering 'foo' component.
[Wed Mar 29 2017 15:27:08 GMT+0300 (MSK)] [Injector] Registering 'bar' component.
Creating instance of Foo.
Creating instance of Bar.
Bar { foo: Foo {} }
Creating instance of Foo.
Creating instance of Bar.
Bar { foo: Foo {} }

Also usage examples can be found in ./examples folder.

Table of Contents