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

@do-while-for-each/provider

v1.0.5

Published

Value provider and not only

Downloads

7

Readme

Installation

Install by npm:

npm install --save @do-while-for-each/provider

or install with yarn:

yarn add @do-while-for-each/provider

Registration of entries

Before getting a value from a provider, you need to register entries in it.
The requested value directly depends on the type of registered entry:

Entry result types:

 1. Value creator:
     class-instance = { provide, useClass?, deps?, multi? }
     factory-result = { provide, useFactory, deps?, multi? }

 2. Ready value:
    { provide, useValue, multi? }

Manual registration

You can use the default provider:

import {provider} from '@do-while-for-each/provider';

provider.register(
  {provide: Duck},
  {provide: Turkey, useValue: 123},
);

or create a new one:

import {Provider} from '@do-while-for-each/provider';

const provider = Provider.of([
  {provide: Duck, useValue: new Duck()},
  {provide: Turkey, useClass: Turkey},
]);

// or

const provider = new Provider('custom-provider#1');
provider.register(
  {provide: Duck, useClass: Turkey},
  {provide: 'Birds', useValue: new Duck(), multi: true},
);

Automatic registration

Automatic registration of entries in the provider is possible under the following conditions:

  1. TypeScript is required;
  2. You need to enable settings in tsconfig.json: experimentalDecorators and emitDecoratorMetadata;
  3. In the index file of your project, you need to import:
import "reflect-metadata";

You don't need to install the package reflect-metadata, it's already in dependencies.

  1. Place the appropriate decorators in the appropriate places of the class.

Decorators

  1. @injectable() - placed above the class. When value requested from the provider, it will create a new instance every time. Accepts the setting: @injectable({isOnlyOne: true}) - when value requested from the provider, it will return the same instance;
  2. @single - this is a short analog of @injectable({isOnlyOne: true});
  3. @inject(provide) - can set or override the constructor parameter of the class.
import {inject, injectable, single} from '@do-while-for-each/provider';

@injectable()
class A1 {
  constructor() {
  }
}

@single
class A2 {
  constructor(public id = 90,
              public a1: A1,
              @inject('Alex') public name = 'Naruto',
              public type: boolean) {
  }
}

Getting values from the provider

If you are sure that the provider should return one existing value, then:

const duck = provider.get<Duck>(Duck);

Otherwise, it is better to use:

const birds = provider.getAll('Birds') as Array<any>;

Normal use examples

Class instance provided

{provide: Duck}
{provide: Duck, useClass: Duck}
{provide: Duck, useClass: Duck, deps: ['quack!']}
{provide: Turkey, useClass: Duck, deps: ['ololo!']}

Factory result provided

{provide: "lang", useFactory: (user: User) => user?.lang || 'en', deps: [User]}

Value provided

{provide: Duck, useValue: 123}
{provide: Turkey, useClass: Turkey, deps: ['ololo!'], useValue: 123}

Multiple provided

{provide: "Bird", useClass: Duck, multi: true}
{provide: "Bird", useClass: Turkey, multi: true}
{provide: "Bird", useValue: 'Eagle', multi: true}

Single value

You should use the entry { provide, useValue } to manually register a single value, e.g.:

provider.register({provide: Duck, useValue: new Duck()});

Or use a @single decorator, e.g.:

@single  // or @injectable({isOnlyOne: true})
class Duck {
  constructor(public sound = 'quack!') {
  }
}

In both cases, you will always get the same value from the provider:

const obj = provider.get<Duck>(Duck);
const obj2 = provider.get<Duck>(Duck);
expect(obj === obj2).toBe(true);