The pipeline operator is already available in JavaScript

What is this?

This refers to the proposal https://github.com/tc39/proposal-pipeline-operator.

Define the compose function

While is the official symbol in mathematics, it's not a valid JavaScript identifier.
I will be using which looks like the pipe operator |> in most languages.

JavaScript
        
String.prototype.ᐅ = function(f) {
  return f(this)
}
        
      

Note that it's a new method on JavaScript's String object. It could work the same on every object (even on Object).

Usage

This is the same example as in the proposal.

JavaScript
        
function doubleSay (str) {
  return str + ", " + str;
}
function capitalize (str) {
  return str[0].toUpperCase() + str.substring(1);
}
function exclaim (str) {
  return str + '!';
}

let result = "hello"
  .ᐅ (doubleSay)
  .ᐅ (capitalize)
  .ᐅ (exclaim)

result //=> "Hello, hello!"
        
      

Contact me

Say hello: sven.sauleau@xtuc.fr.

Ping me on Twitter: @svensauleau.

© XTUC SAS
N° SIRET : 821 797 891 00016, RCS Strasbourg TI 821 797 891