diff --git a/src/libs/compose.js b/src/libs/compose.js deleted file mode 100644 index 876aea40b6ff..000000000000 --- a/src/libs/compose.js +++ /dev/null @@ -1,36 +0,0 @@ -import _ from 'underscore'; - -/** - * This is a utility function taken directly from Redux. (We don't want to add Redux as a dependency) - * It enables functional composition, useful for the chaining/composition of HOCs. - * - * For example, instead of: - * - * export default hoc1(config1, hoc2(config2, hoc3(config3)))(Component); - * - * Use this instead: - * - * export default compose( - * hoc1(config1), - * hoc2(config2), - * hoc3(config3), - * )(Component) - * - * @returns {Function} - */ -export default function compose(...funcs) { - if (funcs.length === 0) { - return (arg) => arg; - } - - if (funcs.length === 1) { - return funcs[0]; - } - - return _.reduce( - funcs, - (a, b) => - (...args) => - a(b(...args)), - ); -} diff --git a/src/libs/compose.ts b/src/libs/compose.ts new file mode 100644 index 000000000000..dadc586d0f0d --- /dev/null +++ b/src/libs/compose.ts @@ -0,0 +1,71 @@ +/* eslint-disable @typescript-eslint/ban-types */ +/* eslint-disable import/export */ + +/** + * This is a utility function taken directly from Redux. (We don't want to add Redux as a dependency) + * It enables functional composition, useful for the chaining/composition of HOCs. + * + * For example, instead of: + * + * export default hoc1(config1, hoc2(config2, hoc3(config3)))(Component); + * + * Use this instead: + * + * export default compose( + * hoc1(config1), + * hoc2(config2), + * hoc3(config3), + * )(Component) + */ +export default function compose(): (a: R) => R; + +export default function compose(f: F): F; + +/* two functions */ +export default function compose(f1: (...args: A) => R1, f2: (a: R1) => R2): (...args: A) => R2; + +/* three functions */ +export default function compose(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3): (...args: A) => R3; + +/* four functions */ +export default function compose(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4): (...args: A) => R4; + +/* five functions */ +export default function compose( + f1: (...args: A) => R1, + f2: (a: R1) => R2, + f3: (a: R2) => R3, + f4: (a: R3) => R4, + f5: (a: R4) => R5, +): (...args: A) => R5; + +/* six functions */ +export default function compose( + f1: (...args: A) => R1, + f2: (a: R1) => R2, + f3: (a: R2) => R3, + f4: (a: R3) => R4, + f5: (a: R4) => R5, + f6: (a: R5) => R6, +): (...args: A) => R6; + +/* rest */ +export default function compose(f1: (a: unknown) => R, ...funcs: Function[]): (...args: unknown[]) => R; + +export default function compose(...funcs: Function[]): Function { + if (funcs.length === 0) { + // infer the argument type so it is usable in inference down the line + return (arg: T) => arg; + } + + if (funcs.length === 1) { + return funcs[0]; + } + + return funcs.reduce( + (a, b) => + (...args: unknown[]) => + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + a(b(...args)), + ); +}