type-plus 7.3.0 → 7.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (248) hide show
  1. package/README.md +131 -156
  2. package/cjs/any/any_type.d.ts +8 -8
  3. package/cjs/any/any_type.d.ts.map +1 -1
  4. package/cjs/array/array.at.d.ts +3 -1
  5. package/cjs/array/array.at.d.ts.map +1 -1
  6. package/cjs/array/array.find.d.ts +3 -1
  7. package/cjs/array/array.find.d.ts.map +1 -1
  8. package/cjs/array/array.find_last.d.ts +3 -1
  9. package/cjs/array/array.find_last.d.ts.map +1 -1
  10. package/cjs/array/array.some.d.ts +1 -1
  11. package/cjs/array/array.some.d.ts.map +1 -1
  12. package/cjs/array/{array.concat.d.ts → array_plus.concat.d.ts} +4 -1
  13. package/cjs/array/array_plus.concat.d.ts.map +1 -0
  14. package/cjs/array/{array.concat.js → array_plus.concat.js} +1 -1
  15. package/cjs/array/array_plus.concat.js.map +1 -0
  16. package/cjs/array/array_plus.d.ts +3 -2
  17. package/cjs/array/array_plus.d.ts.map +1 -1
  18. package/cjs/array/array_plus.filter.d.ts +2 -0
  19. package/cjs/array/array_plus.filter.d.ts.map +1 -0
  20. package/cjs/array/array_plus.filter.js +3 -0
  21. package/cjs/array/array_plus.filter.js.map +1 -0
  22. package/cjs/array/array_plus.pad_start.d.ts +5 -0
  23. package/cjs/array/array_plus.pad_start.d.ts.map +1 -0
  24. package/cjs/array/array_plus.pad_start.js +3 -0
  25. package/cjs/array/array_plus.pad_start.js.map +1 -0
  26. package/cjs/array/array_type.d.ts +30 -12
  27. package/cjs/array/array_type.d.ts.map +1 -1
  28. package/cjs/array/filter.d.ts +24 -4
  29. package/cjs/array/filter.d.ts.map +1 -1
  30. package/cjs/array/head.d.ts +18 -1
  31. package/cjs/array/head.d.ts.map +1 -1
  32. package/cjs/array/intersect_of_props.d.ts +5 -3
  33. package/cjs/array/intersect_of_props.d.ts.map +1 -1
  34. package/cjs/array/last.d.ts +16 -2
  35. package/cjs/array/last.d.ts.map +1 -1
  36. package/cjs/array/literal_array.d.ts +5 -0
  37. package/cjs/array/literal_array.d.ts.map +1 -1
  38. package/cjs/array/literal_array.js +5 -0
  39. package/cjs/array/literal_array.js.map +1 -1
  40. package/cjs/array/pad_start.d.ts +5 -5
  41. package/cjs/array/pad_start.d.ts.map +1 -1
  42. package/cjs/array/reduce_while.d.ts +6 -0
  43. package/cjs/array/reduce_while.d.ts.map +1 -1
  44. package/cjs/array/reduce_while.js +6 -0
  45. package/cjs/array/reduce_while.js.map +1 -1
  46. package/cjs/array/reverse.d.ts +5 -0
  47. package/cjs/array/reverse.d.ts.map +1 -1
  48. package/cjs/array/union_of_props.d.ts +1 -1
  49. package/cjs/array/union_of_props.d.ts.map +1 -1
  50. package/cjs/functional/compose.d.ts +2 -1
  51. package/cjs/functional/compose.d.ts.map +1 -1
  52. package/cjs/functional/compose.js.map +1 -1
  53. package/cjs/functional/context.d.ts.map +1 -1
  54. package/cjs/index.d.ts +19 -3
  55. package/cjs/index.d.ts.map +1 -1
  56. package/cjs/index.js +9 -3
  57. package/cjs/index.js.map +1 -1
  58. package/cjs/math/numeric_struct.d.ts +1 -1
  59. package/cjs/math/numeric_struct.d.ts.map +1 -1
  60. package/cjs/mix_types/any_or_never_type.d.ts +18 -10
  61. package/cjs/mix_types/any_or_never_type.d.ts.map +1 -1
  62. package/cjs/nodejs/isNodeError.d.ts.map +1 -1
  63. package/cjs/nominal/brand.d.ts.map +1 -1
  64. package/cjs/nominal/flavor.d.ts.map +1 -1
  65. package/cjs/testing/test_type.d.ts +44 -1
  66. package/cjs/testing/test_type.d.ts.map +1 -1
  67. package/cjs/tuple/common_prop_keys.d.ts +29 -0
  68. package/cjs/tuple/common_prop_keys.d.ts.map +1 -0
  69. package/cjs/tuple/common_prop_keys.js.map +1 -0
  70. package/cjs/tuple/create_tuple.d.ts.map +1 -1
  71. package/cjs/tuple/drop.d.ts +72 -0
  72. package/cjs/tuple/drop.d.ts.map +1 -0
  73. package/cjs/{array → tuple}/drop.js +3 -1
  74. package/cjs/tuple/drop.js.map +1 -0
  75. package/{esm/array → cjs/tuple}/tail.d.ts +1 -1
  76. package/cjs/{array → tuple}/tail.d.ts.map +1 -1
  77. package/cjs/{array → tuple}/tail.js.map +1 -1
  78. package/cjs/tuple/tuple_plus.d.ts +2 -1
  79. package/cjs/tuple/tuple_plus.d.ts.map +1 -1
  80. package/cjs/tuple/tuple_plus.filter.d.ts +13 -0
  81. package/cjs/tuple/tuple_plus.filter.d.ts.map +1 -0
  82. package/cjs/tuple/tuple_plus.filter.js +3 -0
  83. package/cjs/tuple/tuple_plus.filter.js.map +1 -0
  84. package/cjs/tuple/tuple_plus.js +0 -15
  85. package/cjs/tuple/tuple_plus.js.map +1 -1
  86. package/cjs/tuple/tuple_plus.pad_start.d.ts +8 -3
  87. package/cjs/tuple/tuple_plus.pad_start.d.ts.map +1 -1
  88. package/esm/any/any_type.d.ts +8 -8
  89. package/esm/any/any_type.d.ts.map +1 -1
  90. package/esm/array/array.at.d.ts +3 -1
  91. package/esm/array/array.at.d.ts.map +1 -1
  92. package/esm/array/array.find.d.ts +3 -1
  93. package/esm/array/array.find.d.ts.map +1 -1
  94. package/esm/array/array.find_last.d.ts +3 -1
  95. package/esm/array/array.find_last.d.ts.map +1 -1
  96. package/esm/array/array.some.d.ts +1 -1
  97. package/esm/array/array.some.d.ts.map +1 -1
  98. package/esm/array/{array.concat.d.ts → array_plus.concat.d.ts} +4 -1
  99. package/esm/array/array_plus.concat.d.ts.map +1 -0
  100. package/esm/array/array_plus.concat.js +2 -0
  101. package/esm/array/array_plus.concat.js.map +1 -0
  102. package/esm/array/array_plus.d.ts +3 -2
  103. package/esm/array/array_plus.d.ts.map +1 -1
  104. package/esm/array/array_plus.filter.d.ts +2 -0
  105. package/esm/array/array_plus.filter.d.ts.map +1 -0
  106. package/esm/array/array_plus.filter.js +2 -0
  107. package/esm/array/array_plus.filter.js.map +1 -0
  108. package/esm/array/array_plus.pad_start.d.ts +5 -0
  109. package/esm/array/array_plus.pad_start.d.ts.map +1 -0
  110. package/esm/array/array_plus.pad_start.js +2 -0
  111. package/esm/array/array_plus.pad_start.js.map +1 -0
  112. package/esm/array/array_type.d.ts +30 -12
  113. package/esm/array/array_type.d.ts.map +1 -1
  114. package/esm/array/filter.d.ts +24 -4
  115. package/esm/array/filter.d.ts.map +1 -1
  116. package/esm/array/head.d.ts +18 -1
  117. package/esm/array/head.d.ts.map +1 -1
  118. package/esm/array/intersect_of_props.d.ts +5 -3
  119. package/esm/array/intersect_of_props.d.ts.map +1 -1
  120. package/esm/array/last.d.ts +16 -2
  121. package/esm/array/last.d.ts.map +1 -1
  122. package/esm/array/literal_array.d.ts +5 -0
  123. package/esm/array/literal_array.d.ts.map +1 -1
  124. package/esm/array/literal_array.js +5 -0
  125. package/esm/array/literal_array.js.map +1 -1
  126. package/esm/array/pad_start.d.ts +5 -5
  127. package/esm/array/pad_start.d.ts.map +1 -1
  128. package/esm/array/reduce_while.d.ts +6 -0
  129. package/esm/array/reduce_while.d.ts.map +1 -1
  130. package/esm/array/reduce_while.js +6 -0
  131. package/esm/array/reduce_while.js.map +1 -1
  132. package/esm/array/reverse.d.ts +5 -0
  133. package/esm/array/reverse.d.ts.map +1 -1
  134. package/esm/array/union_of_props.d.ts +1 -1
  135. package/esm/array/union_of_props.d.ts.map +1 -1
  136. package/esm/functional/compose.d.ts +2 -1
  137. package/esm/functional/compose.d.ts.map +1 -1
  138. package/esm/functional/compose.js.map +1 -1
  139. package/esm/functional/context.d.ts.map +1 -1
  140. package/esm/index.d.ts +19 -3
  141. package/esm/index.d.ts.map +1 -1
  142. package/esm/index.js +4 -2
  143. package/esm/index.js.map +1 -1
  144. package/esm/math/numeric_struct.d.ts +1 -1
  145. package/esm/math/numeric_struct.d.ts.map +1 -1
  146. package/esm/mix_types/any_or_never_type.d.ts +18 -10
  147. package/esm/mix_types/any_or_never_type.d.ts.map +1 -1
  148. package/esm/nodejs/isNodeError.d.ts.map +1 -1
  149. package/esm/nominal/brand.d.ts.map +1 -1
  150. package/esm/nominal/flavor.d.ts.map +1 -1
  151. package/esm/testing/test_type.d.ts +44 -1
  152. package/esm/testing/test_type.d.ts.map +1 -1
  153. package/esm/tuple/common_prop_keys.d.ts +29 -0
  154. package/esm/tuple/common_prop_keys.d.ts.map +1 -0
  155. package/esm/tuple/common_prop_keys.js.map +1 -0
  156. package/esm/tuple/create_tuple.d.ts.map +1 -1
  157. package/esm/tuple/drop.d.ts +72 -0
  158. package/esm/tuple/drop.d.ts.map +1 -0
  159. package/esm/tuple/drop.js +9 -0
  160. package/esm/tuple/drop.js.map +1 -0
  161. package/{cjs/array → esm/tuple}/tail.d.ts +1 -1
  162. package/esm/{array → tuple}/tail.d.ts.map +1 -1
  163. package/esm/{array → tuple}/tail.js.map +1 -1
  164. package/esm/tuple/tuple_plus.d.ts +2 -1
  165. package/esm/tuple/tuple_plus.d.ts.map +1 -1
  166. package/esm/tuple/tuple_plus.filter.d.ts +13 -0
  167. package/esm/tuple/tuple_plus.filter.d.ts.map +1 -0
  168. package/esm/tuple/tuple_plus.filter.js +2 -0
  169. package/esm/tuple/tuple_plus.filter.js.map +1 -0
  170. package/esm/tuple/tuple_plus.js +1 -1
  171. package/esm/tuple/tuple_plus.js.map +1 -1
  172. package/esm/tuple/tuple_plus.pad_start.d.ts +8 -3
  173. package/esm/tuple/tuple_plus.pad_start.d.ts.map +1 -1
  174. package/package.json +15 -11
  175. package/ts/any/any_type.ts +8 -9
  176. package/ts/any/readme.md +91 -7
  177. package/ts/array/array.at.ts +3 -1
  178. package/ts/array/array.find.ts +3 -1
  179. package/ts/array/array.find_last.ts +3 -1
  180. package/ts/array/array.some.ts +1 -1
  181. package/ts/array/{array.concat.ts → array_plus.concat.ts} +3 -0
  182. package/ts/array/array_plus.filter.ts +5 -0
  183. package/ts/array/array_plus.pad_start.ts +13 -0
  184. package/ts/array/array_plus.ts +3 -2
  185. package/ts/array/array_type.ts +30 -12
  186. package/ts/array/filter.ts +27 -22
  187. package/ts/array/head.ts +18 -1
  188. package/ts/array/intersect_of_props.ts +7 -3
  189. package/ts/array/last.ts +16 -2
  190. package/ts/array/literal_array.ts +5 -0
  191. package/ts/array/pad_start.ts +6 -10
  192. package/ts/array/readme.md +181 -16
  193. package/ts/array/reduce_while.ts +6 -0
  194. package/ts/array/reverse.ts +5 -0
  195. package/ts/array/union_of_props.ts +1 -1
  196. package/ts/assertion/readme.md +3 -3
  197. package/ts/functional/compose.ts +2 -1
  198. package/ts/functional/context.ts +1 -1
  199. package/ts/index.ts +22 -3
  200. package/ts/math/numeric_struct.ts +9 -9
  201. package/ts/mix_types/any_or_never_type.ts +18 -10
  202. package/ts/mix_types/readme.md +40 -0
  203. package/ts/nodejs/isNodeError.ts +13 -13
  204. package/ts/nominal/brand.ts +1 -1
  205. package/ts/nominal/flavor.ts +1 -1
  206. package/ts/testing/readme.md +30 -0
  207. package/ts/testing/test_type.ts +90 -47
  208. package/ts/tuple/common_prop_keys.ts +55 -0
  209. package/ts/tuple/create_tuple.ts +9 -9
  210. package/ts/tuple/drop.ts +124 -0
  211. package/ts/tuple/readme.md +116 -1
  212. package/ts/{array → tuple}/tail.ts +1 -1
  213. package/ts/tuple/tuple_plus.filter.ts +29 -0
  214. package/ts/tuple/tuple_plus.pad_start.ts +26 -13
  215. package/ts/tuple/tuple_plus.ts +2 -1
  216. package/cjs/array/array.concat.d.ts.map +0 -1
  217. package/cjs/array/array.concat.js.map +0 -1
  218. package/cjs/array/common_prop_keys.d.ts +0 -11
  219. package/cjs/array/common_prop_keys.d.ts.map +0 -1
  220. package/cjs/array/common_prop_keys.js.map +0 -1
  221. package/cjs/array/drop.d.ts +0 -19
  222. package/cjs/array/drop.d.ts.map +0 -1
  223. package/cjs/array/drop.js.map +0 -1
  224. package/cjs/array/index.d.ts +0 -17
  225. package/cjs/array/index.d.ts.map +0 -1
  226. package/cjs/array/index.js +0 -33
  227. package/cjs/array/index.js.map +0 -1
  228. package/esm/array/array.concat.d.ts.map +0 -1
  229. package/esm/array/array.concat.js +0 -2
  230. package/esm/array/array.concat.js.map +0 -1
  231. package/esm/array/common_prop_keys.d.ts +0 -11
  232. package/esm/array/common_prop_keys.d.ts.map +0 -1
  233. package/esm/array/common_prop_keys.js.map +0 -1
  234. package/esm/array/drop.d.ts +0 -19
  235. package/esm/array/drop.d.ts.map +0 -1
  236. package/esm/array/drop.js +0 -7
  237. package/esm/array/drop.js.map +0 -1
  238. package/esm/array/index.d.ts +0 -17
  239. package/esm/array/index.d.ts.map +0 -1
  240. package/esm/array/index.js +0 -17
  241. package/esm/array/index.js.map +0 -1
  242. package/ts/array/common_prop_keys.ts +0 -18
  243. package/ts/array/drop.ts +0 -71
  244. package/ts/array/index.ts +0 -16
  245. /package/cjs/{array → tuple}/common_prop_keys.js +0 -0
  246. /package/cjs/{array → tuple}/tail.js +0 -0
  247. /package/esm/{array → tuple}/common_prop_keys.js +0 -0
  248. /package/esm/{array → tuple}/tail.js +0 -0
package/ts/any/readme.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Any
2
2
 
3
- `any` is one of the two top type in TypeScript. It is the supertype of all types. It is a way to opt-out of type checking and let the values pass through compile-time checks.
3
+ `any` is one of the two top types in TypeScript.
4
+ It is a supertype of all types.
5
+ It is a way to opt-out of type checking and let the values pass through compile-time checks.
4
6
 
5
7
  ## Type Checking
6
8
 
@@ -10,14 +12,96 @@ They are strict type checks, meaning they match only the type `any`.
10
12
  Union and intersections are not a factor here they are resolved to `any`,
11
13
  except `any & never` which is `never`.
12
14
 
13
- - [`AnyType<T, Then = T, Else = never>`](any_type.ts#L15): check if `T` is exactly `any`.
14
- - [`IsAny<T, Then = true, Else = false`](any_type.ts#L30): is `T` exactly `any`.
15
- - [`NotAnyType<T, Then = T, Else = never>`](any_type.ts#L45): check if `T` is not exactly `any`.
16
- - [`IsNotAny<T, Then = true, Else = false>`](any_type.ts#L60): is `T` not exactly `any`.
15
+ ### [AnyType](./any.spec.ts#15)
17
16
 
18
- ## Type Utilities
17
+ `AnyType<T, Then = T, Else = never>`
19
18
 
20
- - [`IsAnyOrNever<T, Then = true, Else = false>`](any_or_never.ts#L17)]: is `T` type `any` or `never`.
19
+ 🌪️ *filter*
20
+
21
+ Filter `T` to ensure it is exactly `any`.
22
+
23
+ ```ts
24
+ import type { AnyType } from 'type-plus'
25
+
26
+ type R = AnyType<any> // any
27
+
28
+ type R = AnyType<never> // never
29
+ type R = AnyType<unknown> // never
30
+ type R = AnyType<string | boolean> // never
31
+ ```
32
+
33
+ ### [IsAny](./any.spec.ts#31)
34
+
35
+ `IsAny<T, Then = true, Else = false>`
36
+
37
+ 🎭 *validate*
38
+
39
+ Validate if `T` is exactly `any`.
40
+
41
+ ```ts
42
+ import type { IsAny } from 'type-plus'
43
+
44
+ type R = IsAny<any> // true
45
+
46
+ type R = IsAny<never> // false
47
+ type R = IsAny<unknown> // false
48
+ type R = IsAny<string | boolean> // false
49
+ ```
50
+
51
+ ### [NotAnyType](./any.spec.ts#47)
52
+
53
+ `NotAnyType<T, Then = T, Else = never>`
54
+
55
+ 🌪️ *filter*
56
+
57
+ Filter `T` to ensure it is not exactly `any`.
58
+
59
+ ```ts
60
+ import type { NotAnyType } from 'type-plus'
61
+
62
+ type R = NotAnyType<any> // never
63
+
64
+ type R = NotAnyType<never> // never
65
+ type R = NotAnyType<unknown> // never
66
+ type R = NotAnyType<string | boolean> // string | boolean
67
+ ```
68
+
69
+ ### [IsNotAny](./any.spec.ts#65)
70
+
71
+ `IsNotAny<T, Then = true, Else = false>`
72
+
73
+ 🎭 *validate*
74
+
75
+ Validate if `T` is not exactly `any`.
76
+
77
+ ```ts
78
+ import type { IsNotAny } from 'type-plus'
79
+
80
+ type R = IsNotAny<any> // false
81
+
82
+ type R = IsNotAny<never> // true
83
+ type R = IsNotAny<unknown> // true
84
+ type R = IsNotAny<string | boolean> // true
85
+ ```
86
+
87
+ ## Trivia
88
+
89
+ > Both `any` and `unknown` are top types?
90
+
91
+ Well, yeah:
92
+
93
+ ```ts
94
+ type A = any extends unknown ? 1 : 2 // 1
95
+ type B = unknown extends any ? 1 : 2 // 1
96
+ ```
97
+
98
+ > Aren't using `any` is bad?
99
+
100
+ If you use it to simply tell TypeScript to shut up because you are lazy, then yes.
101
+
102
+ But it is ok to use `any` in many cases, as the type system of TypeScript is not sounded.
103
+
104
+ There are many cases it is not possible to induce the type correctly.
21
105
 
22
106
  ## References
23
107
 
@@ -3,7 +3,9 @@ import type { TupleType } from '../tuple/tuple_type.js'
3
3
  import type { IndexAt } from './array_index.js'
4
4
 
5
5
  /**
6
- * Gets the type of the array or tuple at index `N`.
6
+ * 🦴 *utilities*
7
+ *
8
+ * Gets the type of the array or tuple at positive or negative index `N`.
7
9
  *
8
10
  * Like `Array.at()`, this type supports negative numbers.
9
11
  *
@@ -1,7 +1,9 @@
1
1
  import type { TupleType } from '../tuple/tuple_type.js'
2
2
 
3
3
  /**
4
- * Returns the first type in the array or tuple that matches the `Criteria`.
4
+ * 🦴 *utilities*
5
+ *
6
+ * Gets the first type in the array or tuple that matches the `Criteria`.
5
7
  *
6
8
  * If the `Criteria` is not met, it will return `never'.
7
9
  *
@@ -1,7 +1,9 @@
1
1
  import type { TupleType } from '../tuple/tuple_type.js'
2
2
 
3
3
  /**
4
- * Returns the last type in the array or tuple that matches the `Criteria`.
4
+ * 🦴 *utilities*
5
+ *
6
+ * Gets the last type in the array or tuple that matches the `Criteria`.
5
7
  *
6
8
  * If the `Criteria` is not met, it will return `never'.
7
9
  *
@@ -1,6 +1,6 @@
1
1
  import type { IsEqual } from '../equal/equal.js'
2
2
  import type { CanAssign } from '../index.js'
3
- import type { Tail } from './tail.js'
3
+ import type { Tail } from '../tuple/tail.js'
4
4
  import type { UnionOfValues } from './union_of_values.js'
5
5
 
6
6
  /**
@@ -1,4 +1,7 @@
1
1
  /**
2
+ * 🦴 *utilities
3
+ * 💀 *deprecated* Will be available only as `ArrayPlus.Concat` in the next version
4
+ *
2
5
  * Concats two arrays or tuples.
3
6
  *
4
7
  * alias of: `[...A, ...B]`
@@ -0,0 +1,5 @@
1
+ export type Filter<A extends unknown[], Criteria> = A[0] extends Criteria
2
+ ? A
3
+ : Criteria extends A[0]
4
+ ? Array<Criteria>
5
+ : never[]
@@ -0,0 +1,13 @@
1
+ import type { CanAssign } from '../index.js'
2
+ import type { CreateTuple } from '../tuple/create_tuple.js'
3
+ import type { UnionOfValues } from './union_of_values.js'
4
+
5
+ export type PadStart<
6
+ A extends unknown[],
7
+ MaxLength extends number,
8
+ PadWith = unknown
9
+ > = MaxLength extends 0
10
+ ? A
11
+ : CanAssign<PadWith, UnionOfValues<A>> extends true
12
+ ? A
13
+ : PadStart<[...CreateTuple<MaxLength, PadWith>, ...A], MaxLength, PadWith>
@@ -1,10 +1,11 @@
1
1
  export type { At } from './array.at.js'
2
- export type { Concat } from './array.concat.js'
2
+ export type { Concat } from './array_plus.concat.js'
3
3
  export type { Entries } from './array.entries.js'
4
4
  export type { FindFirst as Find } from './array.find.js'
5
5
  export type { FindLast } from './array.find_last.js'
6
6
  export type { Reverse } from './array.reverse.js'
7
7
  export type { Some } from './array.some.js'
8
8
  export type { IndexAt, IsIndexOutOfBound } from './array_index.js'
9
+ export type { Filter } from './array_plus.filter.js'
9
10
  export type { SplitAt } from './array_plus.split_at.js'
10
- export type { PadStart } from './pad_start.js'
11
+ export type { PadStart } from './array_plus.pad_start.js'
@@ -2,12 +2,17 @@ import type { IsNever } from '../never/never_type.js'
2
2
  import type { StrictNumberType } from '../number/strict_number_type.js'
3
3
 
4
4
  /**
5
- * Check if the type `T` is an array and not a tuple.
5
+ * 🌪️ *filter*
6
6
  *
7
+ * Filter `T` to ensure it is an array, excluding tuple.
8
+ *
9
+ * @example
7
10
  * ```ts
8
11
  * type R = ArrayType<number[]> // number[]
9
12
  *
10
13
  * type R = ArrayType<[1]> // never
14
+ * type R = ArrayType<number[] | 1> // never
15
+ * type R = ArrayType<number[] & { a: 1 }> // never
11
16
  * ```
12
17
  */
13
18
  export type ArrayType<T, Then = T, Else = never> = IsNever<
@@ -17,32 +22,45 @@ export type ArrayType<T, Then = T, Else = never> = IsNever<
17
22
  >
18
23
 
19
24
  /**
20
- * Check if the type `T` is not an array.
25
+ * 🎭 *validate*
26
+ *
27
+ * Validate that `T` is an array, excluding tuple.
21
28
  *
29
+ * @example
22
30
  * ```ts
23
- * type R = NotArrayType<number[]> // never
31
+ * type R = IsArray<number[]> // true
24
32
  *
25
- * type R = NotArrayType<number> // number
26
- * type R = NotArrayType<[1]> // [1]
33
+ * type R = IsArray<number> // false
34
+ * type R = IsArray<[1]> // false
27
35
  * ```
28
36
  */
29
- export type NotArrayType<T, Then = T, Else = never> = ArrayType<T, Else, Then>
37
+ export type IsArray<T, Then = true, Else = false> = ArrayType<T, Then, Else>
30
38
 
31
39
  /**
32
- * Is `T` an array?
40
+ * 🌪️ *filter*
41
+ *
42
+ * Filter `T` to ensure it is not an array, excluding tuple.
33
43
  *
44
+ * i.e. *tuple* will pass through this filter.
45
+ *
46
+ * @example
34
47
  * ```ts
35
- * type R = IsArray<number[]> // true
48
+ * type R = NotArrayType<number[]> // never
36
49
  *
37
- * type R = IsArray<number> // false
38
- * type R = IsArray<[1]> // false
50
+ * type R = NotArrayType<number> // number
51
+ * type R = NotArrayType<[1]> // [1]
39
52
  * ```
40
53
  */
41
- export type IsArray<T, Then = true, Else = false> = ArrayType<T, Then, Else>
54
+ export type NotArrayType<T, Then = T, Else = never> = ArrayType<T, Else, Then>
42
55
 
43
56
  /**
44
- * Is `T` not an array?
57
+ * 🎭 *validate*
58
+ *
59
+ * Validate that `T` is not an array, excluding tuple.
60
+ *
61
+ * i.e. *tuple* will pass through this filter.
45
62
  *
63
+ * @example
46
64
  * ```ts
47
65
  * type R = IsNotArray<number[]> // false
48
66
  *
@@ -1,27 +1,32 @@
1
+ import type { Filter as FilterTuple } from '../tuple/tuple_plus.filter.js'
2
+ import type { Filter as FilterArray } from './array_plus.filter.js'
3
+
1
4
  /**
2
- * filter the array or tuple `A`, keeping entries satisfying `Criteria`.
5
+ * ⚗️ *transform*
6
+ *
7
+ * Filter the array or tuple `A`, keeping entries satisfying `Criteria`.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * type R = Filter<[1, 2, '3'], number> // [1, 2]
12
+ * type R = Filter<Array<string | undefined>, string> // string[]
13
+ * ```
3
14
  */
4
- export type Filter<A extends Array<any>, Criteria> = number extends A['length']
5
- ? // array
6
- A[0] extends Criteria
7
- ? A
8
- : Criteria extends A[0]
9
- ? Array<Criteria>
10
- : never[]
11
- : // tuple
12
- A['length'] extends 0
13
- ? never
14
- : A extends [infer Head, ...infer Tail]
15
- ? Tail['length'] extends 0
16
- ? Head extends Criteria
17
- ? [Head]
18
- : never[]
19
- : Head extends Criteria
20
- ? [Head, ...Filter<Tail, Criteria>]
21
- : Filter<Tail, Criteria>
22
- : never
15
+ export type Filter<A extends unknown[], Criteria> = number extends A['length']
16
+ ? FilterArray<A, Criteria>
17
+ : FilterTuple<A, Criteria>
23
18
 
24
19
  /**
25
- * keeps entries satisfying `Criteria` in array or tuple `A`.
20
+ * ⚗️ *transform*
21
+ * 👽 *alias*
22
+ *
23
+ * Keeps entries satisfying `Criteria` in array or tuple `A`.
24
+ *
25
+ * @alias of [Filter](./filter.ts)
26
+ * @example
27
+ * ```ts
28
+ * type R = KeepMatch<[1, 2, 3], number> // [1, 2]
29
+ * type R = KeepMatch<Array<string|undefined>, string> // string[]
30
+ * ```
26
31
  */
27
- export type KeepMatch<A extends Array<any>, Criteria> = Filter<A, Criteria>
32
+ export type KeepMatch<A extends unknown[], Criteria> = Filter<A, Criteria>
package/ts/array/head.ts CHANGED
@@ -1 +1,18 @@
1
- export type Head<T extends any[]> = T['length'] extends 0 ? never : T[0]
1
+ /**
2
+ * 🦴 *utilities*
3
+ *
4
+ * Gets the first entry in the tuple or the type of array.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * type R = Head<[1, 2, 3]> // 1
9
+ * type R = Head<string[]> // string
10
+ *
11
+ * type R = Head<[]> // never
12
+ * ```
13
+ */
14
+ export type Head<T extends any[], Cases extends {
15
+ empty_tuple?: unknown
16
+ } = {
17
+ empty_tuple: never
18
+ }> = T['length'] extends 0 ? Cases['empty_tuple'] : T[0]
@@ -1,10 +1,14 @@
1
1
  import type { KeyTypes } from '../object/index.js'
2
- import type { Tail } from './tail.js'
2
+ import type { Tail } from '../tuple/tail.js'
3
3
 
4
4
  /**
5
- * Gets the intersect of properties of the elements in `A`
5
+ * 🦴 *utilities*
6
+ *
7
+ * Gets the intersect of properties of the elements in `A`.
6
8
  */
7
- export type IntersectOfProps<A extends Record<any, unknown>[], P extends KeyTypes> = A['length'] extends 0
9
+ export type IntersectOfProps<A extends Record<any, unknown>[], P extends KeyTypes> = number extends A['length']
10
+ ? A[0][P]
11
+ : A['length'] extends 0
8
12
  ? never
9
13
  : A['length'] extends 1
10
14
  ? A[0][P]
package/ts/array/last.ts CHANGED
@@ -1,4 +1,18 @@
1
1
  /**
2
- * Get the last type of an array or tuple.
2
+ * 🦴 *utilities*
3
+ *
4
+ * Gets the last entry in the tuple or the type of array.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * type R = Last<[1, 2, 3]> // 3
9
+ * type R = Last<string[]> // string
10
+ *
11
+ * type R = Last<[]> // never
12
+ * ```
3
13
  */
4
- export type Last<T extends any[]> = T extends [...any[], infer R] ? R : T[0]
14
+ export type Last<T extends unknown[], Cases extends {
15
+ empty_tuple?: unknown
16
+ } = {
17
+ empty_tuple: never
18
+ }> = T['length'] extends 0 ? Cases['empty_tuple'] : T extends [...unknown[], infer R] ? R : T[0]
@@ -1,5 +1,10 @@
1
1
  import type { KeyTypes } from '../object/index.js'
2
2
 
3
+ /**
4
+ * 🦴 *utilities*
5
+ *
6
+ * return an array whose items are restricted to the provided literals.
7
+ */
3
8
  export function literalArray<T extends KeyTypes>(...entries: T[]): T[] {
4
9
  return entries
5
10
  }
@@ -1,11 +1,11 @@
1
- import type { CanAssign } from '../index.js'
2
- import type { CreateTuple } from '../tuple/create_tuple.js'
3
- import type { PadStart as TuplePadStart } from '../tuple/tuple_plus.pad_start.js'
4
- import type { UnionOfValues } from './union_of_values.js'
1
+ import type { PadStart as PadStartTuple } from '../tuple/tuple_plus.pad_start.js'
2
+ import type { PadStart as PadStartArray } from './array_plus.pad_start.js'
5
3
 
6
4
  /**
7
5
  * Pads the start of an array or tuple with `PadWith`.
8
6
  *
7
+ * ⚗️ *transform*
8
+ *
9
9
  * @example
10
10
  * ```ts
11
11
  * // Padding array
@@ -30,12 +30,8 @@ export type PadStart<
30
30
  MaxLength extends number,
31
31
  PadWith = unknown
32
32
  > = number extends A['length']
33
- ? MaxLength extends 0
34
- ? A
35
- : CanAssign<PadWith, UnionOfValues<A>> extends true
36
- ? A
37
- : PadStart<[...CreateTuple<MaxLength, PadWith>, ...A], MaxLength, PadWith>
38
- : TuplePadStart<A, MaxLength, PadWith>
33
+ ? PadStartArray<A, MaxLength, PadWith>
34
+ : PadStartTuple<A, MaxLength, PadWith>
39
35
 
40
36
  /**
41
37
  * @deprecated use PadStart instead
@@ -1,6 +1,11 @@
1
1
  # Array
2
2
 
3
- `Array<T>` or `T[]` is a type that represents an array of `T`.
3
+ In TypeScript, the type of an JavaScript array can be an [array](#array) or a [tuple](../tuple/readme.md).
4
+
5
+ `Array<T>` or `T[]` is a type that represents an array of `T`
6
+
7
+ There is no length limitation to an `Array`,
8
+ and each element has the same type `T`.
4
9
 
5
10
  ## Type Checking
6
11
 
@@ -9,6 +14,14 @@ The `ArrayType<T>` and friends are used to check if a type is exactly `Array<T>`
9
14
  They are strict type checks, meaning they match only the type `Array<T>`,
10
15
  and not [tuple], union, or intersection types.
11
16
 
17
+ ### [ArrayType](./array_type.ts#18)
18
+
19
+ `ArrayType<T, Then = T, Else = never>`
20
+
21
+ 🌪️ *filter*
22
+
23
+ Filter `T` to ensure it is an array, excluding tuple.
24
+
12
25
  ```ts
13
26
  import type { ArrayType } from 'type-plus'
14
27
 
@@ -19,25 +32,64 @@ type R = ArrayType<number[] | 1> // never
19
32
  type R = ArrayType<number[] & { a: 1 }> // never
20
33
  ```
21
34
 
22
- - [`ArrayType<T, Then = T, Else = never>`](array_type.ts#L15): check if `T` is an array and not a tuple.
23
- - [`IsArray<T, Then = true, Else = false`](array_type.ts#L33): is `T` an array and not a tuple.
24
- - [`NotArrayType<T, Then = T, Else = never>`](array_type.ts#L47): check if `T` is not an array (can be a tuple).
25
- - [`IsNotArray<T, Then = true, Else = false>`](array_type.ts#L61): is `T` not an array (can be a tuple).
35
+ ### [IsArray](./array_type.ts#37)
26
36
 
27
- ## ArrayPlus
37
+ `IsArray<T, Then = true, Else = false>`
28
38
 
29
- `ArrayPlus` contains all types and type utilities related to array.
30
- Whenever possible, these types and type utilities also work with *tuples*,
31
- as *tuples* is a subset of array.
39
+ 🎭 *validate*
32
40
 
33
- For *tuple* specific types and type utilities,
34
- please check [`TuplePlus`](../tuple/readme.md#TuplePlus).
41
+ Validate that `T` is an array, excluding tuple.
35
42
 
36
- ### [`ArrayPlus.At`](./array.at.ts#L18)
43
+ ```ts
44
+ import type { IsArray } from 'type-plus'
45
+
46
+ type R = IsArray<number[]> // true
47
+
48
+ type R = IsArray<number> // false
49
+ type R = IsArray<[1]> // false
50
+ ```
51
+
52
+ ### [NotArrayType](./array_type.ts#54)
53
+
54
+ `NotArrayType<T, Then = T, Else = never>`
55
+
56
+ 🌪️ *filter*
57
+
58
+ Filter `T` to ensure it is not an array, excluding tuple.
59
+
60
+ ```ts
61
+ import type { NotArrayType } from 'type-plus'
62
+
63
+ type R = NotArrayType<number[]> // never
64
+
65
+ type R = NotArrayType<number> // number
66
+ type R = NotArrayType<[1]> // [1]
67
+ ```
37
68
 
38
- > `ArrayPlus.At<A, N, Fail = never>`
69
+ ### [IsNotArrayType](./array_type.ts#71)
39
70
 
40
- Gets the type of the array or tuple `A` at index `N`.
71
+ `IsNotArrayType<T, Then = true, Else = false>`
72
+
73
+ 🎭 *validate*
74
+
75
+ Validate that `T` is not an array, excluding tuple.
76
+
77
+ ```ts
78
+ import type { IsNotArrayType } from 'type-plus'
79
+
80
+ type R = IsNotArrayType<number[]> // false
81
+
82
+ type R = IsNotArrayType<number> // true
83
+ type R = IsNotArrayType<[1]> // true
84
+ ```
85
+
86
+ ## [At](./array.at.ts)
87
+
88
+ `At<A, N, Fail = never>`
89
+
90
+ 🦴 *utilities*
91
+
92
+ Gets the type of the array or tuple at positive or negative index `N`.
41
93
 
42
94
  For array, it will return the union of the type of the array value and `undefined`,
43
95
  as there is no way to guarantee the array has value at `N`.
@@ -61,9 +113,12 @@ If the `N` is out of bound,
61
113
  or `N` is not a valid index,
62
114
  `ArrayPlus.At` will return the `Fail` case, which defaults to `never`.
63
115
 
64
- ### [`ArrayPlus.Concat`](./array.concat.ts#L12)
116
+ ## [`Concat`](./array.concat.ts#L12)
117
+
118
+ `Concat<A, B>`
65
119
 
66
- > `ArrayPlus.Concat<A, B>`
120
+ 🦴 *utilities*
121
+ 💀 *deprecated* Will be available only as `ArrayPlus.Concat` in the next version
67
122
 
68
123
  Concats two arrays or tuples.
69
124
 
@@ -72,6 +127,116 @@ It is added for completeness.
72
127
 
73
128
  You are encouraged to use `[...A, ...B]` directly.
74
129
 
130
+ ## [`FindFirst`](./array.find.ts)
131
+
132
+ ## [`FineLast`](./array.find_last.ts)
133
+
134
+ ## [`Some`](./array.some.ts)
135
+
136
+ ## [`Filter`](./filter.ts#l17)
137
+
138
+ `Filter<A, Criteria>`
139
+
140
+ ⚗️ *transform*
141
+
142
+ Filter the array or tuple `A`, keeping entries satisfying `Criteria`.
143
+
144
+ ```ts
145
+ import type { Filter } from 'type-plus'
146
+
147
+ type R = Filter<[1, 2, '3'], number> // [1, 2]
148
+ type R = Filter<Array<string | undefined>, string> // string[]
149
+ ```
150
+
151
+ ## [`KeepMatch`](./filter.ts)
152
+
153
+ `KeepMatch<A, Criteria>`
154
+
155
+ ⚗️ *transform*
156
+ 👽 *alias* of [`Filter`](#filter)
157
+
158
+ Keeps entries satisfying `Criteria` in array or tuple `A`.
159
+
160
+ ```ts
161
+ import type { KeepMatch } from 'type-plus'
162
+
163
+ type R = KeepMatch<[1, 2, '3'], number> // [1, 2]
164
+ type R = KeepMatch<Array<string | undefined>, string> // string[]
165
+ ```
166
+
167
+ ## [`Head`](./head.ts#l14)
168
+
169
+ `Head<T, Cases = { empty_tuple }>`
170
+
171
+ 🦴 *utilities*
172
+
173
+ Gets the first entry in the tuple or the type of array.
174
+
175
+ ```ts
176
+ import type { Head } from 'type-plus'
177
+
178
+ type R = Head<[1, 2, 3]> // 1
179
+ type R = Head<string[]> // string
180
+
181
+ type R = Head<[]> // never
182
+ ```
183
+
184
+ ## [`IntersectOfProps`](./intersect_of_props.ts)
185
+
186
+ ## [`MapToProp`](./intersect_of_props.ts)
187
+
188
+ ## [`Last`](./last.ts)
189
+
190
+ `Last<T, Cases = { empty_tuple }>`
191
+
192
+ 🦴 *utilities*
193
+
194
+ Gets the last entry in the tuple or the type of array.
195
+
196
+ ```ts
197
+ import type { Last } from 'type-plus'
198
+
199
+ type R = Last<[1, 2, 3]> // 3
200
+ type R = Last<string[]> // string
201
+
202
+ type R = Last<[]> // never
203
+ ```
204
+
205
+ ## [`literalArray`](./literal_array.ts)
206
+
207
+ ## [`PadStart`](./pad_start.ts)
208
+
209
+ ## [`reduceWhile`](./reduce_while.ts)
210
+
211
+ ## [`Reverse`](./reverse.ts)
212
+
213
+ ## [`PropUnion`](./union_of_props.ts)
214
+
215
+ ## [`UnionOfProps`](./union_of_props.ts)
216
+
217
+ ## [`UnionOfValues`](./union_of_values.ts)
218
+
219
+ ## ArrayPlus
220
+
221
+ `ArrayPlus` contains all types and type utilities related to array.
222
+ Whenever possible, these types and type utilities also work with *tuples*,
223
+ as *tuples* is a subset of array.
224
+
225
+ For *tuple* specific types and type utilities,
226
+ please check [`TuplePlus`](../tuple/readme.md#TuplePlus).
227
+
228
+ ### [`ArrayPlus.At`](./array.at.ts#L18)
229
+
230
+ `ArrayPlus.At<A, N, Fail = never>`
231
+
232
+ Alias of [At](#at).
233
+
234
+ ### [`ArrayPlus.Concat`](./array.concat.ts#L12)
235
+
236
+ `ArrayPlus.Concat<A, B>`
237
+
238
+ Alias of [Concat](#concat).
239
+
75
240
  ### [`ArrayPlus.Entries`](./array.entries.ts#L14)
76
241
 
77
242
  > `ArrayPlus.Entries<A>`
@@ -1,3 +1,9 @@
1
+ /**
2
+ * 🦴 *utilities*
3
+ *
4
+ * `reduce()` with predicate for early termination.
5
+ * A simple version of the same function in the `ramda` package.
6
+ */
1
7
  export function reduceWhile<T, R>(
2
8
  predicate: (acc: R, currentValue: T) => boolean,
3
9
  callbackfn: (previousValue: R, currentValue: T, currentIndex: number, array: T[]) => R,