Have ideas to improve npm?Join in the discussion! »

    pipe-operator

    0.0.7 • Public • Published

    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.

    Install

    npm i pipe-operator

    DownloadsWeekly Downloads

    11

    Version

    0.0.7

    License

    MIT

    Last publish

    Collaborators

    • avatar