Pipeline operator is an operator that allows to chain multiple functions together to form a pipeline
Operator symbole is |>
Syntax:
returnedResult=parameters |> function1 |> function2
The above statement is equivalent to
returnedResult=function2(function1(parameters))
- parameters are passed to the first function
- functions are executed in a declared order from left to right
- function can be named or anonymous function
Pipeline operator example
Check if the string contains lone surrogates or not.
Returns true, if unicode string is not present.
const incrementOne = (value) => value + 1;
const decrementOne = (value) => value - 1
const output = 20 |> incrementOne |> decrementOne; // 20
The above function equal to decrementOne(incrementOne(20))
Above example
- parameter 20 is passed to
incrementOne
function, output is 21 - 21 value is passed to decrementOne function, result is 20
The pipeline operator does not add any new functionality to javascript, however it adds new syntax to chain multiple functions together, and improves readability of a code.
It is also known as composition. It helps in functional programming to chain complex functions together, thus allows to write more readable code in simple way.
Advantages:
- Readable
- Easy to use
- maintainability