concept pipe in category angular

appears as: pipes, pipe, A pipe, The pipe, pipe, pipes
Getting MEAN with Mongo, Express, Angular, and Node.js 2ED

This is an excerpt from Manning's book Getting MEAN with Mongo, Express, Angular, and Node.js 2ED.

A pipe that can be useful for debugging is the JSON pipe, which turns a JSON object into a string so that it can be displayed in the browser. If you aren’t sure what data is coming through in the location object, you could temporarily bind to it somewhere in the HTML and add the JSON pipe.

9.4.2. Creating a pipe to transform the line breaks

You want to create a pipe that takes the provided text and replaces each instance of \n with a <br/> tag. You’ve already solved this problem in Pug by using a JavaScript replace command, as shown in the following code snippet:

p !{(content).replace(/\n/g, '<br/>')}

With Angular, you can’t do this inline. Instead, you need to create a pipe and apply it to the binding.

As you’ve already seen, pipes are best created by the Angular CLI, so run the following command in terminal to generate the files and register the pipe with the application:

$ ng generate pipe html-line-breaks

The pipe itself is fairly straightforward. It needs to accept incoming text as a string value. Replace each \n with a <br/>, and then return a string value. Update the main content of html-line-breaks.html to look like the following snippet:

Testing Angular Applications

This is an excerpt from Manning's book Testing Angular Applications.

This book is separated into two parts. The first part covers unit testing, which tests units of code. You’ll learn how to create unit tests for components, directives, pipes, services, and routing—using testing tools like Karma and Jasmine—and run those tests using the Angular command-line interface (CLI). The following list breaks down each of the testable concepts we’ll cover in part 1 of the book:

In this chapter, you’ll be testing a custom pipe called PhoneNumberPipe. This pipe takes in a phone number as a number or string in valid format and puts it into a format that the user specifies. You need to write tests for the pipe so you can confirm that it transforms data into the right format.

Each pipe in Angular has a method named transform. This method is responsible for formatting the pipe’s input. The signature for the transform function for PhoneNumberPipe looks like this:

transform(value: string, format?: string, countryCode?: string): string

value is passed into the function from the left of the pipe and represents a phone number. format is an optional string parameter that determines how the phone number is formatted. Different valid values for format are listed in table 5.1.

Tests for pipes all follow this structure because, as mentioned before, pipes are pure functions. There’s no need to mock or set anything up—you pass the function some input and confirm the result is what you’d expect.

Angular in Action

This is an excerpt from Manning's book Angular in Action.

  • Generates new application pieces —Need a new component? Easy; it can generate the files for you. It can generate components, services, routes, and pipes, and it also will automatically ensure they are fully wired up in the build process.
  • Pipes are essentially a way to format data, and depending on the data you have you may find it useful to create your own set of pipes to simplify your templates. Pipes are generally simple and easy to implement, and if they can save you repeated formatting logic, then you should make your own.

    Pipes are functions that data is passed through prior to being rendered into the page. Somewhat similar to how components have the OnInit lifecycle hook to handle logic before the component renders, pipes do the same thing for bindings. You can see how Angular processes bindings when pipes are present or not in figure 8.6.

    Figure 8.6 Data is passed through a pipe before being rendered in a template binding.

    c08-6.png

    In the chapter example, there are several uses of the Currency and Percent pipes to format data. These are simple but great examples of how pipes can be invaluable. Imagine your application has to support different currencies—the Currency pipe can figure out how each currency is typically formatted.

    There are fundamentally two types of pipes: pure and impure. Pure pipes maintain no state information, and impure pipes maintain state.

    Pure and impure pipes are also rendered differently, as you can see in figure 8.7. A pure pipe is only run if the input value passed into the pipe has changed, making it more efficient because it won’t run very often. On the other hand, an impure pipe will execute on every change detection run, because an impure pipe has state that might have changed, and that would render a different output regardless of whether the input has changed.

    Figure 8.7 How pure and impure pipes are handled by Angular

    c08-7.png

    This is very important to understand if you start to build pipes. Choosing to make a pipe pure or impure can have significant consequences in the way that pipe is executed and potentially on the performance of your application.

    We’ll build examples of both pure and impure pipes, but you should always try to build a pure pipe. I’d bet that 98% of all custom pipes created are pure—or should be pure. We’ll look a little more closely at why this is when we build an impure pipe, but storing state in a pipe is tricky and makes its implementation more complex.

    Let’s build a pipe that helps us display the change information for a stock, and we’ll be making it a pure pipe as well.