From ee672b763576ff1198db2b062199821121bd4358 Mon Sep 17 00:00:00 2001 From: Dustin Kane Date: Fri, 2 Jun 2023 10:12:52 -0700 Subject: [PATCH 1/2] Adding typing for swap function --- types/swap.d.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 types/swap.d.ts diff --git a/types/swap.d.ts b/types/swap.d.ts new file mode 100644 index 0000000..a781158 --- /dev/null +++ b/types/swap.d.ts @@ -0,0 +1,41 @@ +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)(__, list)(indexB) + (__: Placeholder, list: readonly T[]): (indexB: number) => T[]; + // swap(indexA)(indexB)(list) + (indexB: number): (list: readonly T[]) => T[]; +}; + +// swap(__, indexB, list)(indexA) +export function swap( + __: Placeholder, + indexB: number, + list: readonly T[] +): (indexA: number) => T[]; +// swap(indexA, __, list)(indexB) +export function swap( + indexA: number, + __: Placeholder, + list: readonly T[] +): (indexB: number) => T[]; +// swap(__, __, list) +export function swap( + __: Placeholder, + __2: Placeholder, + 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[]; +}; From ffc8e8d802b7c4ad3c08b41fb5071d1c50c53082 Mon Sep 17 00:00:00 2001 From: Dustin Kane Date: Fri, 2 Jun 2023 10:50:15 -0700 Subject: [PATCH 2/2] Fixing ordering and adding tests --- CONTRIBUTING.md | 4 ++-- test/swap.test.ts | 30 ++++++++++++++++++++++++ types/swap.d.ts | 59 ++++++++++++++++++++++++++++++----------------- 3 files changed, 70 insertions(+), 23 deletions(-) create mode 100644 test/swap.test.ts diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4d4b18b..1b0337d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,7 +13,7 @@ 1. Run `npm install` to install needed local dependencies. -1. Run `npm run build` first, then `npm run test` and address any errors. Preferably, fix commits in place +1. Run `npm run build` first, then `npm run test` and `npm run lint` and address any errors. Preferably, fix commits in place using `git rebase` or `git commit --amend` to make the changes easier to review and to keep the history tidy. @@ -21,6 +21,6 @@ $ git push origin -2. Open a pull request. +1. Open a pull request. TODO: Explain the convention for creating types for ramda diff --git a/test/swap.test.ts b/test/swap.test.ts new file mode 100644 index 0000000..cf74f02 --- /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..f6032fb 100644 --- a/types/swap.d.ts +++ b/types/swap.d.ts @@ -1,41 +1,58 @@ 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[];