From f91dda0d7da532925db761791046826368e80bc7 Mon Sep 17 00:00:00 2001 From: Hans Oksendahl Date: Sun, 23 Jun 2024 16:27:42 -0700 Subject: [PATCH] added swap tuple type --- src/tuple/index.ts | 1 + src/tuple/swap.spec.ts | 0 src/tuple/swap.ts | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 src/tuple/swap.spec.ts create mode 100644 src/tuple/swap.ts diff --git a/src/tuple/index.ts b/src/tuple/index.ts index 03cc5ce1..7d49191e 100644 --- a/src/tuple/index.ts +++ b/src/tuple/index.ts @@ -9,4 +9,5 @@ export * from './prepend' export * from './rest' export * from './reverse' export * from './slice' +export * from './swap' export * from './tuple' diff --git a/src/tuple/swap.spec.ts b/src/tuple/swap.spec.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/tuple/swap.ts b/src/tuple/swap.ts new file mode 100644 index 00000000..bf4aacd9 --- /dev/null +++ b/src/tuple/swap.ts @@ -0,0 +1,23 @@ +import type { LengthProp } from '~/object/length-prop' + +/** + * Swaps two elements in an array. + * + * @typeParam A - The input array. + * @typeParam B - The index of the first element to swap. + * @typeParam C - The index of the second element to swap. + * @typeParam D - The resulting array after swapping the elements. + */ +type Swap< + A extends any[], + B extends number, + C extends number, + D extends any[] = [], +> = + LengthProp extends LengthProp + ? D + : LengthProp extends B + ? Swap + : LengthProp extends C + ? Swap + : Swap]]>