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

villar

v0.0.25

Published

Facilitates factory creation and infrastructure

Downloads

292

Readme

Villar factory

Facilitates factory creation and infrastructure

Usage


Classes and decorators

  1. Implementation

    1. key defines which key the impl represents
    2. implName if the instance is initialized with the original class name
    3. ref distinguishes if there are two identical keys for different implementations
  2. VillarImplDiscovery

    1. findImpl(string) searches for the implementation according to the key provided
  3. NestVillarImplDiscovery

    1. findImpl(string) has the same function as VillarImplDiscovery but does search for impl in the context of nest injection

Usage example

Configurate the impls


// The interface
export interface Calculator {
    calc(num1: number, num2: number): number
}

import { Implementation } from "villar"

// Addition impl
@Implementation({
    key: '+'
})
// At NestJs use @Injectable()
export class AdditionCalculator implements Calculator {

    calc(num1: number, num2: number): number {
        return num1 + num2
    }

}

// Subtraction
@Implementation({
    key: '-'
})
// At NestJs use @Injectable()
export class SubtractionCalculator implements Calculator {

    calc(num1: number, num2: number): number {
        return num1 - num2
    }
}

// Division
// includes attr is like in key
@Implementation({
    key: 'any_division_inclues(/)',
    includes: true
})
export class DivisionCalculator implements Calculator {

    calc(num1: number, num2: number): number {
        return num1 / num2
    }

}

// Multiplication
// truthCustom is function from custom truth impl selector
// function (key: string, metadata: any)
@Implementation({
    key: 'multiplication*',
    truthCustom: (key: string, metadata: any): boolean => { 
        return key.includes('*') && metadata?.value == 10
    }
})
export class MultiplicationCalculator implements Calculator {

    calc(num1: number, num2: number): number {
        return num1 * num2
    }

}

Discovery impls


import { VillarImplResolver } from "villar"

// Register impls
VillarImplResolver.register(SubtractionCalculator, AdditionCalculator)

// Addition Impl
const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl<Calculator>('+')
console.log(calculator?.calc(10, 1)) //result: 11

// Subtraction Impl result
const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl<Calculator>('-')
console.log(calculator?.calc(10, 1)) //result: 9

// Anu Impl rsult
const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl<Calculator>('any_key')
console.log(calculator?.calc(10, 1)) //result: undefined

// Division
const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl<Calculator>('*')
console.log(calculator?.calc(10, 2)) //result: 5

// Multiplication
const options: any = { 
      metadata: { 
        value: 10 
      }
    }

const calculator: Calculator | undefined = VillarImplDiscovery.getInstance().findImpl<Calculator>('*',  options)
console.log(calculator?.calc(10, 2)) //result: 20

NestJs Discovery impls


// Configure AppModule
@Module({
  imports: [],
  controllers: [AppController],
  providers: [
    AdditionCalculator, // Custom impl
    SubtractionCalculator // Custom impl,
    NestVillarImplDiscovery, // Helper Villar, import with import { NestVillarImplDiscovery } from "villar",
    anysProviders....
  ],
})
export class AppModule {}

// Example in controller

@Controller()
export class AppController {
  
  constructor(
    private readonly implDiscovery: NestVillarImplDiscovery
  ) {}

  @Get("execute-calc/:operator/:value/:value2")
  executeCalc(@Param("operator") operator: string, @Param("value") value: number, @Param("value2") value2: number) {
    const calculator = this.implDiscovery.findImpl<Calculator>(operator)
    return calculator?.calc(value, value2)
  }
}