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/vasa

v0.0.1

Published

Vasa is a minimal and unobtrusive inversion of control container.

Downloads

1

Readme

@daisugi/vasa

Vasa is a minimal and unobtrusive inversion of control container.

Usage

const { vasa } = require('@daisugi/vasa');

const { container } = vasa();

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

class B {}

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

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

Table of contents

Install

Using npm:

npm install @daisugi/vasa

Using yarn:

yarn add @daisugi/vasa

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).
  • 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 using decorators by annotations (see STYLE_GUIDE.md pt.21).
  • Support a limited set of features; singleton and not, classes, values, and factories, and not much more.

API

token

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

useClass

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

Usage

const { vasa } = require('@daisugi/vasa');

const { container } = vasa();

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

class Bar {}

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

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

useValue

Useful for storing constants.

Usage

const { vasa } = require('@daisugi/vasa');

const { container } = vasa();

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

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

useFactory

Provides container as argument to the factory method.

Usage

const { vasa } = require('@daisugi/vasa');

const { container } = vasa();

class Foo {}

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

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

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

useFactoryWithParams

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

Usage

const { vasa } = require('@daisugi/vasa');

const { container } = vasa();

class Foo {}

function bar(foo) {
  return foo;
}

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

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

scope

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

Usage

const { vasa } = require('@daisugi/vasa');

const { container } = vasa();

class Foo {}

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

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

#list()

Get the list of the registered dependencies.

Usage

const { vasa } = require('@daisugi/vasa');

const { container } = vasa();

class Foo {}

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

const manifest = container.list();

// Now you can iterate the manifest items and decorate methods.

Goal

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

License

MIT