@caijs/container

1.0.2 • Public • Published

@caijs/container

Build Status Coverage Status NPM version NPM downloads

An IoC container that allows to register instance by locale with a fallback to english or default.

Installation

In your project folder run:

$ npm install @caijs/container

Example of use

const { Container } = require('@caijs/container');

class Something {
  constructor(name) {
    this.name = name;
  }
}
const container = new Container();
container.register('a', new Something('a'), true);
container.register('b', new Something('b'), true);
container.register('Something', Something, false);
const a = container.get('a'); // It will be the instance a
const b = container.get('b'); // it will be the instance b
const c = container.get('c'); // it will be undefined
const something1 = container.get('Something'); // it will return an instance of Something
const something2 = container.get('Something'); // it will return another instance of Something

Fallback locales

const { Container } = require('@caijs/container');

class Something {
  constructor(name) {
    this.name = name;
  }
}
const container = new Container();
container.register('something-es', new Something('something-es'), true);
container.register('something-en', new Something('something-en'), true);
container.register('otherthing-es', new Something('otherthing-es'), true);
container.register('otherthing', new Something('otherthing'), true);

const somethinges = container.get('something-es'); // it will be something-es
const somethingar = container.get('something-ar'); // as something-ar does not exists, it return something-en
const otherthingar = container.get('otherthing-ar'); // as otherthing-ar does not exists, neither otherthing-en, return otherthing

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.20latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i @caijs/container

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

11 kB

Total Files

7

Last publish

Collaborators

  • jesus-seijas
  • jseijas