Skip to content

Instantly share code, notes, and snippets.

@aeinbu
Created May 14, 2019 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeinbu/723ab53a17e2d108dfa497f9a43277f8 to your computer and use it in GitHub Desktop.
Save aeinbu/723ab53a17e2d108dfa497f9a43277f8 to your computer and use it in GitHub Desktop.
Helper to chain method calls while we're waiting for |> (the js pipe operator)
const chain = (obj, op = obj => obj, ...restOps) => restOps.length > 0
? chain(op(obj), ...restOps)
: op(obj);
@aeinbu
Copy link
Author

aeinbu commented May 14, 2019

Example usage

Given these methods to process an array

const mapper = collection => collection.map(x => /*something*/);
const filter = collection => collection.filter(x => /*something*/);

With the pipe operator I would do like this:

// JS with the pipe operator (currently a stage 2 proposal)
console.log( arr |> mapper |> filter |> (coll => coll.length));

Since we don't have the pipe operator yet, I'll settle for this:

console.log(chain(arr, mapper, filter, coll => coll.length));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment