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

hypodermic-js

v0.1.1

Published

a dependency injection library for javascript

Downloads

1

Readme

Hypodermic

A portable dependency injection library for JavaScript.

Installing

To use this library in you project with npm

npm install hypodermic-js

and you can import it as a module with

require('hypodermic-js')

Or simply download from github and include ./dist/hypodermic.js into your scripts.

Registering Dependencies

Hypodermic allows you to register either a class, factory or value. Registered dependencies get created as a singleton instance, persisting state across many resolution contexts.

Registering a class:

Registering a class stores a new'd instance of the class.

    function MyClass(){}

    //creates and stores a new instance of MyClass as 'RegistrationName'
    Hypodermic.registerProvider({
        provide: 'RegistrationName',
        useClass: MyClass
    });

You can also register a class by default by passing only the class

    function MyClass(){}
    
    //creates and stores a new instance MyClass as 'MyClass'
    Hypodermic.registerProvider(MyClass);

Registering a factory:

Registering a factory executes the factory function and stores the result under the registered name.

    Hypodermic.registerProvider({
        provide: 'CreatedObject',
        useFactory: function(){
            if(config.production){
                return new ProductionLogger();
            }
            else{
                return new NullLogger();
            }
        }
    })

Registering a value:

You can also register static values.

    var configData = {
        environment: 'test',
        key: '3DFA-121A-DDE4'
    }
    
    Hypodermic.registerProvider({
        provide: 'config',
        useValue: configData
    })

Registering a dependency with dependencies

Registered classes and factories can be instantiated with other dependencies that are registered in hypodermic

In this example we have already regisered a provider called 'UserRepository'

    function UserService(userRepository){
        this.userRepository = userRepository;
    }
    
    //Assuming we have already registered UserRepository we can specify the deps property
    //and the UserService will be new'd with that dependency injected into the constructor
    Hypodermic.registerProvider({ 
        provide: 'UserService', 
        useClass: UserService, 
        deps:['UserRepository']
    })

Resolving Dependencies

Resolving objects

    //Assuming we have already registered a class called UserService
    var service = Hypodermic.resolve('UserService')

Injecting into function

    //Assuming we have already registered a class called UserService
    Hypodermic.inject(['UserService'], function(service){
        //service is the registered instance of UserService because of inject
        service.getData();
    })

Contributing

If you have any ideas or find any issues, leave an entry in the issues tab of Github. If you would like to contribute, fork, and make a pull request, add a good description outlining your changes and I will review it ASAP. Make sure to add unit tests around your contributions.