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

just-another-di

v0.1.4

Published

Just another dependency injection solution for JavaScript

Downloads

7

Readme

just-another-di

npm version build status code coverage

Just another one implementation of di container for Javascript - TypeScript / Node.js (v6)

Features

Installing

Using npm:

$ npm install just-another-di

Using yarn:

$ yarn add just-another-di

Usage examples

NodeJS

const Container = require('just-another-di').Container;
function FooClass (arg, arg2) {
    this.arg = arg;
    this.arg2 = arg2;
}

// Initialize new Container object
const di = new Container;
di.set('my.var', 'My awesome string');
di.get('my.var'); // Will return 'My awesome string' value

let factoryCounter = 0;
di.set('fooObj', function (c) {
    return new FooClass('Counter = ' + (factoryCounter++), c.get('my.var'))
}, {createEveryTime: false}); // Default value for createEveryTime option
// If option createEveryTime will be true,
// di.get('fooObj') will return new result of function call

di.get('fooObj'); // Will return object#1 
// instanceof FooClass with props:
// {arg: 'Counter = 0', arg2: 'My awesome string'}
di.get('fooObj', true); // Will return object#2
// instanceof FooClass with props:
// {arg: 'Counter = 1', arg2: 'My awesome string'}
di.get('fooObj'); // Will return object#2
// instanceof FooClass with props:
// {arg: 'Counter = 1', arg2: 'My awesome string'}
// Because container memorize only last result of creation

Typescript

import {Container} from 'just-another-di';

class FooClass {
    private arg: any;
    private arg2: any;

    constructor(arg: any, arg2: any) {
        this.arg = arg;
        this.arg2 = arg2;
    }
}

// Initialize new Container object
const di = new Container;
di.set('my.var', 'My awesome string');
di.get('my.var'); // Will return 'My awesome string' value

let factoryCounter = 0;
di.set('fooObj', function (c: Container) {
    return new FooClass('Counter = ' + (factoryCounter++), c.get('my.var'))
}, {createEveryTime: false}); // Default value for createEveryTime option
// If option createEveryTime will be true,
// di.get('fooObj') will return new result of function call

di.get('fooObj'); // Will return object#1 
// instanceof FooClass with props:
// {arg: 'Counter = 0', arg2: 'My awesome string'}
di.set('fooObj', true); // Will return object#2
// instanceof FooClass with props:
// {arg: 'Counter = 1', arg2: 'My awesome string'}
di.get('fooObj'); // Will return object#2
// instanceof FooClass with props:
// {arg: 'Counter = 1', arg2: 'My awesome string'}
// Because container memorize only last result of creation

API description

Module returns Container class, that must be initiated for usage

Javascript:

const Container = require('just-another-di').Container;
const di = new Container;

Typescript:

import Container from 'just-another-di';
const di = new Container;

Next you can use on of following methods to define values:

set( name: string, value: any, options?: {createEveryTime: false} ) - define factory lambda function, for object initialization. Container will execute function on demand (di.get(name)), memorize result of execution (createEveryTime !== true), then will return execution result; if typeof value is scalar Container will return it unchanged on demand;

Semver

Until di reaches a 1.0 release, breaking changes will be released with a new minor version.

License

MIT