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

dididi

v1.1.2

Published

Inversion of Control Container for JavaScript

Downloads

13

Readme

DiDiDi

Tiny Inversion of Control Container for JavaScript

  • Can inject services and add paramaters just with the use of simple strings and the power of RegExp.
  • Its ease to use makes it very handy to manage dependencies from the tiniest of the apps, to the biggest.
  • Tinier than your ex's heart.
  • Inspired by Symfony's Dependency Injection.

Usage

Installation

npm install dididi

Getting Started

The fist step to everything is getting an instance of the container. It can be easily achieved by:

const container = require('dididi')()

Now that we have the container, let's see what it can do for us.

Setting Paramaters

To set parameters for use in the container you have to pass and object with any given key/value pairs:

container.parameters({
	parameter: 'value'
})

Parameters also accept injecting other parameters:

container.parameters({
	parameter_in_parameter: 'onset_<parameter>' // onset_value
})

Or environment variables:

container.parameters({
	environment_in_parameter: '<<NODE_ENV>>' // 'production' or 'development'
})

You can also add and overwrite parameters at any point in your project.

Register a Service

To register a service you need an string identifier that needs to be in camel case and separated by dots and its constructor like so:

class MockService {}

container.register(
	'example.mock_service',
	MockService
)

You can also add arguments:

class MockServiceWithArgs {
	constructor(title, description, random) {
		this.title = title // 'an argument'
		this.description = description // 'can really be anything'
		this.random = random // whatever random number
	}
}

container.register(
	'example.mock_service_with_args',
	MockServiceWithArgs,
	[
		'an argument',
		'can really be anything',
		Math.random()
	]
)

To inject parameters to a service, using the setted parameter from before:

class MockServiceWithParams {
	constructor(parameter) {
		this.parameter = parameter // 'value'
	}
}

container.register(
	'example.mock_service_with_params',
	MockServiceWithParams,
	[
		'<parameter>'
	]
)

You can also use the parameters inside your process.env global:

class MockServiceWithEnv {
	constructor(environment) {
		this.environment = environment // 'production' or 'development'
	}
}

container.register(
	'example.mock_service_with_env',
	MockServiceWithEnv,
	[
		'<<NODE_ENV>>'
	]
)

And last but not least, you can inject dependencies:

class MockServiceWithDependency {
	constructor(mockService) {
		this.mockService = mockService // MockService
	}
}

container.register(
	'example.mock_service_with_dependency',
	MockServiceWithDependency,
	[
		'>>example.mock_service'
	]
)

Getting a Service

To get a Service you just need to use its identifier like that:

/** @var {MockServiceWithDependency} mockServiceWithDependency */
const mockServiceWithDependency = container.get('example.mock_service_with_dependency')

License

MIT