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

@daisugi/kado

v0.5.2

Published

Kado is a minimal and unobtrusive inversion of control container.

Downloads

174

Readme

@daisugi/kado

version npm downloads bundlephobia

This project is part of the @daisugi monorepo.

Kado is a minimal and unobtrusive inversion of control container.

🌟 Features

  • 💡 Minimum size overhead.
  • ⚡️ Written in TypeScript.
  • 📦 Only uses trusted dependencies.
  • 🔨 Powerful and agnostic to your code.
  • 🧪 Well tested.
  • 🤝 Is used in production.
  • ⚡️ Exports ES Modules as well as CommonJS.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {
  constructor(bar) {
    this.bar = bar;
  }
}

class Bar {}

container.register([
  {
    token: 'Foo',
    useClass: Foo,
    params: ['Bar'],
  },
  {
    token: 'Bar',
    useClass: Bar,
  },
]);

const foo = await container.resolve('Foo');

Table of contents

Install

Using npm:

npm install @daisugi/kado

Using yarn:

yarn add @daisugi/kado

:top: back to top

Motivation

This library is a result of a series of the requirements that either were not met by other libraries of same type, or were partially met, or finally met everything but also brought an overhead not required by the project.

If you feel that any of the following requirements is close to your demand, feel free to use this library, otherwise there are many other good IoC libraries out there such as di-ninja or tsyringe, among many others that you can use.

  • ✅ Should allow to create multiple instances of the container, and not share the state globally (useful when multiple packages are using it, or for monorepo).
  • ✅ The DI configuration must be abstracted from the base code, and must be able to be easily ported (Composition Root).
  • ✅ Dependencies must be able easily decorated (useful to add telemetry, debug ...).
  • ✅ Avoid use of decorators by annotations (see style guide).
  • ✅ Should work with pure JavaScript (don't depend of any superset like TypeScript).
  • ✅ Keep the API simple (singleton, transient, classes, values, factories, and not much more), but with enough pieces to cover the most common use cases.

:top: back to top

API

#register(manifestItems)

Used for registration of manifest items in the container.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

container.register([{ token: 'Foo' }]);

:top: back to top

#resolve(token)

Use this method when you need to resolve the registered dependency.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

container.register([{ token: 'Foo' }]);

const foo = await container.resolve('Foo');

:top: back to top

#get(token)

Returns registered manifest item by token.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {}

container.register([
  {
    token: 'Foo',
    useClass: Foo,
    scope: 'Transient',
  },
]);

const manifestItem = container.get('Foo');

:top: back to top

token

Is the name used to register the dependency, to later be resolved.

:top: back to top

useClass

Can go along with params property, which contains tokens with which the class should be resolved.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {
  constructor(bar) {
    this.bar = bar;
  }
}

class Bar {}

container.register([
  {
    token: 'Foo',
    useClass: Foo,
    params: ['Bar'],
  },
  {
    token: 'Bar',
    useClass: Bar,
  },
]);

const foo = await container.resolve('Foo');

:top: back to top

useValue

Useful for storing constants.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

container.register([
  {
    token: 'foo',
    useValue: 'text',
  },
]);

const foo = await container.resolve('foo');

:top: back to top

useFnByContainer

Provides container as argument to the factory method.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {}

function bar(c) {
  return c.resolve('Foo');
}

container.register([
  {
    token: 'Foo',
    useClass: Foo,
  },
  {
    token: 'bar',
    useFnByContainer: bar,
  },
]);

const foo = await container.resolve('bar');

:top: back to top

useFn

Same as useFnByContainer, except provides params to it, instead of the container.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {}

function bar(foo) {
  return foo;
}

container.register([
  {
    token: 'Foo',
    useClass: Foo,
  },
  {
    token: 'bar',
    useFn: bar,
    params: ['Foo'],
  },
]);

const foo = await container.resolve('bar');

:top: back to top

scope

Scope can be Transient or Singleton, by default it's Singleton. Can be used along with useClass, useFnByContainer and useFn. Having scope as Transient it will create a new instance every time the dependency is resolved, Singleton will reuse the already created instance.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {}

container.register([
  {
    token: 'Foo',
    useClass: Foo,
    scope: 'Transient',
  },
]);

const foo = await container.resolve('Foo');

:top: back to top

meta

Can be used to store arbitrary values.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {}

container.register([
  {
    token: 'Foo',
    meta: {
      isFoo: true,
    }
  },
]);

const foo = container.get('Foo');
foo.meta.isFoo; // true

:top: back to top

params

As can be observed in the previous examples the params key can receive an array of tokens, but also you can provide manifest items, you have an example below where we are injecting a text to the Foo class. Also for the convenience Kado provides some helpers Kado.value, Kado.map and Kado.flatMap, behind the scene these helpers are returning a simple manifest items.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {
  constructor(bar) {
    this.bar = bar;
  }
}

container.register([
  {
    token: 'Foo',
    useClass: Foo,
    param: [{
      useValue: 'text',
    }],
  },
]);

const foo = await container.resolve('Foo');

foo.bar; // 'text'

:top: back to top

#list()

Get the list of the registered dependencies.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {}

container.register([
  {
    token: 'Foo',
    useClass: Foo,
    scope: 'Transient',
  },
]);

const manifestItems = container.list();

// Now you can iterate over the manifest items and decorate them.

:top: back to top

Kado.value

Useful when you want to inject a value.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {
  constructor(bar) {
    this.bar = bar;
  }
}

container.register([
  {
    token: 'Foo',
    useClass: Foo,
    param: [Kado.value('text')],
  },
]);

const foo = await container.resolve('Foo');

foo.bar; // 'text'

:top: back to top

Kado.map

Useful when you want to resolve an array of items.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {
  constructor(args) {
    this.bar = args[0];
  }
}

container.register([
  {
    token: 'bar',
    useValue: 'text',
  },
  {
    token: 'Foo',
    useClass: Foo,
    param: [Kado.map(['bar'])],
  },
]);

const foo = await container.resolve('Foo');

foo.bar; // 'text'

:top: back to top

Kado.flatMap

The same as Kado.map but also it flats the array result.

Usage

import { Kado } from '@daisugi/kado';

const { container } = new Kado();

class Foo {
  constructor(args) {
    this.bar = args[0];
  }
}

container.register([
  {
    token: 'bar',
    useValue: ['text'],
  },
  {
    token: 'Foo',
    useClass: Foo,
    param: [Kado.flatMap(['bar'])],
  },
]);

const foo = await container.resolve('Foo');

foo.bar; // 'text'

:top: back to top

TypeScript

The Kado is fully written in TypeScript, therefore you have available some types.

import {
  Kado,
  type KadoManifestItem,
  type KadoContainer,
  type KadoToken,
  type KadoScope,
} from '@daisugi/kado';

const { container } = new Kado();

class Foo {}

const myContainer: KadoContainer = container;
const token: KadoToken = 'Foo';
const scope: KadoScope = Kado.scope.Transient;
const manifestItems: KadoManifestItem[] = [
  {
    token,
    useClass: Foo,
    scope,
  }
];

myContainer.register(manifestItems);

const foo = await myContainer.resolve<Foo>('Foo');

:top: back to top

Goal

The project aims to provide the basic functionality for IoC. The functionality will be kept simple and will not be overextended.

:top: back to top

Etymology

Kado is a Japanese art that involves an arrangement of a variety of plants. A characteristic of Japanese Kado is an emphasis on shapes and lines, as well as the manner in which the flower is placed into the dish.

:top: back to top

Other projects

Meet the ecosystem

:top: back to top

License

MIT