* Safe chained function * * Will only create a new function if needed, * otherwise will pass back existing functions or null. * * @param {function} functions to chain * @returns {function|null}
(...funcs)
| 8 | * @returns {function|null} |
| 9 | */ |
| 10 | function createChainedFunction(...funcs) { |
| 11 | return funcs |
| 12 | .filter((f) => f != null) |
| 13 | .reduce((acc, f) => { |
| 14 | if (typeof f !== 'function') { |
| 15 | throw new Error( |
| 16 | 'Invalid Argument Type, must only provide functions, undefined, or null.', |
| 17 | ); |
| 18 | } |
| 19 | |
| 20 | if (acc === null) return f; |
| 21 | |
| 22 | return function chainedFunction(...args) { |
| 23 | // @ts-expect-error ignore "this" error |
| 24 | acc.apply(this, args); |
| 25 | // @ts-expect-error ignore "this" error |
| 26 | f.apply(this, args); |
| 27 | }; |
| 28 | }, null); |
| 29 | } |
| 30 | |
| 31 | export default createChainedFunction; |
no outgoing calls
no test coverage detected
searching dependent graphs…