This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
128 lines (110 loc) · 4.49 KB
/
index.ts
1
2
3
4
5
6
7
8
9
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
59
60
61
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import * as utils from './utils'
/**
* Choose type base on whether or not a tuple is finite
* @example IsFinite<[0, 1, 2]> → true
* @example IsFinite<[0, 1, 2, ...number[]]> → false
* @example IsFinite<[0], 'Finite', 'Infinite'> → 'Finite'
* @example IsFinite<[0, ...number[]], 'Finite', 'Infinite'> → Infinite
*/
export type IsFinite<Tuple extends any[], Finite = true, Infinite = false> =
utils.IsFinite<Tuple, Finite, Infinite>
/**
* Get type of first element
* @example First<[0, 1, 2]> → 0
*/
export type First<Tuple extends [any, ...any[]]> = Tuple[0]
/**
* Get type of last element
* @example Last<[0, 1, 2]> → 2
*/
export type Last<Tuple extends any[]> = utils.Last<Tuple>
/**
* Drop the first element
* @example Tail<[0, 1, 2, 3]> → [1, 2, 3]
*/
export type Tail<Tuple extends any[]> = utils.Tail<Tuple>
/**
* Add an element to the end of a tuple
* @example Append<[0, 1, 2], 'new'> → [0, 1, 2, 'new']
*/
export type Append<Tuple extends any[], Addend> = Reverse<Prepend<Reverse<Tuple>, Addend>>
/**
* Add an element to the beginning of a tuple
* @example Prepend<[0, 1, 2], 'new'> → ['new', 0, 1, 2]
*/
export type Prepend<Tuple extends any[], Addend> = utils.Prepend<Tuple, Addend>
/**
* Reverse a tuple
* @example Reverse<[0, 1, 2]> → [2, 1, 0]
*/
export type Reverse<Tuple extends any[]> = utils.Reverse<Tuple>
/**
* Concat two tuple into one
* @example Concat<[0, 1, 2], ['a', 'b', 'c']> → [0, 1, 2, 'a', 'b', 'c']
*/
export type Concat<Left extends any[], Right extends any[]> = utils.Concat<Left, Right>
/**
* Repeat a certain type into a tuple
* @example Repeat<'foo', 4> → ['foo', 'foo', 'foo', 'foo']
* @warning To avoid potential infinite loop, Count must be an integer greater than or equal to 0
*/
export type Repeat<Type, Count extends number> = utils.Repeat<Type, Count, []>
/**
* Concat multiple tuples
* @example ConcatMultiple<[], [0], [1, 2], [3, 4, 5]> → [0, 1, 2, 3, 4, 5]
*/
export type ConcatMultiple<TupleSet extends any[][]> = utils.ConcatMultiple<TupleSet>
/**
* Drop `Quantity` first elements of a tuple
* @example Drop<['a', 'b', 'c', 'd', 'e'], 2> → ['c', 'd', 'e']
* @example Drop<['a', 'b', 'c', 'd', 'e', ...string[]], 2> → ['c', 'd', 'e', ...string[]]
* @example Drop<['a', 'b', 'c', 'd', 'e', ...string[]], 10> → string[]
*/
export type Drop<Tuple extends any[], Quantity extends number> = utils.Drop<Tuple, Quantity>
/**
* Slice a tuple
* @example SliceStartQuantity<[0, 1, 2, 3, 4, 5, 6], 2, 3> → [2, 3, 4]
* @example SliceStartQuantity<[0, 1, 2, 3, 4, 5, 6], 2, 9> → [2, 3, 4, 5, 6]
*/
export type SliceStartQuantity<
Tuple extends any[],
Start extends number,
Quantity extends number
> = utils.SliceStartQuantity<Tuple, Start, Quantity>
/**
* Fill a tuple of types
* @example FillTuple<[0, 1, 2], 'x'> → ['x', 'x', 'x']
* @example FillTuple<any[], 'x'> → 'x'[]
*/
export type FillTuple<Tuple extends any[], Replacement> = utils.FillTuple<Tuple, Replacement>
/**
* Compare length of two tuple
* @example CompareLength<[0, 1, 2], ['a', 'b', 'c']> → 'equal'
* @example CompareLength<[0, 1], ['a', 'b', 'c']> → 'shorterLeft'
* @example CompareLength<[0, 1, 2], ['a', 'b']> → 'shorterRight'
*/
export type CompareLength<Left extends any[], Right extends any[]> = utils.CompareLength<Left, Right>
/**
* Sort two tuples in order of [shorter, longer]
* @example SortTwoTuple<[0, 1, 2, 3], ['a', 'b']> → [['a', 'b'], [0, 1, 2, 3]]
* @example SortTwoTuple<[0, 1], ['a', 'b', 'c', 'd']> → [[0, 1], ['a', 'b', 'c', 'd']]
* @example SortTwoTuple<[0, 1, 2], ['a', 'b', 'c']> → [[0, 1, 2], ['a', 'b', 'c']]
* @example SortTwoTuple<[0, 1, 2], ['a', 'b', 'c'], 'EQUAL'> → 'EQUAL'
*/
export type SortTwoTuple<Left extends any[], Right extends any[], WhenEqual = [Left, Right]> =
utils.SortTwoTuple<Left, Right, WhenEqual>
/**
* Find shortest tuple in a set of tuples
* @example ShortestTuple<[[0, 1, 2], [true, false], ['a', 'b', 'c', 'd']]> → [true, false]
*/
export type ShortestTuple<TupleSet extends [any[], ...any[][]]> = utils.ShortestTuple<TupleSet>
/**
* Find shortest tuple in a set of tuples
* @example LongestTuple<[[0, 1, 2], [true, false], ['a', 'b', 'c', 'd']]> → ['a', 'b', 'c', 'd']
*/
export type LongestTuple<TupleSet extends [any[], ...any[][]]> = utils.LongestTuple<TupleSet>
/**
* Filter tuple elements thats match the mask
* @example FilterTuple<[1, 2, true, '3'], string | number> → [1, 2, "3"]
*/
export type FilterTuple<TupleSet extends any[], Mask> = utils.FilterTuple<TupleSet, Mask>