UNPKG

3.04 kBTypeScriptView Raw
1import type {BuildTuple, StaticPartOfArray, VariablePartOfArray} from './internal';
2import type {GreaterThanOrEqual} from './greater-than-or-equal';
3import type {Subtract} from './subtract';
4import type {UnknownArray} from './unknown-array';
5
6/**
7The implementation of `SplitArrayByIndex` for fixed length arrays.
8*/
9type SplitFixedArrayByIndex<T extends UnknownArray, SplitIndex extends number> =
10SplitIndex 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/**
19The implementation of `SplitArrayByIndex` for variable length arrays.
20*/
21type 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> =
26SplitIndex 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/**
45Split the given array `T` by the given `SplitIndex`.
46
47@example
48```
49type A = SplitArrayByIndex<[1, 2, 3, 4], 2>;
50// type A = [[1, 2], [3, 4]];
51
52type B = SplitArrayByIndex<[1, 2, 3, 4], 0>;
53// type B = [[], [1, 2, 3, 4]];
54```
55*/
56type 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/**
64Creates a new array type by adding or removing elements at a specified index range in the original array.
65
66Use-case: Replace or insert items in an array type.
67
68Like [`Array#splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) but for types.
69
70@example
71```
72type SomeMonths0 = ['January', 'April', 'June'];
73type Mouths0 = ArraySplice<SomeMonths0, 1, 0, ['Feb', 'March']>;
74//=> type Mouths0 = ['January', 'Feb', 'March', 'April', 'June'];
75
76type SomeMonths1 = ['January', 'April', 'June'];
77type Mouths1 = ArraySplice<SomeMonths1, 1, 1>;
78//=> type Mouths1 = ['January', 'June'];
79
80type SomeMonths2 = ['January', 'Foo', 'April'];
81type Mouths2 = ArraySplice<SomeMonths2, 1, 1, ['Feb', 'March']>;
82//=> type Mouths2 = ['January', 'Feb', 'March', 'April'];
83```
84
85@category Array
86*/
87export 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 // Should never happen
97 : never; // Should never happen