Skip to content

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

Notifications You must be signed in to change notification settings

iamdtang/pipe-operator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published