feat: add reverse

This commit is contained in:
Mert Ciflikli
2023-04-23 14:16:29 +02:00
parent 7a1ff51cbe
commit 0968fcf200
3 changed files with 27 additions and 0 deletions

View File

@@ -160,6 +160,7 @@ type res5 = Pipe<
- [x] `Filter<Fn, Tuple>`
- [x] `Reduce<Fn, Init, Tuple>`
- [x] `ReduceRight<Fn, Init, Tuple>`
- [x] `Reverse<Tuple>`
- [x] `Every<Fn, Tuple>`
- [x] `Some<Fn, Tuple>`
- [x] `ToUnion<Tuple>`

View File

@@ -260,6 +260,26 @@ export namespace Tuples {
return: ReduceImpl<Extract<this["arg0"], Fn>, this["arg1"], this["arg2"]>;
}
type ReverseImpl<tuple> = tuple extends [infer first, ...infer rest]
? [...ReverseImpl<rest>, first]
: [];
/**
* Reverse a tuple.
* @params args[0] - A tuple.
* @return Reversed tuple.
* @example
* ```ts
* type T0 = Call<T.Reverse,[1,2,3]>; // [3,2,1]
* ```
*/
export type Reverse<tuple extends readonly any[] | unset = unset> =
PartialApply<ReverseFn, [tuple]>;
interface ReverseFn extends Fn {
return: ReverseImpl<this["arg0"]>;
}
type ReduceRightImpl<xs, acc, fn extends Fn> = xs extends [
...infer rest,
infer last

View File

@@ -148,6 +148,12 @@ describe("Tuples", () => {
type tes3 = Expect<Equal<res3, "b">>;
});
it("Reverse", () => {
type res1 = Call<Tuples.Reverse, ["a", "b", "c", 2, "d"]>;
// ^?
type tes1 = Expect<Equal<res1, ["d", 2, "c", "b", "a"]>>;
});
it("Drop", () => {
type res1 = Call<Tuples.Drop<1>, ["a", "b", "c", 2, "d"]>;
// ^?