-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
222 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
// https://blex.me/@baealex/angular-pipe | ||
|
||
μ΅κ·€λ¬λ ν νλ¦Ώμ νκΈ°λλ λ°μ΄ν°λ₯Ό λ³ννλ κΈ°λ₯μ νμ΄νλΌκ³ λΆλ₯Έλ€. μλμ κ°μ΄ μ¬μ©ν μ μλ€. | ||
|
||
```ts | ||
{{ someData | somePipe }} | ||
``` | ||
|
||
<br> | ||
|
||
#### κΈ°λ³Έ νμ΄ν | ||
|
||
CommonModuleμ ν¬ν¨λ κΈ°λ³Έ νμ΄ν | ||
|
||
https://angular.io/api/common/CommonModule#pipes | ||
|
||
- [AsyncPipe](https://angular.io/api/common/AsyncPipe) | ||
|
||
```ts | ||
promise = new Promise((resolve) => setTimeout(() => resolve('Umm... Hello!'), 1000)) | ||
|
||
{{ promise | async }} | ||
// (1 second later) Umm... Hello! | ||
``` | ||
|
||
- [DatePipe](https://angular.io/api/common/DatePipe) | ||
|
||
```ts | ||
date = new Date() | ||
|
||
{{ date | date:'short' }} | ||
// 2023-11-23 | ||
``` | ||
|
||
- [CurrencyPipe](https://angular.io/api/common/CurrencyPipe) | ||
|
||
```ts | ||
price = 100 | ||
|
||
{{ price | currency:'USD' }} | ||
// $100.00 | ||
``` | ||
|
||
- [PercentPipe](https://angular.io/api/common/PercentPipe) | ||
|
||
```ts | ||
percentage = 0.15 | ||
|
||
{{ percentage | percent }} | ||
// 15% | ||
``` | ||
|
||
- [UpperCasePipe](https://angular.io/api/common/UpperCasePipe) | ||
|
||
```ts | ||
name = 'Jino Bae' | ||
|
||
{{ name | uppercase }} | ||
// JINO BAE | ||
``` | ||
|
||
- [LowerCasePipe](https://angular.io/api/common/LowerCasePipe) | ||
|
||
```ts | ||
{{ name | lowercase }} | ||
// jino bae | ||
``` | ||
|
||
- [TitleCasePipe](https://angular.io/api/common/TitleCasePipe) | ||
|
||
```ts | ||
{{ name | titlecase }} | ||
// Jino Bae | ||
``` | ||
|
||
- [SlicePipe](https://angular.io/api/common/SlicePipe) | ||
|
||
```ts | ||
longText = "Hello, everyone. My name is Jino." | ||
|
||
{{ longText | slice:0:10 }} | ||
// Hello, eve | ||
``` | ||
|
||
- [JsonPipe](https://angular.io/api/common/JsonPipe) | ||
|
||
```ts | ||
object = { | ||
name: 'Jino Bae' | ||
} | ||
|
||
{{ object | json }} | ||
// { "name": "Jino Bae" } | ||
``` | ||
|
||
<br> | ||
|
||
#### 컀μ€ν νμ΄ν | ||
|
||
νμ΄νλ₯Ό λ§λλ €λ©΄ `@Pipe` ν μ½λ μ΄ν°λ‘ κ°μΌ ν΄λμ€λ₯Ό `PipeTransform` μΆμ ν΄λμ€λ₯Ό μμλ°μ `transform` λ©μλλ₯Ό ꡬνν΄μ£Όλ©΄ λλ€. νλΌλ―Έν°λ `(value, ...args)`, ννμ΄κ³ μ κΈ°λ³Έ νμ΄νλ₯Ό 보면 μκ² μ§λ§ λ°μ΄ν°κ° `value`μ΄κ³ λ€μ λΆλ κ°μ΄ κ°κ°μ `arg`κ° λλ€. | ||
|
||
```ts | ||
{{ someData | somePipe : 'arg1' : 'arg2' : 'arg3' }} | ||
``` | ||
|
||
νμ΄νμ μ΅μ μ λ€μκ³Ό κ°λ€. | ||
|
||
###### name: `string` | ||
|
||
νμ΄νλ₯Ό νΈμΆν μ΄λ¦μ΄λ€. μΉ΄λ© μΌμ΄μ€λ‘ μλͺ ν κ²μ κΆμ₯νκ³ μλ€. | ||
|
||
```ts | ||
import { Pipe } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'myPipe', | ||
}) | ||
export class MyPipePipe implements PipeTransform { | ||
transform(value: string, format: string): unknown { | ||
return null | ||
} | ||
} | ||
``` | ||
|
||
###### pure: `boolean` | ||
|
||
μ΅μ λν μμ±μΌλ‘ κΈ°λ³Έκ°μ `true`μ΄λ€. μμ ν¨μ, λΉμμ ν¨μλ₯Ό λ§νλ κ²μ μκ² λλ°, μ€λͺ λ§ λ³΄κ³ λ μ΄λ»κ² λμνλμ§ λͺ νν μ΄ν΄νκΈ°κ° μ΄λ €μ λ€. λμνλ ννλ‘λ§ λ³΄λ©΄ pure pipeλ μ μΈν λ°μ΄ν°μ μ°Έμ‘° κ°μ λ³νκ° μλ κ²½μ°μλ§ μ€νλκ³ impure νμ΄νλ 맀 μκ°μ μ€νλλ€. | ||
|
||
μλ₯Όλ€μ΄ μλμ κ°μ impure pipeκ° μμ λ | ||
|
||
```ts | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'myPipe', | ||
pure: false, | ||
}) | ||
export class MyPipePipe implements PipeTransform { | ||
transform(value: string, format: string): unknown { | ||
console.log('pipe.transform()') | ||
return null | ||
} | ||
} | ||
``` | ||
|
||
μλμ κ°μ ν νλ¦Ώμ μμ±ν μν©μΈ κ²½μ° | ||
|
||
```ts | ||
<p>{{ state1 }}</p> | ||
<p>{{ state2 | myPipe }}</p> | ||
``` | ||
|
||
νμ΄νλ₯Ό λ°μΈλ©νμ§ μμ `state1`μ κ°μ΄ λ³νλλ μμ μλ μ½μμλ `pipe.transform()`μ΄ μ°νκ² λλ€. pure pipeμ κ²½μ°μλ `state2`μ λ³νμλ§ ν΄λΉ λ‘κ·Έκ° μ°νλ€. | ||
|
||
μ νν λ§νλ©΄ pure pipeμ κ²½μ°μλ μ»΄ν¬λνΈμ λ λλ§μ μλ‘νλ μν©μ΄λΌλ μ λ ₯ λ°μ΄ν°μ μ°Έμ‘° κ°μ΄ λ³νμ§ μμλ€λ©΄ ν΄λΉ νμ΄νμ transform λ©μλλ₯Ό μ€ννμ§ μλλ€. μ λ ₯μ λνμ¬ μΊμ±λ κ²°κ³Όλ₯Ό μΆλ ₯νκΈ° λλ¬Έμ΄λ€. pure pipeλ‘ μ λ§νλ©΄ λ€ μ²λ¦¬ν μ μμ κ² κ°μλ°.. impure pipeλ μ΄λ¨ λ μ¬μ©νλ©΄ μ’μκΉ? | ||
|
||
- μμ νμ΄ν : κ°μ μ λ ₯μ κ°μ κ²°κ³Όκ° λμλ μκ΄μμ λ (`uppercase`, `format`) | ||
- λΉμμ νμ΄ν : λ§€λ² μλ‘μ΄ κ²°κ³Όκ° νμν λ λλ κ°μ λ³νλ₯Ό κ°μ§ν΄μΌ ν λ (`sort`, `async`) | ||
|
||
λΉμμ νμ΄νκ° νμν μν©μ μκ°νλ€κ° κΈ°λ³ΈμΌλ‘ μ 곡λλ `async` νμ΄νκ° λ μ¬λλ€. μ΄ νμ΄νλ₯Ό κ°λ¨νκ² μ§μ ꡬνν΄λ³΄μ. | ||
|
||
```ts | ||
@Pipe({ | ||
name: 'myAsync', | ||
pure: false | ||
}) | ||
export class MyAsyncPipe implements PipeTransform { | ||
latestPromise: Promise<unknown> | null = null; | ||
value = null; | ||
|
||
transform(promise: Promise<unknown>, ...args: unknown[]) { | ||
if (this.latestPromise !== promise) { | ||
this.latestPromise = promise; | ||
this.latestPromise.then((value: any) => { | ||
this.value = value; | ||
}); | ||
} | ||
return this.value; | ||
} | ||
} | ||
``` | ||
|
||
μ΄ μ½λλ μ°Έκ³ μ©μΌλ‘ λ§λ€μλ€. μ΄ νμ΄νλ₯Ό pure pipeλ‘ λ°κΎΈλ©΄ νλ©΄μλ μμν resolve λλ κ°μ μΆλ ₯ν μ μλ€. impure pipeλ μ°Έμ‘° κ° λΏ μλλΌ κ°μ λ³νμλ λμνλ―λ‘ μ§μμ μΌλ‘ μ°Έμ‘° λΉκ΅ λ° resolve λ κ°μ μΆλ ₯ν μ μμμ λ°ν΄ pure pipe μ μ₯μμλ μ°Έμ‘° κ°μ΄ λ³ν κ²μ μλλ―λ‘ μΊμ±λ κ²°κ³ΌμΈ nullμ μΆλ ₯νκΈ° λλ¬Έμ΄λ€. | ||
|
||
###### standalone: `boolean` | ||
|
||
μ΅μ λν μμ±μΌλ‘ κΈ°λ³Έκ°μ `false`μ΄λ€. μ΄ μ΅μ μ μ¬μ©νλ©΄ λͺ¨λμ ν¬ν¨νμ§ μκ³ λ νμ΄ν μμ²΄κ° λͺ¨λλ‘μ λμνλ€. μμΈν κ²μ μ¬κΈ°λ₯Ό μ°Έκ³ νλκ² μ’μ λ― νλ€. | ||
|
||
- Angular :: Standalone Component Β· BLEX @baealex | ||
[#](https://blex.me/@baealex/angular-standalone-component) | ||
|
||
ν μ€νΈμ ν¬λ©§ν μ μ§μ ν΄μ£Όλ νμ΄νλ₯Ό μμ±ν΄ 보μλ€. | ||
|
||
```ts | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'myPipe', | ||
standalone: true, | ||
}) | ||
export class MyPipePipe implements PipeTransform { | ||
transform(value: string, format: string, key = 'x'): string { | ||
let result = ''; | ||
let j = 0; | ||
|
||
for (let i = 0; i < format.length; i++) { | ||
if (format[i] === key) { | ||
result += value.toString()[j++]; | ||
} else { | ||
result += format[i]; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
``` | ||
|
||
```ts | ||
<p>{{ '01012345678' | myPipe: 'xxx-xxxx-xxxx'}}</p> | ||
// 010-1234-5678 | ||
``` |