diff --git a/test/swap.test.ts b/test/swap.test.ts new file mode 100644 index 0000000..4437491 --- /dev/null +++ b/test/swap.test.ts @@ -0,0 +1,30 @@ +import { expectType } from "tsd"; +import { __, pipe, compose, swap } from "../es"; + +const indexA = 0; +const indexB = 1; +const list: number[] = []; + +expectType(swap(indexA, indexB, list)); + +expectType(swap(__, indexB, list)(indexA)); + +expectType(swap(indexA, __, list)(indexB)); + +expectType(swap(__, __, list)(indexA, indexB)); +expectType(swap(__, __, list)(__, indexB)(indexA)); +expectType(swap(__, __, list)(indexA)(indexB)); + +expectType(swap(indexA, indexB)(list)); + +expectType(swap(__, indexB)(indexA, list)); +expectType(swap(__, indexB)(__, list)(indexA)); +expectType(swap(__, indexB)(indexA)(list)); + +expectType(swap(indexA)(indexB, list)); +expectType(swap(indexA)(__, list)(indexB)); +expectType(swap(indexA)(indexB)(list)); + +// sanity checks for compose and pipe +expectType<(list: readonly number[]) => number[]>(pipe(swap(indexA, indexB))); +expectType<(list: readonly number[]) => number[]>(compose(swap(indexA, indexB))); diff --git a/types/swap.d.ts b/types/swap.d.ts index a781158..bed2b8f 100644 --- a/types/swap.d.ts +++ b/types/swap.d.ts @@ -1,41 +1,58 @@ -import { Placeholder } from './util/tools'; +import { Placeholder } from "./util/tools"; -// swap(indexA, indexB, list) -export function swap(indexA: number, indexB: number, list: readonly T[]): T[]; -// swap(indexA, indexB)(list) -export function swap(indexA: number, indexB: number): (list: readonly T[]) => T[]; // swap(indexA) export function swap(indexA: number): { - // swap(indexA)(indexB, list) - (indexB: number, list: readonly T[]): T[]; + // swap(indexA)(indexB)(list) + (indexB: number): (list: readonly T[]) => T[]; // swap(indexA)(__, list)(indexB) (__: Placeholder, list: readonly T[]): (indexB: number) => T[]; - // swap(indexA)(indexB)(list) - (indexB: number): (list: readonly T[]) => T[]; + // swap(indexA)(indexB, list) + (indexB: number, list: readonly T[]): T[]; }; -// swap(__, indexB, list)(indexA) +// swap(__, indexB) +export function swap( + __: Placeholder, + indexB: number +): { + // swap(__, indexB)(indexA)(list) + (indexA: number): (list: readonly T[]) => T[]; + // swap(__, indexB)(__, list)(indexA) + (__2: Placeholder, list: readonly T[]): (indexA: number) => T[]; + // swap(__, indexB)(indexA, list) + (indexA: number, list: readonly T[]): T[]; +}; + +// swap(indexA, indexB)(list) +export function swap(indexA: number, indexB: number): (list: readonly T[]) => T[]; + +// swap(__, __, list) export function swap( __: Placeholder, - indexB: number, + __2: Placeholder, list: readonly T[] -): (indexA: number) => T[]; +): { + // swap(__, __, list)(indexA)(indexB) + (indexA: number): (indexB: number) => T[]; + // swap(__, __, list)(__, indexB)(indexA) + (__3: Placeholder, indexB: number): (indexA: number) => T[]; + // swap(__, __, list)(indexA, indexB) + (indexA: number, indexB: number): T[]; +}; + // swap(indexA, __, list)(indexB) export function swap( indexA: number, __: Placeholder, list: readonly T[] ): (indexB: number) => T[]; -// swap(__, __, list) + +// swap(__, indexB, list)(indexA) export function swap( __: Placeholder, - __2: Placeholder, + indexB: number, list: readonly T[] -): { - // swap(__, __, list)(indexA, indexB) - (indexA: number, indexB: number): T[]; - // swap(__, __, list)(__, indexB)(indexA) - (__3: Placeholder, indexB: number): (indexA: number) => T[]; - // swap(__, __, list)(indexA)(indexB) - (indexA: number): (indexB: number) => T[]; -}; +): (indexA: number) => T[]; + +// swap(indexA, indexB, list) +export function swap(indexA: number, indexB: number, list: readonly T[]): T[];