commit 127ae6abd9f1b182efbc2f9bf816c8506c47200a Author: Robin Chappatte Date: Sat Jun 1 16:32:44 2024 +0200 initial commit diff --git a/inject.test.ts b/inject.test.ts new file mode 100644 index 0000000..87aa522 --- /dev/null +++ b/inject.test.ts @@ -0,0 +1,92 @@ +import { assertEquals } from "https://deno.land/std@0.223.0/assert/mod.ts"; +import { inject } from "./inject.ts"; + +function toInject( + p1: number, + p2: number, + p3: number, + p4: number, + p5: number, + p6: number, + p7: number, + p8: number, + p9: number, +) { + return [ + p1, + p2, + p3, + p4, + p5, + p6, + p7, + p8, + p9, + ]; +} + +const expectedResult = [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, +] + +Deno.test("inject with single parameter", () => { + const injected = inject(toInject, 1); + const result = injected(2, 3, 4, 5, 6, 7, 8, 9); + assertEquals(result, expectedResult); +}); + +Deno.test("inject with two parameters", () => { + const injected = inject(toInject, 1, 2); + const result = injected(3, 4, 5, 6, 7, 8, 9); + assertEquals(result, expectedResult); +}); + +Deno.test("inject with three parameters", () => { + const injected = inject(toInject, 1, 2, 3); + const result = injected(4, 5, 6, 7, 8, 9); + assertEquals(result, expectedResult); +}); + +Deno.test("inject with four parameters", () => { + const injected = inject(toInject, 1, 2, 3, 4); + const result = injected(5, 6, 7, 8, 9); + assertEquals(result, expectedResult); +}); + +Deno.test("inject with five parameters", () => { + const injected = inject(toInject, 1, 2, 3, 4, 5); + const result = injected(6, 7, 8, 9); + assertEquals(result, expectedResult); +}); + +Deno.test("inject with six parameters", () => { + const injected = inject(toInject, 1, 2, 3, 4, 5, 6); + const result = injected(7, 8, 9); + assertEquals(result, expectedResult); +}); + +Deno.test("inject with seven parameters", () => { + const injected = inject(toInject, 1, 2, 3, 4, 5, 6, 7); + const result = injected(8, 9); + assertEquals(result, expectedResult); +}); + +Deno.test("inject with eight parameters", () => { + const injected = inject(toInject, 1, 2, 3, 4, 5, 6, 7, 8); + const result = injected(9); + assertEquals(result, expectedResult); +}); + +Deno.test("inject with nine parameters", () => { + const injected = inject(toInject, 1, 2, 3, 4, 5, 6, 7, 8, 9); + const result = injected(); + assertEquals(result, expectedResult); +}); \ No newline at end of file diff --git a/inject.ts b/inject.ts new file mode 100644 index 0000000..4096859 --- /dev/null +++ b/inject.ts @@ -0,0 +1,95 @@ +/** + * 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( + subject: (p1: P1, ...params: OtherParams) => Result, + p1: P1, +): (...params: OtherParams) => Result; +export function inject( + subject: (p1: P1, p2: P2, ...params: OtherParams) => Result, + p1: P1, + p2: P2, +): (...params: OtherParams) => Result; +export function inject( + subject: (p1: P1, p2: P2, p3: P3, ...params: OtherParams) => Result, + p1: P1, + p2: P2, + p3: P3, +): (...params: OtherParams) => Result; +export function inject( + 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( + 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( + 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( + 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( + 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( + 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( + subject: (first: FirstParam, ...params: OtherParams) => Result, + ...paramsToInject: any[] +) { + return paramsToInject.reduce((functionToInjext, paramToInject) => { + return (...params: OtherParams) => + functionToInjext(paramToInject, ...params); + }, subject) as (...param: OtherParams) => Result; +} diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..faecb8b --- /dev/null +++ b/mod.ts @@ -0,0 +1 @@ +export * from './inject.ts' \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..1600990 --- /dev/null +++ b/readme.md @@ -0,0 +1,13 @@ +# Function injection + +Provide a way to inject the first(s: up to nine) arguments in a function. + +Example: +```typescript +function myFunc(p1: string, p2: string, p3: string) { + console.log(p1, p2, p3) +} + +const injected = inject(myFunc, "A", "B"); //< `injected` is a function that only take one argument +injected("C"); //< same result as calling `myFunc("A", "B", "C") +``` \ No newline at end of file