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!"
        
      

Reach out

Say hello: [email protected].

Ping me on Twitter: @svensauleau.