1.0.1 • Published 7 years ago

pipe-peek v1.0.1

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

pipe-peek

Peek into a functional pipeline to see intermediate results without interfering with the flow.

Installation

yarn add pipe-peek

or if you prefer npm:

npm install --save pipe-peek

Usage

const peek = require(`pipe-peek`);
// or if you use ES6 module syntax:
import peek from 'pipe-peek';

const { add, multiply, pipe, subtract } = require(`lodash/fp`);

const fahrenheitToCelsius = pipe(
  subtract(32),
  multiply(5 / 9)
);

const celsiusToKelvin = add(273.15);

let fahrenheitToKelvin = pipe(
  fahrenheitToCelsius,
  peek, // Should console.log the temperature in Celsius
  celsiusToKelvin
);

fahrenheitToKelvin(32); // outputs 0 to the log
// => 273.15

You can also import the function peekWith by name. It differs from peek in that it expects a function as an argument and calls that function instead of console.log on the intermediate result.

Let's redefine fahrenheitToKelvin to demonstrate:

const { peekWith } = require(`pipe-peek`);
// or
import { peekWith } from 'pipe-peek';

fahrenheitToKelvin = pipe(
  fahrenheitToCelsius,
  peekWith(console.info), // Outputs temp in Celsius using console.info instead of console.log
  celsiusToKelvin
);

fahrenheitToKelvin = pipe(
  fahrenheitToCelsius,
  peekWith(celsius =>
    console.log(`The temperature in Celsius is ${celsius}`),
  ),
  celsiusToKelvin
);

TODO:

  • Improve README
  • Add ESLint
  • Add tests (?)