Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(call-apply): Implemented call apply Pipes #53

Merged
merged 6 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ We will review your PR as soon as possible, and your contribution will be greatl
Most likely, you'll need to create new secondary entry point to put the new utility in. To create entry point, use the following command:

```shell
pnpm nx g local-plugin:entry-point <name-of-your-utility> --library=ngxtension --skip-module
pnpm exec nx g local-plugin:entry-point <name-of-your-utility> --library=ngxtension --skip-module
```

#### Please write some Tests
Expand Down
14 changes: 5 additions & 9 deletions docs/src/content/docs/utilities/call-apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ title: call apply Pipes
description: ngxtension/call-apply
---

`callPipe` and `applyPipe` are simple standalone pipes that simplify the calling of a PURE functions passing params to it, they take advantage of the "memoization" offerd by pure pipes in Angular, and enforces that you use them only with PURE functions (aka if you use this inside the body function they throws error!)
`callPipe` and `applyPipe` are simple standalone pipes that simplify the calling of PURE functions passing params to it; they take advantage of the "memoization" offered by pure pipes in Angular, and ensure that you use them only with PURE functions (aka if you use this inside the body function they throw errors!)

```ts
import { CallPipe, ApplyPipe } from 'ngxtension/if-validation';
```

## Usage

Both `CallPipe` and `ApplyPipe` need a PURE function or method to invoke (aka you can't use `this` in the function body), the difference between the two is only in that invocation order and that `|call` is sutable only for funciton with 1-param, instead `|apply` works for function with any number of params.
Both `CallPipe` and `ApplyPipe` need a PURE function or method to invoke (aka you can't use `this` in the function body), the difference between the two is only in that invocation order and that `|call` is suitable only for function with 1-param, instead `|apply` works for function with any number of params.

```ts
import { Component, ChangeDetectionStrategy } from '@angular/core';
Expand All @@ -26,11 +26,7 @@ import { CallPipe, ApplyPipe } from 'ngxtension/call-apply';

<b>call UTC: {{ now | call : ISOFormat }}</b>
<i>with apply: {{ ISOFormat | apply : now }}</i>
<p>{{ join | apply : 'Hello' : ' world' : '!' }}</p>

<!-- PARAMS ARE NOT STRICT TYPED SORRY :-( ANY IDEA? -->
<div>{{ join | apply : 41 : 1 }}</div>
<!-- PRINT: 42 -->
<p>{{ join | apply : 'Hello' : 'world' : '!' }}</p>

<!-- IF YOU UNCOMMENT NEXT LINE IT WILL THROW ERROR
<h1>THIS IS NOT PURE: {{ updateClock | apply }}</h1>
Expand All @@ -41,8 +37,8 @@ import { CallPipe, ApplyPipe } from 'ngxtension/call-apply';
export class App {
public now = new Date(42, 42, 42, 42, 42, 42, 42); //"1945-08-12T16:42:42.042Z"
public ISOFormat = (date: Date) => date.toISOString();
public join(first: string, ...rest: string[]) {
return rest.reduce((a, b) => a + b, first);
public join(...rest: string[]) {
return rest.join(' ');
}
public updateClock() {
this.now = new Date();
Expand Down
9 changes: 0 additions & 9 deletions libs/ngxtension/call-apply/src/call-apply.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ describe(ApplyPipe.name, () => {
<p>{{ ISOFormat | apply : now }}</p>
<b>{{ doSomething | apply : 'Hello world' }}</b>
<a>{{ doSomething | apply : 'Prova' : 1 : 2 : 3 }}</a>
<!-- I KNOW NO STRICT TYPE CHECK :( ANY IDEA? -->
<u>{{ doSomething | apply : 42 : 1 : 2 : 3 }}</u>
`,
imports: [ApplyPipe],
})
Expand Down Expand Up @@ -128,13 +126,6 @@ describe(ApplyPipe.name, () => {
const elA = fixture.debugElement.query(By.css('a'));
expect(elA.nativeElement.textContent).toContain('Prova123');
});
it('can apply PURE function but it is NOT STRICT!', () => {
const fixture = TestBed.createComponent(Dummy);
fixture.detectChanges();

const elU = fixture.debugElement.query(By.css('u'));
expect(elU.nativeElement.textContent).toContain('48');
});
it('will fail if the function is NOT PURE (using this in the body)', () => {
expect(() => {
const fixture = TestBed.createComponent(FailDummy);
Expand Down
7 changes: 5 additions & 2 deletions libs/ngxtension/call-apply/src/call-apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const NOTHIS = !('Proxy' in window)
standalone: true,
})
export class CallPipe implements PipeTransform {
transform(value: any, args?: Function): any {
transform<T = any, R = any>(value: T, args?: (param?: T) => R): R {
nartc marked this conversation as resolved.
Show resolved Hide resolved
if (typeof args !== 'function')
throw new TypeError('You must pass a PURE funciton to | call');
return args?.call(NOTHIS, value);
Expand All @@ -36,7 +36,10 @@ export class CallPipe implements PipeTransform {
standalone: true,
})
export class ApplyPipe implements PipeTransform {
transform(fn: Function, ...args: any[]): any {
transform<TFunction extends (...args: any[]) => any>(
fn: TFunction,
...args: Parameters<TFunction>
): ReturnType<TFunction> {
if (typeof fn !== 'function')
throw new TypeError('You must use | apply on a PURE function');
return fn.apply(NOTHIS, args);
Expand Down