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

konoha

v1.0.3

Published

A Dependency Injection Container

Downloads

11

Readme

Konoha npm version

Konoha is Dependency Injection Container that allows you take the advantage of the dependency injection principle in Javascript.

Installation

npm install --save konoha

Getting Started

First we need to create a new instace of Konoha. After that we can use methods like set and get to access it.

Using ES6
import Konoha from 'konoha';

class Application extends Konoha {
    constructor() {
        super();
        this.set('isValid', true);
        this.set('user', {
            name: 'User Name',
            permissions: []
        });
        this.set('api', function() {
            return new Api();
        });

        this.get('api').post('/dummy/data');

        ...
    }
}
Pre-ES6 (NodeJS or CommonJS)
var Konoha = require('konoha');
var app = new Konoha();

// you can chain any of the API methods
app.set('isValid', true)
    .set('user', {
        name: 'User Name',
        permissions: []
    })
    .set('api', function() {
        return new Api();
    });

app.get('api').post('/dummy/data');

API

get(name)

Gets the value by the name.

app.set('isValid', false);
app.set('foo', function() { return 'bar'; });

app.get('isValid'); // false
app.get('foo'); // 'bar'

set(name, value)

Sets a new attribute with given value.

You can set anything on the value. If the value is a function it will only be constructed/called once but it won't called until it is requested by get. The function context will be the app, so you access to the app if you need it. This shouldn't be used for setting/binding class definitions. Only the class intances should be stored.

app.set('isValid', true);
app.set('user', {
    name: 'Full Name',
    isActive: true,
    permissions: []
});

// sets an API service
app.set('api.prefix', '/api');
app.set('api', function() {
    return new Api(this.get('api.prefix'));
});

var isValid = app.get('isValid');
var user = app.get('user');

// get API service
// This will initiate a new instance of the API
var api = app.get('api');
api.get('/some/rest/api');

// This time it will use existing API instance, since the function only gets called once
var api = app.get('api');

has(name)

Checks if there is anything registered under given name.

app.has('version'); // false

app.set('version', '4.3.2');

app.has('version'); // true

keys()

Get all the registered names as an array.

app.set('version', '4.3.2');
app.set('isValid', true);

app.keys(); // ['version', 'isValid']

raw(name)

Returns the original definition of the service.

app.set('api', function() {
    return new Api();
});

var api = app.raw('api'); // the callback/function definition

extend(name, callback)

Allows you to extend/modify the registered service

Let's add a response handler to the API service that converts the response to JSON. To do that we need to pass a function that takes one argument which will be the current value of the existing service. It should return a new value that will replace the existing value.

app.set('api', function() {
    return new Api();
});

app.extend('api', function(api) {
    api.addResponseHandler(new JsonResponse());
    return api;
});

Now every response you get should be converted into JSON.

factory(name, service)

Not everytime you want to get the cached (constructed) service. Use factory to create a service that will be reconstructed/called everytime it gets requested by get.

app.factory('person', function() {
    return new Person();
});

// Creates new Person instance
var person1 = app.get('person');

// Creates new Person instance again
var person2 = app.get('person');

protect(name, value)

Unlike set or factory which always returns the value of the service/function the protect allows you define a service/function that will never called by get. It's something that is needed if you want to store something without getting modified by get. The protect will always return the definition of the service/function.

app.protect('person', function(name) {
    var person = new Person(name);
    return person;
});

app.get('person'); // will return the function that takes a name argument

var siri = app.get('person')('Siri');
console.log(siri.getName()); // 'Siri'

var alexa = app.get('person')('Alexa');
console.log(alexa.getName()); // 'Alexa'

register(provider)

This function allows you to register services in bulk. The provider function that is passed doesn't become a service. But it gets called immediately.

app.register(function() {
    this.set('logger.level', 'debug');
    this.set('logger', function() {
        return new Logger(this.get('logger.level'));
    });
});