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

short-di

v0.0.5

Published

Short di loading from config

Downloads

11

Readme

short-di

Short di loading from config tool. Used to loosen coupling between js modules, and realization Dependency-inversion principle.

Installation

For installation type npm command

npm i short-di --save

Usage

  1. Require or import short-di package like:
const shortDi = require('short-di'); //for JS
import * as shortDi from 'short-di'; //for TS
  1. For creating variative loading point use "load(<moduleAbstractID>, <currentModule>)" function:
const usersRepo = shortDi.load("IUsersRepository", module); //its JS. 
//for TS you will need create your own interface of needle module and implement it.
//Create runtime checking of loaded class should you own too
  1. in directory with main script create json config named like "<scriptName>.shortdi.json":
//files example:
...
index.js
index.shortdi.json
  1. In this json file add modules thats should be loaded instead of abstract identifiers. For example:
//index.shortdi.json
{
    "IUserRepository": "../repositories/mysql/mysql-user-repository"
} 
  1. Short-di package will load this modules instead abstract-module-ids you specified in "load()" function.

In other words, with config:

//index.shortdi.json
{
    "ICitiesParser": "some-path-to-cities-parser-module"
} 

The code:

const cityParser = shortDi.load("ICitiesParser", module); 

Will works like:

const cityParser = require("some-path-to-cities-parser-module"); 

Notes

  1. json short-di config file should be near main running script, and should contains resolvings for all "loads" in programm (includes required scripts).
  2. json short-di config file should be names like ".shortdi.json".
  3. The realization and mechanics had been full reworked from v0.2, and became race-condition-safety. Now may be used in asynchronous functions and asynchronous-uses-frameworks like "mocha".
  4. To avoid repetition of module identifiers, try to use as detailed module identifiers as possible.
  5. When loading, program will run from passed module by parents, checks existance of ".shortdi.json"-like di-config-files, and uses the most-close-to-main-script di-config-file.

For example:

if module-parents cache looks like:

+ main-script (has .shortdi.json file)
+ first-required-script (has .shortdi.json file)
+ script-where-used-shortdi-load (has not .shortdi.json file)

then will be used config of main-script, case it most close to main script.

Advanced example

//file-system
main-script.js
main-script.shortdi.json
+catalog1
|-connected-module1-with-loads.js
|-+included-catalog
| |-loaded-module-1.js
+catalog2
|-connected-module1-with-loads.js
|-+included-catalog
| |-loaded-module-2.js
...
//main-script.shortdi.json
{
    "someModule1": "./included-catalog/loaded-module-1",
    "someModule2": "./included-catalog/loaded-module-2",
}
//connected-module1-with-loads.js
const shortDi = require('short-di');
const someModule = shortDi.load("someModule1", module)
...
//connected-module2-with-loads.js
const shortDi = require('short-di');
const someModule = shortDi.load("someModule2", module)
...
//main-script.js
const shortDi = require('short-di');
const connectedModule1 = require('./catalog1/connected-module1-with-loads')
const connectedModule1 = require('./catalog2/connected-module2-with-loads')
...

Recipes

  1. Uses shortDi loadings in some module.
  2. Add configuration for it in unit-tests with loading dependency-stubs
  3. In main program code resolve in di-config real dependencies it should use

Author

License

  • MIT