0.0.7 • Published 7 years ago

pipe-operator v0.0.7

Weekly downloads
10
License
MIT
Repository
github
Last release
7 years ago

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.