pipe-operator

v0.0.7
A small library for creating pipelines in JavaScript. This mimics the pipe operator in other functional languages such as Elixir.
pipe operator pipeline operator pipe pipeline operator elixir functional programming

Build Status

pipe-operator

A small library for creating pipelines in JavaScript. This mimics the pipe operator in other functional languages such as Elixir.

Installation

npm install pipe-operator
const { take, native } = require('pipe-operator');

Usage

let frequentWords = take(text)
  .pipe(removePunctuation)
  .pipe(removeBadWords)
  .pipe(findMostFrequentWords)
  .result();

function removePunctuation(string) { /* ... */ }
function removeBadWords(string) { /* ... */ }
function findMostFrequentWords(string) { /* ... */ }

pipe() can also take additional arguments for the passed in function:

let total = take(1)
  .pipe(sum, 3, 2)
  .pipe(sum, 4)
  .result();

// total === 10

function sum(...args) {
  return args.reduce((total, number) => {
    return total + number;
  }, 0);
}

If you want to pipe to built-in prototype methods, use the native() function:

let languages = take('JavaScript, Elixir, PHP')
  .pipe(native(String.prototype.split, ','))
  .pipe(native(Array.prototype.map, function(string) {
    return string.trim();
  }))
  .result();

// languages === ['JavaScript', 'Elixir', 'PHP']

// OR

let languages = take('JavaScript, Elixir, PHP')
  .pipe(native(String.prototype.split, ','))
  .pipe(native(Array.prototype.map, native(String.prototype.trim)))
  .result();

// languages === ['JavaScript', 'Elixir', 'PHP']

Thanks to Sebastiaan Luca for the API inspiration.

npm i pipe-operator

Metadata

  • MIT
  • Whatever
  • David Tang
  • released 7/14/2017

Downloads

Maintainers