1 | import type {BuildTuple, StaticPartOfArray, VariablePartOfArray} from './internal';
|
2 | import type {GreaterThanOrEqual} from './greater-than-or-equal';
|
3 | import type {Subtract} from './subtract';
|
4 | import type {UnknownArray} from './unknown-array';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | type SplitFixedArrayByIndex<T extends UnknownArray, SplitIndex extends number> =
|
10 | SplitIndex extends 0
|
11 | ? [[], T]
|
12 | : T extends readonly [...BuildTuple<SplitIndex>, ...infer V]
|
13 | ? T extends readonly [...infer U, ...V]
|
14 | ? [U, V]
|
15 | : [never, never]
|
16 | : [never, never];
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | type SplitVariableArrayByIndex<T extends UnknownArray,
|
22 | SplitIndex extends number,
|
23 | T1 = Subtract<SplitIndex, StaticPartOfArray<T>['length']>,
|
24 | T2 = T1 extends number ? BuildTuple<T1, VariablePartOfArray<T>[number]> : [],
|
25 | > =
|
26 | SplitIndex extends 0
|
27 | ? [[], T]
|
28 | : GreaterThanOrEqual<StaticPartOfArray<T>['length'], SplitIndex> extends true
|
29 | ? [
|
30 | SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[0],
|
31 | [
|
32 | ...SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[1],
|
33 | ...VariablePartOfArray<T>,
|
34 | ],
|
35 | ]
|
36 | : [
|
37 | [
|
38 | ...StaticPartOfArray<T>,
|
39 | ...(T2 extends UnknownArray ? T2 : []),
|
40 | ],
|
41 | VariablePartOfArray<T>,
|
42 | ];
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | type SplitArrayByIndex<T extends UnknownArray, SplitIndex extends number> =
|
57 | SplitIndex extends 0
|
58 | ? [[], T]
|
59 | : number extends T['length']
|
60 | ? SplitVariableArrayByIndex<T, SplitIndex>
|
61 | : SplitFixedArrayByIndex<T, SplitIndex>;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | export type ArraySplice<
|
88 | T extends UnknownArray,
|
89 | Start extends number,
|
90 | DeleteCount extends number,
|
91 | Items extends UnknownArray = [],
|
92 | > =
|
93 | SplitArrayByIndex<T, Start> extends [infer U extends UnknownArray, infer V extends UnknownArray]
|
94 | ? SplitArrayByIndex<V, DeleteCount> extends [infer _Deleted extends UnknownArray, infer X extends UnknownArray]
|
95 | ? [...U, ...Items, ...X]
|
96 | : never
|
97 | : never;
|