New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

javascript-dependency-injection

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

javascript-dependency-injection

Javscript dependency injection library

Source
npmnpm
Version
2.0.0-beta.4
Version published
Maintainers
1
Created
Source

Get this with Bower

Build Status Coverage Status Dependency Status devDependency Status Code Climate Inline docs

Gitter

Javascript Dependency Injection library (ES2015)

DI makes classes accessible by a contract. Instances are created when requested and dependencies are injected into the constructor, facilitating lazy initialization and loose coupling between classes.

As an example, consider a User and Persitance classes:

 class FileDB {
     constructor(fs, tableName, listOfFields) { .... }
     
     persist(record) { .... }
 }
 
 class User {
     constructor(email, passwd, storage, role) { ... }
     
     save() { this.storage.persist(this); }
 }

To define the relation between both classes use DI#register

import fs from 'fs';

var di = new DI();

di.register('$user', User, [null, 'welcome', '$fileDb', 'nobody']);
di.register('$fileDb', FileDb, [fs, 'user', ['email','passwd', 'role']], {singleton: true});

The 1st argument of DI#register is the name of the contract, the 2nd the class reference, the 3rd an array of the constructor arguments and the last a config object. Currently ther config has only 2 options

- singleton, it only create the instance once
- notAClass, meaning that the reference is not a class

To get instances do

di.getInstance('$user', 'test@di.com');

or if you also wish to change the role

di.getInstance('$user', 'test@di.com', null, null, 'admin');

For more advanced use-cases checkout the unit tests file.

You can find a detailed API description + a code-coverage report here

Installation

Install the dependencies as follows

$> npm install --save javascript-dependency-injection@beta

Unit testing

Unit testing is can be done as follows

$> npm test

it will also open the code-coverage report

Documentation

$> npm run doc

Keywords

javascript

FAQs

Package last updated on 28 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts