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

dependencyjs

v2.0.11-1

Published

<h1 align=center> <img alt="dependencyjs" src="https://user-images.githubusercontent.com/19161678/53150421-8f99b700-35d6-11e9-9db4-e7fb4a72c5fe.png"> </h1>

Downloads

279

Readme

DependencyJS

Dependency injection framework for Typescript and Javascript

What is dependencyjs?

Dependencyjs is an dependency framework for javascript and typescript applications. This framework will help building independent components in application. So it is going to be useful if you want to achieve a well maintainable and testable application

Installation

Node.js

dependencyjs is available on npm. To install it, type:

$ npm install dependencyjs

Browsers

You can also use it within the browser; install via npm and use the ComponentRegistry.js file found within the download. For example:

<script src="./node_modules/dependencyjs/src/ComponentRegistry.js"></script>

For JavaScript;

DIC.registry.register(BaseSample, sample);

DIC.registry.resolve(BaseSample);

Limitations: loading configurations dynamically from other files is not suppoerted on the browser. It must be bundled together (using browserify or webpack).

Usage

You can register base classes with their implementation. Lets take an example of nodejs application

below is the base class

export abstract class BaseSample{
    public abstract ShowSample(name: string): string;
}

now create a concrete implementation of BaseSample class

export class Sample extends BaseSample{
    ShowSample(name: string): string{
        return "Test Name is :"+ name;
    }
}

Now register class with its concrete implementation

import {ComponentRegistry, registry} from "dependencyjs";
import {BaseSample} from "./BaseSample";
import {Sample} from "./Sample";

//register class object
let sample: BaseSample = new Sample();
registry.register<BaseSample>(BaseSample, sample);

Resolve class object to use it anywhere in code

let sample: BaseSample= registry.resolve<BaseSample>(BaseSample);

Using resolver

Dependencies can be registered with extra parameter so that we can have multiple implementation of base class registered in registry.

lets extend same Base sample in another class and register both of them in registry

export class SampleTypeSecond extends BaseSample{
    ShowSample(name: string): string{
        return "Test Name is :"+ name;
    }
}

Now register both of them in registry

import {ComponentRegistry, registry} from "dependencyjs";
import {BaseSample} from "./BaseSample";
import {Sample} from "./Sample";
import {Sample} from "./SampleTypeSecond";

//register class object
let sample: BaseSample = new Sample();
let secondSample: BaseSample = new SampleTypeSecond();

registry.register<BaseSample>(BaseSample, sample, "sample");
registry.register<BaseSample>(BaseSample, secondSample, "secondSample");

Resolve class object with resolver in your code

let sample: BaseSample= registry.resolve<BaseSample>(BaseSample, "sample");
let sampleSecond: BaseSample= registry.resolve<BaseSample>(BaseSample, "secondSample");

Loading dependencies from configuration

dependencyjs provide a functionality where you can load classes dynamically from json configurations.

How to configure json


{
  "container": {
    "register": [
      {
        "base": {
          "typeName": "<BaseClassType>",
          "sourceType": "<fs or package>",
          "sourceInfo": "<location of file or package name>"
        },
        "resolver": "<any string to identify class>",
        "map": {
          "typeName": "<Concrete implementation class name>",
          "sourceType": "<fs or package>",
          "sourceInfo": "<location of file or package name>"
        }
      },
	  {
        "base": {
          "typeName": "<BaseClassType>",
          "sourceType": "<fs or package>",
          "sourceInfo": "<location of file or package name>"
        },
        "resolver": "<any string to identify class>",
        "map": {
          "typeName": "<Concrete implementation class name>",
          "sourceType": "<fs or package>",
          "sourceInfo": "<location of file or package name>"
        }
      }
    ]
  }
}

Details of each field

1. Container: Container can have multiple class registration.
2. Register: In register, you can mention detail about base class and child class to register the dependency.
  a. base:
      typeName  : This will contain name of the exported base class. In our example about, it could be "BaseSample"
      sourceType: You can use a base class from a node package or it could exist in your local system. So there are two possible
                  values -> "fs" or "package"
      sourceInfo: If sourceType is package then this field will contain name of the package. If source type is "fs" then
                  this field will contain localtion of your file which contains base class. For example, location of Sample
                  class is "/BaseSample".
  b. map
      typeName  : This will contain name of the exported concrete implementation class. In our example, it could be "Sample".
      sourceType: You can use a concrete class from a node package or it could exist in your local system. So there are two possible
                  values -> "fs" or "package"
      sourceInfo: If sourceType is package then this field will contain name of the package. If source type is "fs" then
                  this field will contain localtion of your file which contains concrete class. For example, location of Sample
                  class is "/Sample".
  c. resolver   : If a base class have multiple implementation and you want to get any kind of implementation at run time, you can use
                  resolver. This will help you to identify classes uniquely.

Here is example of one json configuration

{
  "container": {
    "register": [
      {
        "base": {
          "typeName": "BaseSample",
          "sourceType": "fs",
          "sourceInfo": "/BaseSample"
        },
        "resolver": "Sample1",
        "map": {
          "typeName": "Sample",
          "sourceType": "fs",
          "sourceInfo": "/Sample"
        }
      },
      {
        "base": {
          "typeName": "BaseSample",
          "sourceType": "fs",
          "sourceInfo": "/BaseSample"
         },
         "resolver": "Sample2",
         "map": {
           "typeName": "SampleTypeSecond",
           "sourceType": "fs",
           "sourceInfo": "/SampleTypeSecond"
         }
       }
    ]
  }
}

How to load json configuration

In registry, a method has been introduced -

registry.loadConfiguration(config).

You can use above method to load json configurations. This method needs Config object as parameter. You can find it from dependencyjs. Below are the detail of Config object

- Config: This class basically contains two properties.
    - appDirectory: You need to initialize this property with main execution directory.
    - configFolder: This is the folder path where you keep all json configurations to register them in registry.

Below is the example of reading json configuration.

import {registry, Config} from "dependencyjs";

let config: Config = new Config();
config.appDirectory = "Main application directory"
config.configFolder = "where you contains all json"

registry.loadConfiguration(config);

All your classes will be register which has been mentioned in json files. If not, you will get appropriate error message.

Now you can use registry as the way explained in usage section

Core Contributors

Feel free to reach out to any of the core contributors with your questions or concerns. We will do our best to respond in a timely manner.

IntimeTec Manish Kumawat Akhil S