Skip to content

Commit

Permalink
added swap tuple type
Browse files Browse the repository at this point in the history
  • Loading branch information
hansoksendahl committed Jun 23, 2024
1 parent c7d3921 commit f91dda0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/tuple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './prepend'
export * from './rest'
export * from './reverse'
export * from './slice'
export * from './swap'
export * from './tuple'
Empty file added src/tuple/swap.spec.ts
Empty file.
23 changes: 23 additions & 0 deletions src/tuple/swap.ts
Original file line number Diff line number Diff line change
@@ -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<D> extends LengthProp<A>
? D
: LengthProp<D> extends B
? Swap<A, B, C, [...D, A[C]]>
: LengthProp<D> extends C
? Swap<A, B, C, [...D, A[B]]>
: Swap<A, B, C, [...D, A[LengthProp<D>]]>

0 comments on commit f91dda0

Please sign in to comment.