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

hypo-container

v1.0.9

Published

A dependency injection container

Downloads

18

Readme

hypo-container

A dependency injection container. Supports NodeJS and browser development.

Install

NPM:

npm install --save hypo-container

Yarn:

yarn add hypo-container

Usage

To create a container create a new instance of the Container class.

import { Container } from 'hypo-container'

const container = new Container()

A container has two types of dependencies. These are known as services and parameters.

Defining a service

A service is an object that does something as part of a larger system. Examples of services: a database connection, a templating engine, or a mailer. Almost any global object can be a service.

Services are defined using a callback function that returns an instance of an object.

const container = new Container()

container.register('owner', c => {
  return new Person('Brian', 26)
})

container.register('cat', c => {
  return new Cat('Garfield', 4, c.get('owner'))
})

Alternatively calls to the register() method can be chained, as demonstrated below.

container
  .register('owner', c => new Person('Brian', 26))
  .register('cat', c => new Cat('Garfield', 4, c.get('owner')))

Note that the callback has access to the current container's instance, via it's first argument. This allows you to reference other contained services and parameters when defining a new service.

Objects are created only when they are first accessed via the get() method, so order is not important. Using a defined service is very easy as demonstrated below.

const cat = container.get('cat')

The above is roughly equivalent to the following.

const cat = new Cat('Garfield', 4, new Person('Brian', 26))

Defining a factory service

By default the same instance of a service is returned when calling the container's get() method. If you want to return a new instance of the service you can make use of the container's factory() method as demonstrated below.

container.register('uniqueRobot', container.factory(c => {
  return new Robot()
}))

const uniqueRobot = container.get('uniqueRobot')

Defining parameters

Defining a parameter allows for easy configuration of your container from the outside and to store global values.

container.myName = 'Brian'
container.myAge = 26

container.register('me', c => {
  return new Person(c.myName, c.myAge)
})

Protecting parameters

To use an anonymous function to define a parameter use the protect() container method.

container['prop'] = 42

container.register('protected_prop', container.protect(c => {
  return c.prop * 2
}))

Update a registered service

In some cases you may want to modify a service definition after it has been defined. You can use the extend() method to define additional code to be run on your service just after it is created.

container.register('cat', c => {
  return new Cat('Garfield', 4, c.get('owner'))
})

container.extend('cat', (storage, c) => {
  storage.details = () => `${storage.name} - ${storage.age}`
  return storage
})

The first argument is the name of the service to extend, the second a function that gets access to the object instance and the container.

Extend a container

If you use the same libraries over and over, you might want to reuse some services from one project to the next one. You can easily extend a container by registering it to another.

const app = new Container()

app['name'] = 'My Awesome App'

const serviceContainer = new Container()

serviceContainer.register('magicNumberService', c => {
  return new MagicNumber()
})

app.register(serviceContainer)

app.get('magicNumberService')

Get the service creation function

When accessing a service, the container automatically calls the function used to supply the service. This creates an instance of that service. If you want to get access to this function, use the raw() method.

container.register('robot', c => new Robot())

// wraps previously defined robot method to create a factory method
container.register('uniqueRobot', container.factory(c => c.raw('robot')))

Deleting a registered service

You may want to delete a service at some point. To do this you use the delete() method. Supply the name of a registered service as the first argument in order to delete the service.

container.delete('cat')

Deleting all services

To delete all services call the deleteAll() method.

container.deleteAll()

Deleting a parameter

As parameters are set directly on the container object they can be deleted using the built in delete keyword.

container.myNumber = 0987654321

delete container.myNumber

Thanks

Special thanks to the creators and maintainers of Pimple. Hypo attempts to follow the Pimple api for ease of use and familiarity. The Hypo docs also attempt to match that of the Pimple docs.