Skip to content

Commit

Permalink
feat: implement wrap function pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
rxssula committed Oct 31, 2024
1 parent c0af4fa commit 4b1405b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 10 additions & 5 deletions apps/angular/9-wrap-function-pipe/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { NgFor } from '@angular/common';
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { WrapFnPipe } from './wrap-fn.pipe';

@Component({
standalone: true,
imports: [NgFor],
imports: [NgFor, WrapFnPipe],
selector: 'app-root',
template: `
<div *ngFor="let person of persons; let index = index; let isFirst = first">
{{ showName(person.name, index) }}
{{ isAllowed(person.age, isFirst) }}
{{ showName | wrapFn: person.name : index }}
{{ isAllowed | wrapFn: person.age : isFirst }}
</div>
`,
})
export class AppComponent {
export class AppComponent implements OnInit {
persons = [
{ name: 'Toto', age: 10 },
{ name: 'Jack', age: 15 },
{ name: 'John', age: 30 },
];

ngOnInit(): void {
console.log(typeof this.showName);
}

showName(name: string, index: number) {
// very heavy computation
return `${name} - ${index}`;
Expand Down
11 changes: 11 additions & 0 deletions apps/angular/9-wrap-function-pipe/src/app/wrap-fn.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'wrapFn',
standalone: true,
})
export class WrapFnPipe implements PipeTransform {
transform<R>(func: (...arg: any[]) => R, ...args: any[]): R {
return func(...args);
}
}

0 comments on commit 4b1405b

Please sign in to comment.