Thuta Learning
Angular
IntermediateWeb DevelopmentIntermediate

Custom Pipes

AngularLesson 2620 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Custom Pipes
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

Custom Pipes, in plain language

You can create custom pipes for your application's specific requirements.

Problem → Why → Solution

The problem — why this concept matters

Filtering lists with an impure pipe can rerun on every change-detection pass and harm performance.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

Use a custom pipe for deterministic presentation-only transformation, not business filtering or side effects. A pure pipe should produce the same result for the same arguments; expensive computation may belong in the data model or computed state. Define its name, input and output types, null behavior, and locale assumptions as a public API.

Mental model
  1. input and state
  2. component and template
  3. rendered Angular UI

Explain the core concept and Angular data flow of Custom Pipes

Core syntax or pattern

typescript
// reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'reverse'
})
export class ReversePipe implements PipeTransform {
  transform(value: string): string {
    return value.split('').reverse().join('');
  }
}

// exponential.pipe.ts
@Pipe({
  name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
  transform(value: number, exponent: number = 1): number {
    return Math.pow(value, exponent);
  }
}

// Usage in template
<p>{{ 'Angular' | reverse }}</p>              <!-- ralugnA -->
<p>{{ 2 | exponential:3 }}</p>                <!-- 8 (2^3) -->
<p>{{ 5 | exponential:2 }}</p>                <!-- 25 (5^2) -->

// Don't forget to declare pipes in module
@NgModule({
  declarations: [ReversePipe, ExponentialPipe]
})

Use a custom pipe for deterministic presentation-only transformation, not business filtering or side effects. A pure pipe should produce the same result for the same arguments; expensive computation may belong in the data model or computed state. Define its name, input and output types, null behavior, and locale assumptions as a public API.

Read the code step by step

  1. input and state// reverse.pipe.ts

    You can create custom pipes for your application's specific requirements.

  2. component and template})

    Use a custom pipe for deterministic presentation-only transformation, not business filtering or side effects. A pure pipe should produce the same result for the same arguments; expensive computation may belong in the data model or computed state. Define its name, input and output types, null behavior, and locale assumptions as a public API.

  3. rendered Angular UI})

    Create a file-size formatting pipe and unit-test bytes, KB, MB, zero, negative, and null values.

Execution flow
  1. input and state
  2. component and template
  3. rendered Angular UI

Success

Explain the core concept and Angular data flow of Custom Pipes

Failure

Filtering lists with an impure pipe can rerun on every change-detection pass and harm performance.

You can create custom pipes for your application's specific requirements.

typescript
// reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'reverse'
})
export class ReversePipe implements PipeTransform {
  transform(value: string): string {
    return value.split('').reverse().join('');
  }
}

// exponential.pipe.ts
@Pipe({
  name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
  transform(value: number, exponent: number = 1): number {
    return Math.pow(value, exponent);
  }
}

// Usage in template
<p>{{ 'Angular' | reverse }}</p>              <!-- ralugnA -->
<p>{{ 2 | exponential:3 }}</p>                <!-- 8 (2^3) -->
<p>{{ 5 | exponential:2 }}</p>                <!-- 25 (5^2) -->

// Don't forget to declare pipes in module
@NgModule({
  declarations: [ReversePipe, ExponentialPipe]
})
You should see
(The custom pipes work and transform the data)

Real-world usage

How to apply it in a real project

Create a file-size formatting pipe and unit-test bytes, KB, MB, zero, negative, and null values.

Mini challenge

Build it yourself

Rebuild the Custom Pipes example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
// reverse.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'reverse'
})
export class ReversePipe implements PipeTransform {
  transform(value: string): string {
    return value.split('').reverse().join('');
  }
}

// exponential.pipe.ts
@Pipe({
  name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
  transform(value: number, exponent: number = 1): number {
    return Math.pow(value, exponent);
  }
}

// Usage in template
<p>{{ 'Angular' | reverse }}</p>              <!-- ralugnA -->
<p>{{ 2 | exponential:3 }}</p>                <!-- 8 (2^3) -->
<p>{{ 5 | exponential:2 }}</p>                <!-- 25 (5^2) -->

// Don't forget to declare pipes in module
@NgModule({
  declarations: [ReversePipe, ExponentialPipe]
})

Expected: Explain the core concept and Angular data flow of Custom Pipes

Show hint

Create a file-size formatting pipe and unit-test bytes, KB, MB, zero, negative, and null values.

Quick quiz 1 / 3

What best describes the core idea of Custom Pipes?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • input and state → component and template → rendered Angular UI
  • Use a custom pipe for deterministic presentation-only transformation, not business filtering or side effects. A pure pipe should produce the same result for the same arguments; expensive computation may belong in the data model or computed state. Define its name, input and output types, null behavior, and locale assumptions as a public API.
  • Filtering lists with an impure pipe can rerun on every change-detection pass and harm performance.
  • Create a file-size formatting pipe and unit-test bytes, KB, MB, zero, negative, and null values.

Angular — Templates — Angular

Easy traps

  • Filtering lists with an impure pipe can rerun on every change-detection pass and harm performance.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the Custom Pipes example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (The custom pipes work and transform the data)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Custom Pipes | Thuta Learning