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

suntik

v1.0.0

Published

Simple dependency injection

Downloads

1

Readme

Suntik

A minimal dependency injection for Node.Js that doesn't depend on anything else.

Installation

npm install suntik

Motivation

I have a hard time understanding dependency injection. You don't have to.

suntik is intended for learning purpose. It has less features compared to other dependency injection frameworks out there. But on the other hand, it is simple and almost un-opinionated. No high-level Javascript magic. Just classes and that's all. The core of suntik is even less than 100 lines of code (currently it is even less 50 lines).

Alternative Projects

Example

TLDR: Click this

The Container

Suppose you have 3 components, warrior, weapon, and throwableWeapon. The warrior should depend on weapon and throwableWeapon. A warrior has an attack() method.

Now you want your warrior to be a Ninja from ./ninja.js, your weapon to be Katana from ./katana.js, and your throwableWeapon to be Shuriken from ./shuriken.js. This is what you should write:

// file: index.js
const Container = require('suntik');

// set up the container
const container = new Container({
    basepath: __dirname, // this is the basepath to resolve you component files
    component: { // here are the components
        warrior: './ninja.js',
        weapon: './katana.js',
        throwableWeapon: './kunai.js',
    },
    main: 'warrior.attack' // this is the action when you cast container.main()
});

// cast warrior.attack
const warrior = container.initComponent('warrior');
warrior.attack();

// or just cast container.main()
container.main();

The Components

Ninja as warrior

// file: ninja.js
class Ninja {
    attack() {
        this.weapon.slash();
        this.throwableWeapon.throw();
    }
}
Ninja._dependencies = ['weapon', 'throwableWeapon'];

module.exports = Ninja;

The Ninja._dependencies define that the container should inject weapon and throwableWeapon to the component. If you are using typescript, you can define _dependencies as static property.

Note If you are aware, this approach is not so common, since both merapi and inversify use constructor's parameter as injection medium. The reason behind this decision is to emulate lazy loading and allow the developer to not write the constructor.

Katana as weapon

// file: katana.js
class Katana {
    slash() {
        console.log('katana slash')
    }
}

module.exports = Katana;

Shuriken as throwableWeapon

// file: shuriken.js
class Shuriken {
    throw() {
        console.log('shuriken thrown')
    }
}

module.exports = Shuriken;