@libs-ui/pipes-call-function-in-template 0.2.355-8 → 0.2.356-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -1,3 +1,156 @@
|
|
|
1
|
-
# pipes-call-function-in-template
|
|
1
|
+
# @libs-ui/pipes-call-function-in-template
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Version: `0.2.355-15`
|
|
4
|
+
>
|
|
5
|
+
> Pipe đa năng giúp gọi function xử lý logic ngay trong template, hỗ trợ truyền tham số và xử lý kết quả Async (Observable).
|
|
6
|
+
|
|
7
|
+
## Giới thiệu
|
|
8
|
+
|
|
9
|
+
`LibsUiPipesCallFunctionInTemplatePipe` là một giải pháp linh hoạt cho phép bạn thực hiện các phép biến đổi dữ liệu phức tạp ngay trong template HTML của Angular thông qua các hàm helper trong Component, thay vì phải tạo nhiều Pipe đơn lẻ cho từng mục đích cụ thể.
|
|
10
|
+
|
|
11
|
+
### Tính năng
|
|
12
|
+
|
|
13
|
+
- ✅ Gọi function xử lý logic ngay trong template
|
|
14
|
+
- ✅ Hỗ trợ truyền tham số phụ (item, otherData)
|
|
15
|
+
- ✅ Xử lý bất đồng bộ (Async) với Observable
|
|
16
|
+
- ✅ Tự động xử lý giá trị rỗng (null, undefined, 0)
|
|
17
|
+
- ✅ Tùy chỉnh giá trị mặc định cho các trường hợp đặc biệt
|
|
18
|
+
- ✅ Standalone pipe (Angular 16+)
|
|
19
|
+
|
|
20
|
+
## Khi nào sử dụng
|
|
21
|
+
|
|
22
|
+
- Khi cần xử lý logic phức tạp để hiển thị dữ liệu mà không muốn tạo Pipe riêng biệt cho từng trường hợp.
|
|
23
|
+
- Khi cần truyền thêm tham số phụ (item, otherData) vào hàm xử lý.
|
|
24
|
+
- Khi logic xử lý có thể tái sử dụng dưới dạng function helper.
|
|
25
|
+
- Hỗ trợ xử lý bất đồng bộ (Async) đơn giản trong template.
|
|
26
|
+
|
|
27
|
+
## Cài đặt
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install @libs-ui/pipes-call-function-in-template
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Import
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { LibsUiPipesCallFunctionInTemplatePipe } from '@libs-ui/pipes-call-function-in-template';
|
|
37
|
+
|
|
38
|
+
@Component({
|
|
39
|
+
standalone: true,
|
|
40
|
+
imports: [LibsUiPipesCallFunctionInTemplatePipe],
|
|
41
|
+
// ...
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Cách sử dụng
|
|
46
|
+
|
|
47
|
+
### 1. Transform String Cơ bản
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
{{ 'hello world' | LibsUiPipesCallFunctionInTemplatePipe : transformUppercase | async }}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
transformUppercase = (data: { value: string }): Observable<string> => {
|
|
55
|
+
return of((data.value || '').toUpperCase());
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 2. Truyền tham số nâng cao
|
|
60
|
+
|
|
61
|
+
```html
|
|
62
|
+
{{ 100 | LibsUiPipesCallFunctionInTemplatePipe : calculateTotal : 5 : 20 | async }}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
calculateTotal = (data: { value: number; item?: number; otherData?: number }): Observable<string> => {
|
|
67
|
+
const qty = data.item || 0;
|
|
68
|
+
const discount = data.otherData || 0;
|
|
69
|
+
const total = (data.value * qty) - discount;
|
|
70
|
+
return of(`$${total.toFixed(2)}`);
|
|
71
|
+
};
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 3. Xử lý giá trị rỗng/mặc định
|
|
75
|
+
|
|
76
|
+
```html
|
|
77
|
+
<!-- Case 1: Null Input -->
|
|
78
|
+
{{ null | LibsUiPipesCallFunctionInTemplatePipe : undefined : undefined : undefined : { valueIsEmpty: 'Empty Data' } | async }}
|
|
79
|
+
|
|
80
|
+
<!-- Case 2: Zero Input -->
|
|
81
|
+
{{ 0 | LibsUiPipesCallFunctionInTemplatePipe : undefined : undefined : undefined : { valueIs0: 'Zero Value' } | async }}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 4. Async Data (Mock API)
|
|
85
|
+
|
|
86
|
+
```html
|
|
87
|
+
@if (('active' | LibsUiPipesCallFunctionInTemplatePipe : fetchStatus | async) as status) {
|
|
88
|
+
{{ status }}
|
|
89
|
+
} @else {
|
|
90
|
+
<span>Loading status...</span>
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
fetchStatus = (data: { value: string }): Observable<string> => {
|
|
96
|
+
const statusMap: Record<string, string> = {
|
|
97
|
+
'active': 'Đang hoạt động (Online)',
|
|
98
|
+
'inactive': 'Ngừng kích hoạt (Offline)',
|
|
99
|
+
'pending': 'Đang chờ xử lý'
|
|
100
|
+
};
|
|
101
|
+
return of(statusMap[data.value] || 'Unknown').pipe(delay(1000));
|
|
102
|
+
};
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## API Reference
|
|
106
|
+
|
|
107
|
+
### LibsUiPipesCallFunctionInTemplatePipe
|
|
108
|
+
|
|
109
|
+
#### Parameters
|
|
110
|
+
|
|
111
|
+
| Property | Type | Default | Description |
|
|
112
|
+
| ------------------ | ------------------------------------ | ----------- | ------------------------------------------------ |
|
|
113
|
+
| `value` | `any` | `-` | Giá trị đầu vào cần xử lý. |
|
|
114
|
+
| `functionCall` | `TYPE_FUNCTION` | `undefined` | Hàm xử lý logic chính. Nhận object chứa input và params, trả về Observable. |
|
|
115
|
+
| `item` | `any` | `undefined` | Tham số phụ thứ nhất truyền vào functionCall. |
|
|
116
|
+
| `otherData` | `any` | `undefined` | Tham số phụ thứ hai truyền vào functionCall. |
|
|
117
|
+
| `defaultValueEmpty`| `{ valueIs0?: any; valueIsEmpty?: any }` | `undefined` | Cấu hình giá trị trả về mặc định khi kết quả là 0 hoặc rỗng/null. |
|
|
118
|
+
|
|
119
|
+
## Types & Interfaces
|
|
120
|
+
|
|
121
|
+
### TYPE_FUNCTION
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
export type TYPE_FUNCTION<T = any> = (
|
|
125
|
+
data: { value: any; item?: any; otherData?: any }
|
|
126
|
+
) => Observable<T>;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**Mô tả**: Type cho function xử lý logic. Nhận object chứa value, item, otherData và trả về Observable.
|
|
130
|
+
|
|
131
|
+
## Lưu ý quan trọng (Important Notes)
|
|
132
|
+
|
|
133
|
+
> ⚠️ **Caveats**:
|
|
134
|
+
>
|
|
135
|
+
> - **Yêu cầu `async` pipe**: Pipe này trả về một `Observable`. Bạn **BẮT BUỘC** phải sử dụng thêm `async` pipe trong template để hiển thị giá trị cuối cùng (ví dụ: `value | LibsUiPipesCallFunctionInTemplatePipe : fn | async`).
|
|
136
|
+
> - **Cơ chế SwitchMap**: Pipe sử dụng `switchMap` bên trong, do đó hàm xử lý của bạn phải trả về một Observable hoặc Promise.
|
|
137
|
+
> - **Xử lý giá trị rỗng**: Pipe tích hợp sẵn logic để handle `null`, `undefined` hoặc `0`. Bạn có thể tùy chỉnh thông qua tham số cuối cùng.
|
|
138
|
+
> - **Tham số truyền vào**: Hàm xử lý (`functionCall`) sẽ nhận một object duy nhất chứa `{ value, item, otherData }` để thuận tiện cho việc giải nén (destructuring).
|
|
139
|
+
|
|
140
|
+
## Công nghệ sử dụng
|
|
141
|
+
|
|
142
|
+
- Angular 18+
|
|
143
|
+
- RxJS 7.8+
|
|
144
|
+
- TypeScript 5+
|
|
145
|
+
|
|
146
|
+
## Demo
|
|
147
|
+
|
|
148
|
+
- **Local Development**: [http://localhost:4500/pipes/call-function-in-template](http://localhost:4500/pipes/call-function-in-template)
|
|
149
|
+
|
|
150
|
+
## Unit Tests
|
|
151
|
+
|
|
152
|
+
Xem file `test-commands.md` để biết cách chạy unit tests.
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
MIT
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Observable } from 'rxjs';
|
|
1
|
+
import { PipeTransform } from '@angular/core';
|
|
3
2
|
import { TYPE_FUNCTION } from '@libs-ui/interfaces-types';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
4
|
import * as i0 from "@angular/core";
|
|
5
|
-
export declare class LibsUiPipesCallFunctionInTemplatePipe implements PipeTransform
|
|
6
|
-
private
|
|
5
|
+
export declare class LibsUiPipesCallFunctionInTemplatePipe implements PipeTransform {
|
|
6
|
+
private destroyRef;
|
|
7
7
|
transform(value: any, functionCall?: TYPE_FUNCTION, item?: any, otherData?: any, defaultValueEmpty?: {
|
|
8
8
|
valueIs0?: any;
|
|
9
9
|
valueIsEmpty?: any;
|
|
10
10
|
}): Observable<any>;
|
|
11
11
|
private getValueOfType;
|
|
12
|
-
ngOnDestroy(): void;
|
|
13
12
|
static ɵfac: i0.ɵɵFactoryDeclaration<LibsUiPipesCallFunctionInTemplatePipe, never>;
|
|
14
13
|
static ɵpipe: i0.ɵɵPipeDeclaration<LibsUiPipesCallFunctionInTemplatePipe, "LibsUiPipesCallFunctionInTemplatePipe", true>;
|
|
15
14
|
}
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { Pipe } from '@angular/core';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { DestroyRef, inject, Pipe } from '@angular/core';
|
|
3
|
+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
4
|
+
import { CHARACTER_DATA_EMPTY, isPrimitiveType } from '@libs-ui/utils';
|
|
5
|
+
import { map, of, switchMap } from 'rxjs';
|
|
5
6
|
import * as i0 from "@angular/core";
|
|
6
7
|
export class LibsUiPipesCallFunctionInTemplatePipe {
|
|
7
|
-
|
|
8
|
+
destroyRef = inject(DestroyRef);
|
|
8
9
|
transform(value, functionCall, item, otherData, defaultValueEmpty) {
|
|
9
10
|
if (!functionCall) {
|
|
10
11
|
return of(this.getValueOfType(value, defaultValueEmpty));
|
|
11
12
|
}
|
|
12
|
-
return of({ value, item, otherData }).pipe(switchMap(functionCall),
|
|
13
|
+
return of({ value, item, otherData }).pipe(switchMap(functionCall), map((data) => this.getValueOfType(data, defaultValueEmpty)), takeUntilDestroyed(this.destroyRef));
|
|
13
14
|
}
|
|
14
15
|
getValueOfType(value, defaultValueEmpty) {
|
|
15
|
-
if (
|
|
16
|
-
return value
|
|
16
|
+
if (!isPrimitiveType(value) || typeof value === 'boolean') {
|
|
17
|
+
return value ?? defaultValueEmpty?.valueIsEmpty ?? CHARACTER_DATA_EMPTY;
|
|
17
18
|
}
|
|
18
19
|
if (value) {
|
|
19
20
|
return `${value}`;
|
|
@@ -23,10 +24,6 @@ export class LibsUiPipesCallFunctionInTemplatePipe {
|
|
|
23
24
|
}
|
|
24
25
|
return defaultValueEmpty?.valueIsEmpty || CHARACTER_DATA_EMPTY;
|
|
25
26
|
}
|
|
26
|
-
ngOnDestroy() {
|
|
27
|
-
this.onDestroy.next();
|
|
28
|
-
this.onDestroy.complete();
|
|
29
|
-
}
|
|
30
27
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: LibsUiPipesCallFunctionInTemplatePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
|
|
31
28
|
static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: LibsUiPipesCallFunctionInTemplatePipe, isStandalone: true, name: "LibsUiPipesCallFunctionInTemplatePipe" });
|
|
32
29
|
}
|
|
@@ -37,4 +34,4 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.14", ngImpo
|
|
|
37
34
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JldHVybiBvZih7IHZhbHVlLCBpdGVtLCBvdGhlckRhdGEgfSkucGlwZShcbiAgICAgIHN3aXRjaE1hcChmdW5jdGlvbkNhbGwpLFxuICAgICAgbWFwKChkYXRhKSA9PiB0aGlzLmdldFZhbHVlT2ZUeXBlKGRhdGEsIGRlZmF1bHRWYWx1ZUVtcHR5KSksXG4gICAgICB0YWtlVW50aWxEZXN0cm95ZWQodGhpcy5kZXN0cm95UmVmKVxuICAgICk7XG4gIH1cblxuICBwcml2YXRlIGdldFZhbHVlT2ZUeXBlKHZhbHVlOiBhbnksIGRlZmF1bHRWYWx1ZUVtcHR5PzogeyB2YWx1ZUlzMD86IGFueTsgdmFsdWVJc0VtcHR5Pzogc3RyaW5nIH0pIHtcbiAgICBpZiAoIWlzUHJpbWl0aXZlVHlwZSh2YWx1ZSkgfHwgdHlwZW9mIHZhbHVlID09PSAnYm9vbGVhbicpIHtcbiAgICAgIHJldHVybiB2YWx1ZSA/PyBkZWZhdWx0VmFsdWVFbXB0eT8udmFsdWVJc0VtcHR5ID8/IENIQVJBQ1RFUl9EQVRBX0VNUFRZO1xuICAgIH1cblxuICAgIGlmICh2YWx1ZSkge1xuICAgICAgcmV0dXJuIGAke3ZhbHVlfWA7XG4gICAgfVxuXG4gICAgaWYgKHZhbHVlID09PSAwKSB7XG4gICAgICByZXR1cm4gZGVmYXVsdFZhbHVlRW1wdHk/LnZhbHVlSXMwID8/ICcwJztcbiAgICB9XG5cbiAgICByZXR1cm4gZGVmYXVsdFZhbHVlRW1wdHk/LnZhbHVlSXNFbXB0eSB8fCBDSEFSQUNURVJfREFUQV9FTVBUWTtcbiAgfVxufVxuIl19
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { Pipe } from '@angular/core';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { inject, DestroyRef, Pipe } from '@angular/core';
|
|
3
|
+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
4
|
+
import { isPrimitiveType, CHARACTER_DATA_EMPTY } from '@libs-ui/utils';
|
|
5
|
+
import { of, switchMap, map } from 'rxjs';
|
|
5
6
|
|
|
6
7
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
7
8
|
class LibsUiPipesCallFunctionInTemplatePipe {
|
|
8
|
-
|
|
9
|
+
destroyRef = inject(DestroyRef);
|
|
9
10
|
transform(value, functionCall, item, otherData, defaultValueEmpty) {
|
|
10
11
|
if (!functionCall) {
|
|
11
12
|
return of(this.getValueOfType(value, defaultValueEmpty));
|
|
12
13
|
}
|
|
13
|
-
return of({ value, item, otherData }).pipe(switchMap(functionCall),
|
|
14
|
+
return of({ value, item, otherData }).pipe(switchMap(functionCall), map((data) => this.getValueOfType(data, defaultValueEmpty)), takeUntilDestroyed(this.destroyRef));
|
|
14
15
|
}
|
|
15
16
|
getValueOfType(value, defaultValueEmpty) {
|
|
16
|
-
if (
|
|
17
|
-
return value
|
|
17
|
+
if (!isPrimitiveType(value) || typeof value === 'boolean') {
|
|
18
|
+
return value ?? defaultValueEmpty?.valueIsEmpty ?? CHARACTER_DATA_EMPTY;
|
|
18
19
|
}
|
|
19
20
|
if (value) {
|
|
20
21
|
return `${value}`;
|
|
@@ -24,10 +25,6 @@ class LibsUiPipesCallFunctionInTemplatePipe {
|
|
|
24
25
|
}
|
|
25
26
|
return defaultValueEmpty?.valueIsEmpty || CHARACTER_DATA_EMPTY;
|
|
26
27
|
}
|
|
27
|
-
ngOnDestroy() {
|
|
28
|
-
this.onDestroy.next();
|
|
29
|
-
this.onDestroy.complete();
|
|
30
|
-
}
|
|
31
28
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.14", ngImport: i0, type: LibsUiPipesCallFunctionInTemplatePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
|
|
32
29
|
static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "18.2.14", ngImport: i0, type: LibsUiPipesCallFunctionInTemplatePipe, isStandalone: true, name: "LibsUiPipesCallFunctionInTemplatePipe" });
|
|
33
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libs-ui-pipes-call-function-in-template.mjs","sources":["../../../../../libs-ui/pipes/call-function-in-template/src/call-function-in-template.pipe.ts","../../../../../libs-ui/pipes/call-function-in-template/src/libs-ui-pipes-call-function-in-template.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport {
|
|
1
|
+
{"version":3,"file":"libs-ui-pipes-call-function-in-template.mjs","sources":["../../../../../libs-ui/pipes/call-function-in-template/src/call-function-in-template.pipe.ts","../../../../../libs-ui/pipes/call-function-in-template/src/libs-ui-pipes-call-function-in-template.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { DestroyRef, inject, Pipe, PipeTransform } from '@angular/core';\nimport { takeUntilDestroyed } from '@angular/core/rxjs-interop';\nimport { TYPE_FUNCTION } from '@libs-ui/interfaces-types';\nimport { CHARACTER_DATA_EMPTY, isPrimitiveType } from '@libs-ui/utils';\nimport { map, Observable, of, switchMap } from 'rxjs';\n\n@Pipe({\n name: 'LibsUiPipesCallFunctionInTemplatePipe',\n standalone: true,\n})\nexport class LibsUiPipesCallFunctionInTemplatePipe implements PipeTransform {\n private destroyRef = inject(DestroyRef);\n transform(value: any, functionCall?: TYPE_FUNCTION, item?: any, otherData?: any, defaultValueEmpty?: { valueIs0?: any; valueIsEmpty?: any }): Observable<any> {\n if (!functionCall) {\n return of(this.getValueOfType(value, defaultValueEmpty));\n }\n return of({ value, item, otherData }).pipe(\n switchMap(functionCall),\n map((data) => this.getValueOfType(data, defaultValueEmpty)),\n takeUntilDestroyed(this.destroyRef)\n );\n }\n\n private getValueOfType(value: any, defaultValueEmpty?: { valueIs0?: any; valueIsEmpty?: string }) {\n if (!isPrimitiveType(value) || typeof value === 'boolean') {\n return value ?? defaultValueEmpty?.valueIsEmpty ?? CHARACTER_DATA_EMPTY;\n }\n\n if (value) {\n return `${value}`;\n }\n\n if (value === 0) {\n return defaultValueEmpty?.valueIs0 ?? '0';\n }\n\n return defaultValueEmpty?.valueIsEmpty || CHARACTER_DATA_EMPTY;\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;AAAA;MAWa,qCAAqC,CAAA;AACxC,IAAA,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IACvC,SAAS,CAAC,KAAU,EAAE,YAA4B,EAAE,IAAU,EAAE,SAAe,EAAE,iBAA0D,EAAA;QACzI,IAAI,CAAC,YAAY,EAAE;YACjB,OAAO,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC;QAC1D;AACA,QAAA,OAAO,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,IAAI,CACxC,SAAS,CAAC,YAAY,CAAC,EACvB,GAAG,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC,EAC3D,kBAAkB,CAAC,IAAI,CAAC,UAAU,CAAC,CACpC;IACH;IAEQ,cAAc,CAAC,KAAU,EAAE,iBAA6D,EAAA;QAC9F,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE;AACzD,YAAA,OAAO,KAAK,IAAI,iBAAiB,EAAE,YAAY,IAAI,oBAAoB;QACzE;QAEA,IAAI,KAAK,EAAE;YACT,OAAO,CAAA,EAAG,KAAK,CAAA,CAAE;QACnB;AAEA,QAAA,IAAI,KAAK,KAAK,CAAC,EAAE;AACf,YAAA,OAAO,iBAAiB,EAAE,QAAQ,IAAI,GAAG;QAC3C;AAEA,QAAA,OAAO,iBAAiB,EAAE,YAAY,IAAI,oBAAoB;IAChE;wGA3BW,qCAAqC,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,IAAA,EAAA,CAAA;sGAArC,qCAAqC,EAAA,YAAA,EAAA,IAAA,EAAA,IAAA,EAAA,uCAAA,EAAA,CAAA;;4FAArC,qCAAqC,EAAA,UAAA,EAAA,CAAA;kBAJjD,IAAI;AAAC,YAAA,IAAA,EAAA,CAAA;AACJ,oBAAA,IAAI,EAAE,uCAAuC;AAC7C,oBAAA,UAAU,EAAE,IAAI;AACjB,iBAAA;;;ACVD;;AAEG;;;;"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libs-ui/pipes-call-function-in-template",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.356-0",
|
|
4
4
|
"peerDependencies": {
|
|
5
5
|
"@angular/core": ">=18.0.0",
|
|
6
|
-
"@libs-ui/utils": "0.2.
|
|
6
|
+
"@libs-ui/utils": "0.2.356-0",
|
|
7
7
|
"rxjs": "~7.8.0"
|
|
8
8
|
},
|
|
9
9
|
"sideEffects": false,
|