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

injector-js

v0.2.1

Published

Simple Dependency Injection Container for JavaScript

Downloads

6

Readme

InjectorJS

InjectorJS is a simple Dependency Injection Container for JavaScript. It helps you to remove hard-corded dependencies and make it possible to change them at any time. For more information about this pattern look at:

  • http://en.wikipedia.org/wiki/Dependency_injection
  • http://en.wikipedia.org/wiki/Inversion_of_control
  • http://www.youtube.com/watch?v=IKD2-MAkXyQ

Documentation

This docs describe many features of the library, but not all. Use source to learn InjectorJS better.

###Installation###

For installation read related chapter.

###Usage###

Online working version of fol;owing examples is available on plunker: http://plnkr.co/edit/SDXdO5. Feel free to play around with it!

Definition of simple objects (parameters):

console.log(injector.has('name')); // false

injector.set('name', 'Bob');
injector.set('PI', 3.14).set('currentTime', new Date());

console.log(injector.has('name')); // true

Defition of objects by factory functions:

injector.set('auto', function() {
  return {
      color: this.get('auto_color'),
      type:  this.get('auto_type')
});

Factory functions has access to current injector making it possible to references to other objects (inject dependencies). Objects are created only when you require them. These cause to that order of the definitions does not matter and there is no performance penalty.

You can define several objects in one set method call:

injector.set({
    auto_color: function() {
        return Math.random() < 0.5 ? 'black' : 'white';
    },
    auto_type: function() {
        return Math.random() < 0.5 ? 'hatchback' : 'sedan';
    },
    auto: function() {
        return {
            color: this.get('auto_color'),
            type:  this.get('auto_type')
        };
    }
});

You can add object factory to injector to simplify creation of common type objects. Before creation object value will be passed to appropriate object factory:

injector.setFactory('x60', function(value) {
    return value*60;
});

injector.set('hours_in_minutes', 'x60', function() {
    return 5;
});

console.log(injector.get('hours_in_minutes')); // 300

injector.setFactory('join', {
    create: function(strings) {
        return strings.join(' ');
    }
});

injector.set('hello', 'join', ['hello', 'world', '!']);

console.log(injector.get('hello')); // hello world !

As you see object factory can be a function or an object with create method. By default there are 3 object factories:

  • Object Factory - default factory - just return object;
  • Service Factory - create instances of classes;
  • Clazz Factory - create clazzes (see ClazzJS)
injector.set({
    service: {
        person: function() {
            return {
                class: this.get('person_class'),
                init:  [this.get('person_age')],
                call: {
                    setName: [this.get('person_name')]
                }
            }
        }
    },
    clazz: {
        person_class2: function() {
            return {
                name:   'Some/Clazz/Name',
                parent: 'Some/Clazz/Parent',
                deps: [10,20,30]
            }
        }
    },
    person_class: function() {
        
        var Person = function(age) {
            this.name = undefined;
            this.age  = age;
        };
        Person.prototype.setName = function(name) {
            this.name = name;
        }
        
        return Person;
    },
    person_age: 10,
    person_name: 'Bob'
});

var person = injector.get('person');

console.log(person.name); // 'Bob'
console.log(person.age);  // 10

Dependencies

  • ClazzJS (https://github.com/alexpods/ClazzJS)

License

Copyright (c) 2013 Aleksey Podskrebyshev. Licensed under the MIT license.

githalytics.com alpha

Bitdeli Badge