-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: fn pipe no longer needs explicit context unless invoking a …
…method in another instance. refactor: fn pipe types are now stricter
- Loading branch information
Javier Marín
committed
Oct 12, 2024
1 parent
dadf0a4
commit c9875c6
Showing
6 changed files
with
33 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
import {Pipe, PipeTransform} from '@angular/core'; | ||
import {ChangeDetectorRef, EmbeddedViewRef, Inject, Pipe, PipeTransform} from '@angular/core'; | ||
|
||
// https://stackoverflow.com/questions/67605122/obtain-a-slice-of-a-typescript-parameters-tuple | ||
type ParametersExceptFirst<F> = F extends (arg0: any, ...rest: infer R) => any ? R : never; | ||
|
||
/** | ||
* Pipe que permite transformar un valor usando una función, recordando el resultado hasta que el valor cambie. | ||
* | ||
* Si se necesita indicar un contexto, se puede indicar como un array con el objeto y el nombre del método. | ||
* | ||
* Ejemplo: user | fn:[userSvc, 'get'] | ||
* | ||
* https://dev.to/this-is-angular/deep-dive-into-angular-pipes-implementation-2g5n | ||
*/ | ||
@Pipe({ | ||
standalone: true, | ||
name: 'fn', | ||
pure: true, | ||
standalone: true | ||
pure: true | ||
}) | ||
export class FnPipe implements PipeTransform { | ||
/** | ||
*@inject (ChangeDetectorRef) prevents: | ||
* NullInjectorError: No provider for EmbeddedViewRef! | ||
*/ | ||
constructor(@Inject(ChangeDetectorRef) private readonly _viewRef: EmbeddedViewRef<unknown>) { | ||
} | ||
|
||
// A template syntax: {{ templateValue | fn:componentMethodRef:thisArg:fnArgument }} | ||
public transform<P, R>(value: P, fnReference: (arg: P, ...fnArguments: Array<any>) => R, context?: any, ...fnArguments: Array<any>): R { | ||
if (fnArguments.length) { | ||
fnArguments.unshift(value); | ||
return fnReference.apply(context, fnArguments); | ||
} else if (context) { | ||
return fnReference.call(context, value); | ||
public transform<T extends (...args: any) => any>(value: Parameters<T>[0], fn: T, ...args: [...ParametersExceptFirst<T>]): ReturnType<T> { | ||
if (args.length) { | ||
args.unshift(value); | ||
return fn.apply(this._viewRef.context, args); | ||
} else { | ||
return fnReference(value); | ||
return fn.call(this._viewRef.context, value); | ||
} | ||
} | ||
} | ||
|
||
|