Skip to content

Commit

Permalink
Add a post for Week 4
Browse files Browse the repository at this point in the history
  • Loading branch information
baealex committed Nov 25, 2023
1 parent 65c3b56 commit a9804fb
Showing 1 changed file with 222 additions and 0 deletions.
222 changes: 222 additions & 0 deletions JinoBae/2023-11-25-angular-pipe.md
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
```

0 comments on commit a9804fb

Please sign in to comment.