Files
Robin Chappatte 127ae6abd9 initial commit
2024-06-01 16:32:44 +02:00

96 lines
2.9 KiB
TypeScript

/**
* Take a function and the first(s) parameter(s) to inject.
* Return a function that don't need those parameters.
*
* Example:
* ```
* function myFunc(a, b, c) {
* // ...
* }
*
* const injected = inject(myFunc, "A", "B");
* injected("C"); // same result as myFunc("A", "B", "C")
* ```
*/
export function inject<P1, OtherParams extends any[], Result>(
subject: (p1: P1, ...params: OtherParams) => Result,
p1: P1,
): (...params: OtherParams) => Result;
export function inject<P1, P2, OtherParams extends any[], Result>(
subject: (p1: P1, p2: P2, ...params: OtherParams) => Result,
p1: P1,
p2: P2,
): (...params: OtherParams) => Result;
export function inject<P1, P2, P3, OtherParams extends any[], Result>(
subject: (p1: P1, p2: P2, p3: P3, ...params: OtherParams) => Result,
p1: P1,
p2: P2,
p3: P3,
): (...params: OtherParams) => Result;
export function inject<P1, P2, P3, P4, OtherParams extends any[], Result>(
subject: (p1: P1, p2: P2, p3: P3, p4: P4, ...params: OtherParams) => Result,
p1: P1,
p2: P2,
p3: P3,
p4: P4,
): (...params: OtherParams) => Result;
export function inject<P1, P2, P3, P4, P5, OtherParams extends any[], Result>(
subject: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, ...params: OtherParams) => Result,
p1: P1,
p2: P2,
p3: P3,
p4: P4,
p5: P5,
): (...params: OtherParams) => Result;
export function inject<P1, P2, P3, P4, P5, P6, OtherParams extends any[], Result>(
subject: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, ...params: OtherParams) => Result,
p1: P1,
p2: P2,
p3: P3,
p4: P4,
p5: P5,
p6: P6,
): (...params: OtherParams) => Result;
export function inject<P1, P2, P3, P4, P5, P6, P7, OtherParams extends any[], Result>(
subject: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, ...params: OtherParams) => Result,
p1: P1,
p2: P2,
p3: P3,
p4: P4,
p5: P5,
p6: P6,
p7: P7,
): (...params: OtherParams) => Result;
export function inject<P1, P2, P3, P4, P5, P6, P7, P8, OtherParams extends any[], Result>(
subject: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, ...params: OtherParams) => Result,
p1: P1,
p2: P2,
p3: P3,
p4: P4,
p5: P5,
p6: P6,
p7: P7,
p8: P8,
): (...params: OtherParams) => Result;
export function inject<P1, P2, P3, P4, P5, P6, P7, P8, P9, OtherParams extends any[], Result>(
subject: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, ...params: OtherParams) => Result,
p1: P1,
p2: P2,
p3: P3,
p4: P4,
p5: P5,
p6: P6,
p7: P7,
p8: P8,
p9: P9,
): (...params: OtherParams) => Result;
export function inject<FirstParam, OtherParams extends any[], Result>(
subject: (first: FirstParam, ...params: OtherParams) => Result,
...paramsToInject: any[]
) {
return paramsToInject.reduce((functionToInjext, paramToInject) => {
return (...params: OtherParams) =>
functionToInjext(paramToInject, ...params);
}, subject) as (...param: OtherParams) => Result;
}