Skip to content

Instantly share code, notes, and snippets.

@socker210
Created August 8, 2021 17:08
Show Gist options
  • Save socker210/09ade9288cf38f6f8e28ffcfb641bf58 to your computer and use it in GitHub Desktop.
Save socker210/09ade9288cf38f6f8e28ffcfb641bf58 to your computer and use it in GitHub Desktop.
createChainedFunction
/**
* @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