Skip to content

Commit

Permalink
feat: pipeLazy types test (marpple#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
darky committed Jul 20, 2023
1 parent fa9c3aa commit b9f79d2
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/Lazy/pipeLazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,34 @@ import type ReturnPipeType from "../types/ReturnPipeType";
// prettier-ignore
function pipeLazy<T1, R>(
f1: (a: Awaited<T1>) => R
): (a: T1) => ReturnPipeType<[T1,R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1,R]>;

// prettier-ignore
function pipeLazy<T1, T2, R>(
f1: (a: Awaited<T1>) => T2,
f2: (a: Awaited<T2>) => R
): (a: T1) => ReturnPipeType<[T1, T2, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, R]>;

function pipeLazy<T1, T2, T3, R>(
f1: (a: Awaited<T1>) => T2,
f2: (a: Awaited<T2>) => T3,
f3: (a: Awaited<T3>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, R]>;

function pipeLazy<T1, T2, T3, T4, R>(
f1: (a: Awaited<T1>) => T2,
f2: (a: Awaited<T2>) => T3,
f3: (a: Awaited<T3>) => T4,
f4: (a: Awaited<T4>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, R]>;

function pipeLazy<T1, T2, T3, T4, T5, R>(
f1: (a: Awaited<T1>) => T2,
f2: (a: Awaited<T2>) => T3,
f3: (a: Awaited<T3>) => T4,
f4: (a: Awaited<T4>) => T5,
f5: (a: Awaited<T5>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, R]>;

function pipeLazy<T1, T2, T3, T4, T5, T6, R>(
f1: (a: Awaited<T1>) => T2,
Expand All @@ -84,7 +84,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, R>(
f4: (a: Awaited<T4>) => T5,
f5: (a: Awaited<T5>) => T6,
f6: (a: Awaited<T6>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, R]>;

function pipeLazy<T1, T2, T3, T4, T5, T6, T7, R>(
f1: (a: Awaited<T1>) => T2,
Expand All @@ -94,7 +94,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, R>(
f5: (a: Awaited<T5>) => T6,
f6: (a: Awaited<T6>) => T7,
f7: (a: Awaited<T7>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, R]>;

function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, R>(
f1: (a: Awaited<T1>) => T2,
Expand All @@ -105,8 +105,9 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, R>(
f6: (a: Awaited<T6>) => T7,
f7: (a: Awaited<T7>) => T8,
f8: (a: Awaited<T8>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, R>(
f1: (a: Awaited<T1>) => T2,
f2: (a: Awaited<T2>) => T3,
Expand All @@ -117,8 +118,9 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, R>(
f7: (a: Awaited<T7>) => T8,
f8: (a: Awaited<T8>) => T9,
f9: (a: Awaited<T9>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R>(
f1: (a: Awaited<T1>) => T2,
f2: (a: Awaited<T2>) => T3,
Expand All @@ -130,8 +132,9 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R>(
f8: (a: Awaited<T8>) => T9,
f9: (a: Awaited<T9>) => T10,
f10: (a: Awaited<T10>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R>(
f1: (a: Awaited<T1>) => T2,
f2: (a: Awaited<T2>) => T3,
Expand All @@ -144,8 +147,9 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R>(
f9: (a: Awaited<T9>) => T10,
f10: (a: Awaited<T10>) => T11,
f11: (a: Awaited<T11>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R>(
f1: (a: Awaited<T1>) => T2,
f2: (a: Awaited<T2>) => T3,
Expand All @@ -159,8 +163,9 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R>(
f10: (a: Awaited<T10>) => T11,
f11: (a: Awaited<T11>) => T12,
f12: (a: Awaited<T12>) => R,
): () => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R>(
f1: (a: Awaited<T1>) => T2,
f2: (a: Awaited<T2>) => T3,
Expand All @@ -175,9 +180,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R>(
f11: (a: Awaited<T11>) => T12,
f12: (a: Awaited<T12>) => T13,
f13: (a: Awaited<T13>) => R,
): () => ReturnPipeType<
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R]
>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R>(
Expand All @@ -195,7 +198,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R
f12: (a: Awaited<T12>) => T13,
f13: (a: Awaited<T13>) => T14,
f14: (a: Awaited<T14>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R>(
Expand All @@ -214,7 +217,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T
f13: (a: Awaited<T13>) => T14,
f14: (a: Awaited<T14>) => T15,
f15: (a: Awaited<T15>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R>(
Expand All @@ -234,7 +237,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T
f14: (a: Awaited<T14>) => T15,
f15: (a: Awaited<T15>) => T16,
f16: (a: Awaited<T16>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R>(
Expand All @@ -255,7 +258,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T
f15: (a: Awaited<T15>) => T16,
f16: (a: Awaited<T16>) => T17,
f17: (a: Awaited<T17>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R>(
Expand All @@ -277,7 +280,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T
f16: (a: Awaited<T16>) => T17,
f17: (a: Awaited<T17>) => T18,
f18: (a: Awaited<T18>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R>(
Expand All @@ -300,7 +303,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T
f17: (a: Awaited<T17>) => T18,
f18: (a: Awaited<T18>) => T19,
f19: (a: Awaited<T19>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R]>;

// prettier-ignore
function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R>(
Expand All @@ -324,7 +327,7 @@ function pipeLazy<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T
f18: (a: Awaited<T18>) => T19,
f19: (a: Awaited<T19>) => T20,
f20: (a: Awaited<T20>) => R,
): (a: T1) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R]>;
): (a: T1 | Promise<T1>) => ReturnPipeType<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R]>;

function pipeLazy(...fns: any[]) {
return (a: any) => reduce(pipe1, a, fns);
Expand Down
83 changes: 83 additions & 0 deletions type-check/Lazy/pipeLazy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { pipeLazy, throwError } from "../../src";
import * as Test from "../../src/types/Test";

const { checks, check } = Test;

const res1 = pipeLazy(String)(1);
const res2 = pipeLazy(Number)("1");

const res3 = pipeLazy(async (n: number) => String(n))(Promise.resolve(1));
const res4 = pipeLazy(async (s: string) => Number(s))(Promise.resolve("1"));

const res5 = pipeLazy((a) => Promise.resolve(String(a)))(1);
const res6 = pipeLazy((a) => Promise.resolve(Number(a)))("1");

const res7 = pipeLazy((a) => Promise.resolve(String(a)))(Promise.resolve(1));
const res8 = pipeLazy((a) => Promise.resolve(Number(a)))(Promise.resolve("1"));

const res9 = pipeLazy(
(a: "a" | "b") => a.toUpperCase(),
() => 1,
)("a");

const res10 = pipeLazy(
(a: "a") => a.toUpperCase(),
async () => 1,
)(Promise.resolve("a"));

const res11 = pipeLazy(
(a: "a") => Promise.resolve(a.toUpperCase()),
() => 1,
)(Promise.resolve("a"));

const res12 = pipeLazy(
(a: "a") => a.toUpperCase(),
() => Promise.resolve(1),
)(Promise.resolve("a"));

const res13 = pipeLazy(
(a: "a") => (Math.random() > 0.5 ? a.toUpperCase() : Promise.resolve("hi")),
() => 1,
)(Promise.resolve("a"));

const res14 = pipeLazy(throwError(() => Error()))(0);

const res15 = pipeLazy(
async () => 1,
throwError(() => Error()),
)(0);

const res16 = pipeLazy(
throwError(() => Error()),
async () => 1,
() => 2,
)(0);

const res17 = pipeLazy(
async () => {
throw Error("");
},
throwError(() => Error("")),
() => 1,
() => 2,
)(0);

checks([
check<typeof res1, string, Test.Pass>(),
check<typeof res2, number, Test.Pass>(),
check<typeof res3, Promise<string>, Test.Pass>(),
check<typeof res4, Promise<number>, Test.Pass>(),
check<typeof res5, Promise<string>, Test.Pass>(),
check<typeof res6, Promise<number>, Test.Pass>(),
check<typeof res7, Promise<string>, Test.Pass>(),
check<typeof res8, Promise<number>, Test.Pass>(),
check<typeof res9, number, Test.Pass>(),
check<typeof res10, Promise<number>, Test.Pass>(),
check<typeof res11, Promise<number>, Test.Pass>(),
check<typeof res12, Promise<number>, Test.Pass>(),
check<typeof res13, number | Promise<number>, Test.Pass>(),
check<typeof res14, never, Test.Pass>(),
check<typeof res15, Promise<never>, Test.Pass>(),
check<typeof res16, never, Test.Pass>(),
check<typeof res17, Promise<never>, Test.Pass>(),
]);

0 comments on commit b9f79d2

Please sign in to comment.