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

@morglod/di-ioc

v1.0.2

Published

No decorators, no metadata Clean Dependency Injection <sub><sup>*even no changes in original code*</sup></sub>

Downloads

5

Readme

Clean DIIoC

No decorators, no metadata
Clean Dependency Injection
even no changes in original code

  • Everything is typed
  • Context supported
  • Tags supported

Example

We got interfaces

interface Warrior {
    fight(): string;
    sneak(): string;
}

interface Weapon {
    hit(): string;
}

interface ThrowableWeapon {
    throw(): string;
}

Append with

const di = new ContainerDI<{
    warrior: Warrior,
    weapon: Weapon,
    throwableWeapon: ThrowableWeapon
}>();

We got implementations

class Katana implements Weapon {
    public hit() {
        return "cut!";
    }
}

export class Shuriken implements ThrowableWeapon {
    public throw() {
        return "hit!";
    }
}

export class Ninja implements Warrior {
    private _katana: Weapon;
    private _shuriken: ThrowableWeapon;

    constructor(
        katana: Weapon,
        shuriken: ThrowableWeapon,
    ) {
        this._katana = katana;
        this._shuriken = shuriken;
    }

    public fight() { return this._katana.hit(); }
    public sneak() { return this._shuriken.throw(); }
}

Append with

di.setCtor('weapon', Katana);
di.setCtor('throwableWeapon', Shuriken);

di.set('warrior', () => new Ninja(
    di.pick('weapon'),
    di.pick('throwableWeapon')
));

Now use

const warrior = di.create('warrior');

console.log(warrior.sneak(), warrior.fight());

Singletone

// declare
interface ILogger {
    log(msg: string): void;
}

const di = new ContainerDI<{
    logger: ILogger,
}>();

// bind implementation
class Logger implements ILogger {
    log(msg: string): void {
        console.log(msg);
    }
}
di.set('logger', () => new Logger);

// use it as singletone

const logger = di.singletone('logger');
logger.log('hello');

const logger = di.singletone('logger');
logger.log('world');

OOP version

@morglod/di-ioc/lib/oop

  • Decorators
  • Everything is typed
  • Context supported
  • Tags supported

Example

Define interfaces

export interface Weapon {
    hit(): string;
}

export const Weapon = diBase<Weapon>();

Implementation

@diImpl(Weapon)
export class Katana {
    hit() {
        return 'cut!';
    }
}

Main

const weapon = diPick(Weapon);
weapon.hit() === 'cut!';

Example with tags

Implementation

@diImpl(Weapon)
export class Katana {
    hit() {
        return 'cut!';
    }
}

@diImpl(Weapon, 'mock')
export class FakeKatana {
    hit() {
        return 'fake cut!';
    }
}

Main

// Pick mock version
const fakeWeapon = diPick(Weapon, 'mock');

// Set fallback tag when no tag specified
diSetDefaultTag(Weapon, 'mock');
const fakeWeapon = diPick(Weapon);

// Everytime use tag=mock, even if other tag specified
diSetForceTag(Weapon, 'mock');
const fakeWeapon = diPick(Weapon, 'anyTag');