Use Pipes in your next Angular Application! (Part 1)
Tags: angular, javascript
Published: 2020-03-28 23:57:44

Let's say that you're looking for a way to transform the way data is displayed without actually changing the data. But you're tired of writing the same parsing functions over and over again. Don't worry, Angular Pipes have got you covered! They provide a clean, lightweight method to transform your data straight from the HTML template. No longer will you need to worry about how to display JSON or dates, or even Title Casing strings. 

This series of articles will try to encourage you to use Pipes in your daily workflow. I'll be demonstrating how to use the <u>[Pipes that come with Angular](https://angular.io/api?type=pipe)</u>. Later in the series, I'll show how to create your own pipes. Let's get started!

_TL;DR: To see the full project, check it out [here](https://stackblitz.com/edit/ng-pipes-demo)._


### Case Pipes
These pipes simply change the case of the text being displayed. There are 3 main pipes: 
 - [UpperCasePipe](https://angular.io/api/common/UpperCasePipe)
 - [LowerCasePipe](https://angular.io/api/common/LowerCasePipe)
 - [TitleCasePipe](https://angular.io/api/common/TitleCasePipe)

The code snippets below demonstrate these 3 pipes in action:

{% gist https://gist.github.com/Qarun-Qadir-Bissoondial/b283407ce0341d5a27ae0dcf546607f7.js %}

And this is the output: 

```
Text-based Pipes!
This is our string: My sTriNg to ChANge!


Uppercase Pipe -> MY STRING TO CHANGE!

Lowercase Pipe -> my string to change!

Titlecase Pipe -> My String To Change!
```

Displaying different cases has never been easier ;)

### Number Pipes (and also, Pipes with Parameters?!)
Let's say you want to calculate a decimal number as accurately as possible but you don't want to display a bajillion decimal places. Number Pipes can help us out here. There are 3 main types:

 - [DecimalPipe](https://angular.io/api/common/DecimalPipe)
 - [PercentPipe](https://angular.io/api/common/PercentPipe)
 - [CurrencyPipe](https://angular.io/api/common/CurrencyPipe)

Each of these may warrant their own explanation.

#### DecimalPipe

This allows us to control the number of decimal places of a small number. See the following code: 

{% gist https://gist.github.com/Qarun-Qadir-Bissoondial/f1cbaaa5397a3a4f0887fb1c30976546.js %}

And this is the output: 
```
Number-based Pipes!
This is our small number: 0.14285714285714285

Decimal Pipe: 0.143 (This is 3 decimal places by default)

Decimal Pipe at 4 decimal places: 0.1429
```

You'll notice something slightly different about this pipe. It doesn't just say `{{smallNumber | number}}`. It says `{{smallNumber | number:'1.4'}}`. What's going on?

Some Angular Pipes are built to accept _parameters_. At the end of the day, the Pipe is simply a function. However, parameters for Pipes are always strings. In this particular example, '1.4' is broken up to mean "One digit before the decimal point and a minimum of 4 digits after the decimal point". You can find more information on the DecimalPipe [here](https://angular.io/api/common/DecimalPipe).

When building your own pipes, it's up to you to decide the format for the parameter and what it means.

#### PercentPipe

This simply converts our decimal number to display a percentage.

{% gist https://gist.github.com/Qarun-Qadir-Bissoondial/c6a1ec361054f1c11e888cbe3050edbd.js %}

And the corresponding output: 

```
Our number for percent: 0.12

Percent Pipe: 12%
```

#### CurrencyPipe

Angular also provides the CurrencyPipe for displaying different currencies as follows:

{% gist https://gist.github.com/Qarun-Qadir-Bissoondial/3bc463a894a8dbb97da7c9d0c7bc7fa2.js %}

And the output: 

```
Currency Pipes!
Our number: 19.99

Currency Pipe: $19.99

Canadian Currency: CA$19.99

Indian Currency: ₹19.99
```

This pipe takes in multiple parameters but the main one to focus on is the type of currency to be displayed. In the above example, I displayed Canadian (CAD) and Indian (INR) currencies.

<u>A word about locale. </u>
We may be accustomed to writing large numbers as 1,000,000.25. But that might not be common for other countries. For example, some countries would write the same number as 1.000.000,25. Other countries may separate every three digits with spaces, or separate every 4 digits instead of 3.

The CurrencyPipe also helps us handle this but you'll need to import the locale data at the module level and build with specific parameters using the CLI. This applies for any locale usage in Angular. See [here](https://github.com/angular/angular/issues/20286#issuecomment-343649893) for more info.

### Conclusion

And that's it! That's your introduction to using Pipes in Angular. They're pretty simple to use in your application without having to write functions to do the parsing for us.

Stay tuned for Part 2 of this series where we dive into more complicated pipes :D