Created
October 14, 2021 09:38
-
-
Save igorjs/e7da0ceaa50815856cabddccfbd82ee7 to your computer and use it in GitHub Desktop.
JS utility functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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