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

@twn39/juice

v1.0.1

Published

Juice is a small Dependency Injection Container for typescript and javascript, inspired by Pimple.

Downloads

4

Readme

Juice is a small Dependency Injection Container for typescript and javascript.

Installation

Before using Juice in your project, add it to your package.json file:

$ pnpm i @twn39/juice

Usage

Creating a container is a matter of creating a Container instance:

import {Container} from '@twn39/juice'

const container = new Container();

As many other dependency injection containers, Juice manages two different kind of data: services and parameters.

Defining Services

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 by anonymous functions that return an instance of an object:

  container.set('LOGGER', (msg: string) => console.log(msg))

Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters.

As objects are only created when you get them, the order of the definitions does not matter.

Using the defined services is also very easy:

const logger = container.get('LOGGER');

Defining Factory Services

By default, each time you get a service, Pimple returns the same instance of it. If you want a different instance to be returned for all calls, wrap your anonymous function with the factory() method

import {Container} from "@twn39/juice";

container.factory("SESSION", (c: Container) => new Session());

Now, each call to container.get('SESSION') returns a new instance of the session.

Protecting Parameters

Because Juice sees anonymous functions as service definitions, you need to wrap anonymous functions with the protect() method to store them as parameters:

container.protect('RANDOM', () => rand())

Modifying Services after Definition

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:

import {Container} from "@twn39/juice";

container.set('STORAGE', () => localStorage);
container.extend('STORAGE', (storage: LocalStorage, c: Container) => {
    return sessionStorage;
})

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.

Extending 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; package your services into a provider by implementing IServiceProvider:


class FooProvider implements IServiceProvider {
  register(c: Container) {
      // register some services
  }
}

Then, register the provider on a Container:

container.register(new FooProvider());

Fetching the Service Creation Function

When you access an object, Pimple automatically calls the anonymous function that you defined, which creates the service object for you. If you want to get raw access to this function, you can use the raw() method:

container.set('SESSION', () => new Session());
const fn = container.raw('SESSION');