initial commit

This commit is contained in:
Robin Chappatte
2024-06-01 16:32:44 +02:00
commit 127ae6abd9
4 changed files with 201 additions and 0 deletions

92
inject.test.ts Normal file
View File

@@ -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);
});

95
inject.ts Normal file
View File

@@ -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<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;
}

1
mod.ts Normal file
View File

@@ -0,0 +1 @@
export * from './inject.ts'

13
readme.md Normal file
View File

@@ -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")
```