Created
August 8, 2021 17:08
-
-
Save socker210/09ade9288cf38f6f8e28ffcfb641bf58 to your computer and use it in GitHub Desktop.
createChainedFunction
This file contains 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
/** | |
* @see https://github.com/react-bootstrap/react-bootstrap/blob/master/src/createChainedFunction.tsx | |
*/ | |
function createChainedFunction(...funcs) { | |
return funcs | |
.filter(f => f !== null) | |
.reduce((acc, f) => { | |
if (typeof f !== 'function') { | |
throw new Error('Invalid type') | |
} | |
if (acc === null) { | |
return f | |
} | |
/** | |
* @example | |
* funcs - fn1, fn2, fn3 | |
* acc will be chainedFunction - fn1(acc), fn2(f) | |
* f will be fn3 | |
*/ | |
return function chainedFunction(...args) { | |
acc.apply(this, args) | |
f.apply(this, args) | |
} | |
}, null) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment