Created
December 16, 2017 21:11
-
-
Save qborreda/ab8a4acfbd74ec602be6e9c06060a4e6 to your computer and use it in GitHub Desktop.
JavaScript ES6 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
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); | |
/* | |
const add5 = x => x + 5 | |
const multiply = (x, y) => x * y | |
const multiplyAndAdd5 = compose(add5, multiply) | |
multiplyAndAdd5(5, 2) -> 15 | |
*/ |
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
const curry = (fn, arity = fn.length, ...args) => | |
arity <= args.length | |
? fn(...args) | |
: curry.bind(null, fn, arity, ...args); | |
// curry(Math.pow)(2)(10) -> 1024 | |
// curry(Math.min, 3)(10)(50)(2) -> 2 |
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
const pipe = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))); | |
/* | |
const add5 = x => x + 5 | |
const multiply = (x, y) => x * y | |
const multiplyAndAdd5 = pipe(multiply, add5) | |
multiplyAndAdd5(5, 2) -> 15 | |
*/ |
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
const promisify = func => | |
(...args) => | |
new Promise((resolve, reject) => | |
func(...args, (err, result) => | |
err ? reject(err) : resolve(result)) | |
); | |
// const delay = promisify((d, cb) => setTimeout(cb, d)) | |
// delay(2000).then(() => console.log('Hi!')) -> Promise resolves after 2s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment