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

ng-generic-pipe

v17.0.4

Published

Generic pipe for Angular application for use a component method into component template.

Downloads

675

Readme

NgGenericPipe Build Status Coverage Status NPM version

Generic pipe for Angular application for use a component method into component template.

Description

Sometime there is a need to use a component method into component template. Angular best practice says do not use method into html temlate, eg. {{ myMethod(2) }}. With NgGenericPipe you can use all your public component methods as pure pipe with the component scope (this), eg: {{ 2 | ngGenericPipe: myMethod }}.

See the stackblitz demo.

Features

✅ More than 90% unit tested ✅ Use all your component methods as pure pipe with component scope ✅ Strong type check

Get Started

Step 1: install ng-generic-pipe

npm i ng-generic-pipe

Step 2: Import NgGenericPipeModule into your app module, eg.:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { NgGenericPipeModule } from 'ng-generic-pipe';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgGenericPipeModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
  ],
})
export class AppModule { }

Step 3: Use ngGenericPipe into your html template, eg.:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<div>{{ 'Simone' | ngGenericPipe: sayHello }}</div>`
})
export class AppComponent {
    sayHello(name: string): string {
      return `Hello! I'm ${name}.`; 
    }
}

API

ngGenericPipe need to pipe on a value. The value become the first argument of the funtion called by ngGenericPipe, eg.:

'Hello world!' | ngGenericPipe: writeMessage

is translated into:

writeMessage('Hello world!')

You can pass, multiple parameter in this way, eg.:

'Hello world!' | ngGenericPipe: writeMessage:'Simone'

is translated into:

writeMessage('Hello world!', 'Simone')

and with more parameters, eg.:

'Hello world!' | ngGenericPipe: writeMessage:'Simone':'Foo':'Bar':'Baz'

is translated into:

writeMessage('Hello world!', 'Simone', 'Foo', 'Bar', 'Baz')

Because ngGenericPipe is a pure pipe, the method is memoized. This means that the pipe transform the html only if an argument change. You can force the change by passing and aditional parameter that change when you need a repaint (see the example below "Call component method with component scope and force change detection ").

Strong type check

ngGenericPipe has strong type checking

alt text

Examples

Below there are some examples of use case.

Example: Call component method with component scope

You can call from template a componet method test(x: number) and access to the componet scope (this), eg.:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: '<div>{{ 3 | ngGenericPipe: test }}</div>'
})
export class AppComponent {

    public y: number = 2;

    test(x: number): number {
      return x * this.y; 
    }
}

Example: Call component method with component scope and multiple parameters

You can call from template a componet method test(x: number, z: number) and access to the componet scope (this), eg.:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: '<div>{{ 3 | ngGenericPipe: test:3 }}</div>'
})
export class AppComponent {

    public y: number = 2;

    test(x: number, z: number): number {
      return x * this.y * z; 
    }
}

Example: Call component method with component scope and no parameters

You can call from template a componet method test() and access to the componet scope (this), eg.:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: '<div>{{ undefined | ngGenericPipe: test }}</div>'
})
export class AppComponent {

    public y: number = 2;

    test(): number {
      return this.y; 
    }
}

Example: Call component method with component scope and force change detection

You can call from template a componet method test() and access to the componet scope (this), eg.:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <div>{{ undefined | ngGenericPipe: test:i }}</div>
    <button (click)="onUpdate()">Update</button>
  `
})
export class AppComponent {

    public y: number = Date.now();
    public i: number = 0;

    test(): number {
      return this.y; 
    }

    onUpdate(){
      this.i++;
    }
}

Example: Call observable component's method

You can call from template a component's method testAsync() that return observable, eg.:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <div>{{ 'hello!' | ngGenericPipe: testAsync | async }}</div>
  `
})
export class AppComponent {

    testAsync(value: string): Observable<string> {
      return of(value);
    }

}

Support

This is an open-source project. Star this repository, if you like it, or even donate. Thank you so much!

My other libraries

I have published some other Angular libraries, take a look: