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

di-asap

v2.1.0

Published

An easy way to introduce dependecy injection to your project

Downloads

9

Readme

Synopsis

This project introduces dependency injection to our project.

It's fairly simple to use.

  1. Declare modules (withoutInjection) that you do not wish to inject other dependencies into.
  2. Declare modules (withInjection) that will get their dependencies injected as function arguments.
  3. Declare already resolved modules (resolved). These modules are JS objects that you plan on injecting somewhere else.
  4. Either build the dependency tree, or require a single module from the index!

Installation

npm install di-asap

https://www.npmjs.com/package/di-asap

API

register.withoutInjection(moduleName, fileName) Register a module that does need injection, by passing the desired module name and the file name.

  • The module name indicates the way that this module should be referenced by other in order to be injected.
  • The file name is the path to the module.

register.withInjection(moduleName, fileName) Register a module that need injection, by passing the desired module name and the file name.

  • The module name indicates the way that this module should be referenced by other in order to be injected.
  • The file name is the path to the module.
  • This module should export a single function. Its arguments will be the modules that will be injected into it!
  • No need to require anything anymore, not even NMP packages!

register.resolved(moduleName, module) Register a module that is resolved, meaning that it does not need any injections itself.

  • The module name indicates the way that this module should be referenced by other in order to be injected.
  • The difference with the method withoutInjection is that you have to provide a JS Object, Array, String etc instead of a filename.

require(moduleName) After you have registered at least one module to the index, via the register methods, you can require a single module to your code using the di-asap. This is extremely useful for code reusability , since you can require a single service from a project that uses di-asap.

build After you have register at least one module to the index, you may call the build method. This method, resolves every single object in the index, and returns a container that makes them available to the caller.

Code Example

Start of by requiring the di-asap module:

const di = require('di-asap')()

Then register your modules:

di.register.withoutInjection('simpleStrings', path.join(root + '/stringExamples'))
di.register.withInjection('printer', path.join(root + '/printer'))

Bootstrap your application

const printingModule = di.build()

Printer is factory written as:

module.exports = function (simpleStrings, path) {
    return function () {
        console.log('Start printing from: ' + path.join(process.cwd()))
        simpleStrings.forEach(function(str) { console.log(str) })
    }
}

Notice the arguments of the printer! They're the dependencies we wish to inject! 'simpleStrings' was a module that we registered, while should is an npm package!

Alternatively, you could require only the printer module instead of building the whole index!

const printer = di.require('printer')

You may attach variables that you wish to inject to other modules by using the resolved method, or the di module itself!

di.register.resolved('scope', {scope: 'admin'})
di.register.resolved('di', di)

Then inside another module you could:,

module.exports = (di) => {
    let global_scope = di.require('scope')
    console.log(global_scope.scope) // admin //
}

Motivation

This project was build in order to use the 'require' only in place in your code! Build a 'mapping' module that takes care of your dependencies and simply inject dependencies as function arguments!

Tests

node test/build_spec