Skip to content

Commit

Permalink
Fixing ordering and adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ranneyd committed Jun 2, 2023
1 parent ee672b7 commit c061b8b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
30 changes: 30 additions & 0 deletions test/swap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expectType } from "tsd";
import { __, pipe, compose, swap } from "../es";

const indexA = 0;
const indexB = 1;
const list: number[] = [];

expectType<number[]>(swap(indexA, indexB, list));

expectType<number[]>(swap(__, indexB, list)(indexA));

expectType<number[]>(swap(indexA, __, list)(indexB));

expectType<number[]>(swap(__, __, list)(indexA, indexB));
expectType<number[]>(swap(__, __, list)(__, indexB)(indexA));
expectType<number[]>(swap(__, __, list)(indexA)(indexB));

expectType<number[]>(swap(indexA, indexB)(list));

expectType<number[]>(swap(__, indexB)(indexA, list));
expectType<number[]>(swap(__, indexB)(__, list)(indexA));
expectType<number[]>(swap(__, indexB)(indexA)(list));

expectType<number[]>(swap(indexA)(indexB, list));
expectType<number[]>(swap(indexA)(__, list)(indexB));
expectType<number[]>(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)));
61 changes: 39 additions & 22 deletions types/swap.d.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
import { Placeholder } from './util/tools';
import { Placeholder } from "./util/tools";

// swap(indexA, indexB, list)
export function swap<T>(indexA: number, indexB: number, list: readonly T[]): T[];
// swap(indexA, indexB)(list)
export function swap<T>(indexA: number, indexB: number): (list: readonly T[]) => T[];
// swap(indexA)
export function swap(indexA: number): {
// swap(indexA)(indexB, list)
<T>(indexB: number, list: readonly T[]): T[];
// swap(indexA)(indexB)(list)
(indexB: number): <T>(list: readonly T[]) => T[];
// swap(indexA)(__, list)(indexB)
<T>(__: Placeholder, list: readonly T[]): (indexB: number) => T[];
// swap(indexA)(indexB)(list)
<T>(indexB: number): (list: readonly T[]) => T[];
// swap(indexA)(indexB, list)
<T>(indexB: number, list: readonly T[]): T[];
};

// swap(__, indexB, list)(indexA)
// swap(__, indexB)
export function swap(
__: Placeholder,
indexB: number
): {
// swap(__, indexB)(indexA)(list)
(indexA: number): <T>(list: readonly T[]) => T[];
// swap(__, indexB)(__, list)(indexA)
<T>(__2: Placeholder, list: readonly T[]): (indexA: number) => T[];
// swap(__, indexB)(indexA, list)
<T>(indexA: number, list: readonly T[]): T[];
};

// swap(indexA, indexB)(list)
export function swap(indexA: number, indexB: number): <T>(list: readonly T[]) => T[];

// swap(__, __, list)
export function swap<T>(
__: 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<T>(
indexA: number,
__: Placeholder,
list: readonly T[]
): (indexB: number) => T[];
// swap(__, __, list)

// swap(__, indexB, list)(indexA)
export function swap<T>(
__: 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<T>(indexA: number, indexB: number, list: readonly T[]): T[];

0 comments on commit c061b8b

Please sign in to comment.