feat(tuples): SplitAt
This commit is contained in:
@@ -162,6 +162,7 @@ type res5 = Pipe<
|
||||
- [x] `ReduceRight<Fn, Init, Tuple>`
|
||||
- [x] `Every<Fn, Tuple>`
|
||||
- [x] `Some<Fn, Tuple>`
|
||||
- [x] `SplitAt<N, Tuple>`
|
||||
- [x] `ToUnion<Tuple>`
|
||||
- [x] `ToIntersection<Tuple>`
|
||||
- [x] `Prepend<X, Tuple>`
|
||||
|
||||
@@ -3,7 +3,6 @@ import { Std } from "../std/Std";
|
||||
import { Tuples } from "../tuples/Tuples";
|
||||
import * as H from "../helpers";
|
||||
import * as Impl from "./impl/strings";
|
||||
import { Functions } from "../functions/Functions";
|
||||
|
||||
export namespace Strings {
|
||||
export type Stringifiable =
|
||||
|
||||
@@ -690,6 +690,39 @@ export namespace Tuples {
|
||||
>;
|
||||
}
|
||||
|
||||
/**
|
||||
* SplitAt takes an index and a tuple, splits the tuple
|
||||
* at the provided index and returns the list of elements
|
||||
* before this index and the list of elements after this
|
||||
* index as a [before[], after[]] tuple.
|
||||
*
|
||||
* @param index - the index at which to split the list
|
||||
* @param tuple - The list to split
|
||||
* @returns A [before[], after[]] tuple.
|
||||
* @example
|
||||
* ```ts
|
||||
* type T0 = Call<Tuples.SplitAt<2>, [1, 2, 3, 4]>; // [[1, 2], [3, 4]]
|
||||
* type T1 = Call<Tuples.SplitAt<2>, [1]>; // [[1], []]
|
||||
* ```
|
||||
*/
|
||||
export type SplitAt<
|
||||
index extends number | unset | _ = unset,
|
||||
tuple extends unknown[] | unset | _ = unset
|
||||
> = PartialApply<SplitAtFn, [index, tuple]>;
|
||||
|
||||
export interface SplitAtFn extends Fn {
|
||||
return: this["args"] extends [
|
||||
infer index extends number,
|
||||
infer tuple extends any[],
|
||||
...any
|
||||
]
|
||||
? [
|
||||
TakeImpl<tuple, Iterator.Iterator<index>>,
|
||||
DropImpl<tuple, Iterator.Iterator<index>>
|
||||
]
|
||||
: never;
|
||||
}
|
||||
|
||||
interface ZipWithMapper<fn extends Fn, arrs extends unknown[][]> extends Fn {
|
||||
return: this["args"] extends [infer Index extends number, ...any]
|
||||
? Apply<fn, Call<Tuples.Map<Tuples.At<Index>, arrs>>>
|
||||
|
||||
@@ -327,6 +327,16 @@ describe("Tuples", () => {
|
||||
type test2 = Expect<Equal<res2, undefined>>;
|
||||
});
|
||||
|
||||
it("SplitAt", () => {
|
||||
type res0 = Call<Tuples.SplitAt<2>, [1, 2, 3, 4]>;
|
||||
// ^?
|
||||
type test0 = Expect<Equal<res0, [[1, 2], [3, 4]]>>;
|
||||
|
||||
type res1 = Call<Tuples.SplitAt<2>, [1]>;
|
||||
// ^?
|
||||
type test1 = Expect<Equal<res1, [[1], []]>>;
|
||||
});
|
||||
|
||||
it("IsEmpty", () => {
|
||||
type res1 = Call<
|
||||
// ^?
|
||||
|
||||
Reference in New Issue
Block a user