1 | import type {Sum} from './sum';
|
2 | import type {LessThanOrEqual} from './less-than-or-equal';
|
3 | import type {GreaterThanOrEqual} from './greater-than-or-equal';
|
4 | import type {GreaterThan} from './greater-than';
|
5 | import type {IsNegative} from './numeric';
|
6 | import type {Not, TupleMin} from './internal';
|
7 | import type {IsEqual} from './is-equal';
|
8 | import type {And} from './and';
|
9 | import type {ArraySplice} from './array-splice';
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export type ArraySlice<
|
59 | Array_ extends readonly unknown[],
|
60 | Start extends number = never,
|
61 | End extends number = never,
|
62 | > = And<IsEqual<Start, never>, IsEqual<End, never>> extends true
|
63 | ? Array_
|
64 | : number extends Array_['length']
|
65 | ? VariableLengthArraySliceHelper<Array_, Start, End>
|
66 | : ArraySliceHelper<Array_, IsEqual<Start, never> extends true ? 0 : Start, IsEqual<End, never> extends true ? Array_['length'] : End>;
|
67 |
|
68 | type VariableLengthArraySliceHelper<
|
69 | Array_ extends readonly unknown[],
|
70 | Start extends number,
|
71 | End extends number,
|
72 | > = And<Not<IsNegative<Start>>, IsEqual<End, never>> extends true
|
73 | ? ArraySplice<Array_, 0, Start>
|
74 | : And<
|
75 | And<Not<IsNegative<Start>>, Not<IsNegative<End>>>,
|
76 | IsEqual<GreaterThan<End, Start>, true>
|
77 | > extends true
|
78 | ? ArraySliceByPositiveIndex<Array_, Start, End>
|
79 | : [];
|
80 |
|
81 | type ArraySliceHelper<
|
82 | Array_ extends readonly unknown[],
|
83 | Start extends number = 0,
|
84 | End extends number = Array_['length'],
|
85 | TraversedElement extends Array<Array_[number]> = [],
|
86 | Result extends Array<Array_[number]> = [],
|
87 | ArrayLength extends number = Array_['length'],
|
88 | PositiveS extends number = IsNegative<Start> extends true
|
89 | ? Sum<ArrayLength, Start> extends infer AddResult extends number
|
90 | ? number extends AddResult
|
91 | ? 0
|
92 | : AddResult
|
93 | : never
|
94 | : Start,
|
95 | PositiveE extends number = IsNegative<End> extends true ? Sum<ArrayLength, End> : End,
|
96 | > = true extends [IsNegative<PositiveS>, LessThanOrEqual<PositiveE, PositiveS>, GreaterThanOrEqual<PositiveS, ArrayLength>][number]
|
97 | ? []
|
98 | : ArraySliceByPositiveIndex<Array_, TupleMin<[PositiveS, ArrayLength]>, TupleMin<[PositiveE, ArrayLength]>>;
|
99 |
|
100 | type ArraySliceByPositiveIndex<
|
101 | Array_ extends readonly unknown[],
|
102 | Start extends number,
|
103 | End extends number,
|
104 | Result extends Array<Array_[number]> = [],
|
105 | > = Start extends End
|
106 | ? Result
|
107 | : ArraySliceByPositiveIndex<Array_, Sum<Start, 1>, End, [...Result, Array_[Start]]>;
|