1 | import type { TypedArray } from "./typedarray.js";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export type ArrayValue<T extends unknown[]> = T[0];
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export type DeepArrayValue<T> = T extends unknown[] ? DeepArrayValue<T[0]> : T extends TypedArray ? number : T;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export type Tuple<T, N extends number> = [T, ...T[]] & {
|
19 | length: N;
|
20 | } & Iterable<T>;
|
21 |
|
22 |
|
23 |
|
24 | export type TupleLength<T extends unknown[]> = T["length"];
|
25 |
|
26 |
|
27 |
|
28 | export type IsEmpty<T extends unknown[]> = T extends [] ? 1 : 0;
|
29 |
|
30 |
|
31 |
|
32 | export type Head<T extends unknown[]> = T extends [infer A, ...unknown[]] ? A : never;
|
33 |
|
34 |
|
35 |
|
36 | export type Tail<T extends unknown[]> = T extends [unknown, ...infer A] ? A : never;
|
37 |
|
38 |
|
39 |
|
40 | export type Prepend<T, U extends unknown[]> = [T, ...U];
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | type ReverseReducer<T extends unknown[], C extends unknown[]> = {
|
46 | 0: C;
|
47 | 1: Prepend<Head<T>, C>;
|
48 | 2: ReverseReducer<Tail<T>, Prepend<Head<T>, C>>;
|
49 | }[Tail<T> extends [] ? (Head<T> extends never ? 0 : 1) : 2];
|
50 |
|
51 |
|
52 |
|
53 | export type Reverse<T extends unknown[]> = ReverseReducer<T, []>;
|
54 |
|
55 |
|
56 |
|
57 | export type Last<T extends unknown[]> = {
|
58 | 0: Last<Tail<T>>;
|
59 | 1: Head<T>;
|
60 | }[IsEmpty<Tail<T>>];
|
61 |
|
62 |
|
63 |
|
64 | export type ButLast<T extends unknown[], C extends unknown[] = []> = {
|
65 | 0: ButLast<Tail<T>, Prepend<Head<T>, C>>;
|
66 | 1: Reverse<C>;
|
67 | }[IsEmpty<Tail<T>>];
|
68 | export {};
|
69 |
|
\ | No newline at end of file |