Skip to content

Instantly share code, notes, and snippets.

@igorjs
Created October 14, 2021 09:38
Show Gist options
  • Save igorjs/e7da0ceaa50815856cabddccfbd82ee7 to your computer and use it in GitHub Desktop.
Save igorjs/e7da0ceaa50815856cabddccfbd82ee7 to your computer and use it in GitHub Desktop.
JS utility functions
/**
* Example:
*
* const multiply20 = (price) => price * 20;
* const divide100 = (price) => price / 100;
* const normalizePrice = (price) => price.toFixed(2);
* const addPrefix = (price) => '$' + String(price);
*
* const getDiscount = pipe(multiply20, divide100, normalizePrice, addPrefix);
*
* getDiscount(200); // '$40.00'
*/
const pipe =
(...functions) =>
(value) => functions.reduce((result, nextFn) => nextFn(result), value);
module.exports = pipe;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment