A small library for creating pipelines in JavaScript. This mimics the pipe operator in other functional languages such as Elixir.
npm install pipe-operator
const { take, native } = require('pipe-operator');
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.