@planet-matrix/mobius-model 0.3.0 → 0.5.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.
- package/CHANGELOG.md +15 -0
- package/README.md +30 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +22 -4
- package/package.json +3 -3
- package/scripts/build.ts +4 -4
- package/src/basic/README.md +144 -0
- package/src/basic/array.ts +872 -0
- package/src/basic/bigint.ts +114 -0
- package/src/basic/boolean.ts +180 -0
- package/src/basic/enhance.ts +10 -0
- package/src/basic/error.ts +51 -0
- package/src/basic/function.ts +453 -0
- package/src/basic/helper.ts +276 -0
- package/src/basic/index.ts +17 -0
- package/src/basic/is.ts +320 -0
- package/src/basic/number.ts +178 -0
- package/src/basic/object.ts +140 -0
- package/src/basic/promise.ts +464 -0
- package/src/basic/regexp.ts +7 -0
- package/src/basic/stream.ts +140 -0
- package/src/basic/string.ts +308 -0
- package/src/basic/symbol.ts +164 -0
- package/src/basic/temporal.ts +224 -0
- package/src/encoding/README.md +105 -0
- package/src/encoding/base64.ts +98 -0
- package/src/encoding/index.ts +1 -0
- package/src/index.ts +4 -0
- package/src/random/README.md +109 -0
- package/src/random/index.ts +1 -0
- package/src/random/uuid.ts +103 -0
- package/src/type/README.md +330 -0
- package/src/type/array.ts +5 -0
- package/src/type/boolean.ts +471 -0
- package/src/type/class.ts +419 -0
- package/src/type/function.ts +1519 -0
- package/src/type/helper.ts +135 -0
- package/src/type/index.ts +14 -0
- package/src/type/intersection.ts +93 -0
- package/src/type/is.ts +247 -0
- package/src/type/iteration.ts +233 -0
- package/src/type/number.ts +732 -0
- package/src/type/object.ts +788 -0
- package/src/type/path.ts +73 -0
- package/src/type/string.ts +1004 -0
- package/src/type/tuple.ts +2424 -0
- package/src/type/union.ts +108 -0
- package/tests/unit/basic/array.spec.ts +290 -0
- package/tests/unit/basic/bigint.spec.ts +50 -0
- package/tests/unit/basic/boolean.spec.ts +74 -0
- package/tests/unit/basic/error.spec.ts +32 -0
- package/tests/unit/basic/function.spec.ts +175 -0
- package/tests/unit/basic/helper.spec.ts +118 -0
- package/tests/unit/basic/number.spec.ts +74 -0
- package/tests/unit/basic/object.spec.ts +46 -0
- package/tests/unit/basic/promise.spec.ts +232 -0
- package/tests/unit/basic/regexp.spec.ts +11 -0
- package/tests/unit/basic/stream.spec.ts +120 -0
- package/tests/unit/basic/string.spec.ts +74 -0
- package/tests/unit/basic/symbol.spec.ts +72 -0
- package/tests/unit/basic/temporal.spec.ts +78 -0
- package/tests/unit/encoding/base64.spec.ts +40 -0
- package/tests/unit/random/uuid.spec.ts +37 -0
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/reactor/index.d.ts +0 -3
- package/dist/reactor/index.d.ts.map +0 -1
- package/dist/reactor/reactor-core/flags.d.ts +0 -99
- package/dist/reactor/reactor-core/flags.d.ts.map +0 -1
- package/dist/reactor/reactor-core/index.d.ts +0 -4
- package/dist/reactor/reactor-core/index.d.ts.map +0 -1
- package/dist/reactor/reactor-core/primitive.d.ts +0 -276
- package/dist/reactor/reactor-core/primitive.d.ts.map +0 -1
- package/dist/reactor/reactor-core/reactive-system.d.ts +0 -241
- package/dist/reactor/reactor-core/reactive-system.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/branch.d.ts +0 -19
- package/dist/reactor/reactor-operators/branch.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/convert.d.ts +0 -30
- package/dist/reactor/reactor-operators/convert.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/create.d.ts +0 -26
- package/dist/reactor/reactor-operators/create.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/filter.d.ts +0 -269
- package/dist/reactor/reactor-operators/filter.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/index.d.ts +0 -8
- package/dist/reactor/reactor-operators/index.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/join.d.ts +0 -48
- package/dist/reactor/reactor-operators/join.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/map.d.ts +0 -165
- package/dist/reactor/reactor-operators/map.d.ts.map +0 -1
- package/dist/reactor/reactor-operators/utility.d.ts +0 -48
- package/dist/reactor/reactor-operators/utility.d.ts.map +0 -1
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Helpers
|
|
3
|
+
// ============================================================================
|
|
4
|
+
|
|
5
|
+
type InternalBuildArray<N extends number, Acc extends readonly unknown[] = []> =
|
|
6
|
+
Acc['length'] extends N ? Acc : InternalBuildArray<N, [...Acc, unknown]>
|
|
7
|
+
|
|
8
|
+
type InternalIsGreaterThan<A extends number, B extends number> =
|
|
9
|
+
A extends B ? false : (
|
|
10
|
+
InternalBuildArray<A> extends [...InternalBuildArray<B>, ...infer _Rest]
|
|
11
|
+
? (_Rest extends [unknown, ...unknown[]] ? true : false)
|
|
12
|
+
: false
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
// ============================================================================
|
|
16
|
+
// Primitives
|
|
17
|
+
// ============================================================================
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Conditional type selector based on a boolean value (ternary operator).
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```
|
|
24
|
+
* // Expect: 'yes'
|
|
25
|
+
* type Example1 = BooleanIf<true, 'yes', 'no'>
|
|
26
|
+
* // Expect: 'no'
|
|
27
|
+
* type Example2 = BooleanIf<false, 'yes', 'no'>
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export type BooleanIf<Condition extends boolean, Then, Else> = Condition extends true ? Then : Else
|
|
31
|
+
|
|
32
|
+
// ============================================================================
|
|
33
|
+
// Comparison
|
|
34
|
+
// ============================================================================
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Check if two boolean values are equal.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```
|
|
41
|
+
* // Expect: true
|
|
42
|
+
* type Example1 = BooleanIsEqual<true, true>
|
|
43
|
+
* // Expect: true
|
|
44
|
+
* type Example2 = BooleanIsEqual<false, false>
|
|
45
|
+
* // Expect: false
|
|
46
|
+
* type Example3 = BooleanIsEqual<true, false>
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export type BooleanIsEqual<T extends boolean, U extends boolean> =
|
|
50
|
+
T extends U ? (U extends T ? true : false) : false
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Logical implication (A → B), equivalent to ¬A ∨ B.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```
|
|
57
|
+
* // Expect: true
|
|
58
|
+
* type Example1 = BooleanImplies<true, true>
|
|
59
|
+
* // Expect: false
|
|
60
|
+
* type Example2 = BooleanImplies<true, false>
|
|
61
|
+
* // Expect: true
|
|
62
|
+
* type Example3 = BooleanImplies<false, true>
|
|
63
|
+
* // Expect: true
|
|
64
|
+
* type Example4 = BooleanImplies<false, false>
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export type BooleanImplies<T extends boolean, U extends boolean> =
|
|
68
|
+
T extends true ? (U extends true ? true : false) : true
|
|
69
|
+
|
|
70
|
+
// ============================================================================
|
|
71
|
+
// Query
|
|
72
|
+
// ============================================================================
|
|
73
|
+
|
|
74
|
+
type InternalBooleanCountTrue<T extends readonly boolean[], Acc extends readonly unknown[] = []> =
|
|
75
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
76
|
+
? (First extends true ? InternalBooleanCountTrue<Rest, [...Acc, unknown]> : InternalBooleanCountTrue<Rest, Acc>)
|
|
77
|
+
: Acc['length']
|
|
78
|
+
/**
|
|
79
|
+
* Count the number of true values in a boolean tuple.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```
|
|
83
|
+
* // Expect: 2
|
|
84
|
+
* type Example1 = BooleanCountTrue<[true, false, true]>
|
|
85
|
+
* // Expect: 0
|
|
86
|
+
* type Example2 = BooleanCountTrue<[false, false]>
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export type BooleanCountTrue<T extends readonly boolean[]> = InternalBooleanCountTrue<T>
|
|
90
|
+
|
|
91
|
+
type InternalBooleanCountFalse<T extends readonly boolean[], Acc extends readonly unknown[] = []> =
|
|
92
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
93
|
+
? (First extends false ? InternalBooleanCountFalse<Rest, [...Acc, unknown]> : InternalBooleanCountFalse<Rest, Acc>)
|
|
94
|
+
: Acc['length']
|
|
95
|
+
/**
|
|
96
|
+
* Count the number of false values in a boolean tuple.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```
|
|
100
|
+
* // Expect: 2
|
|
101
|
+
* type Example1 = BooleanCountFalse<[true, false, false]>
|
|
102
|
+
* // Expect: 0
|
|
103
|
+
* type Example2 = BooleanCountFalse<[true, true]>
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export type BooleanCountFalse<T extends readonly boolean[]> = InternalBooleanCountFalse<T>
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Count the number of true or false values in a boolean tuple.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```
|
|
113
|
+
* // Expect: 2
|
|
114
|
+
* type Example1 = BooleanCount<[true, false, true], true>
|
|
115
|
+
* // Expect: 1
|
|
116
|
+
* type Example2 = BooleanCount<[true, false, true], false>
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export type BooleanCount<T extends readonly boolean[], Value extends boolean> =
|
|
120
|
+
Value extends true ? BooleanCountTrue<T> : BooleanCountFalse<T>
|
|
121
|
+
|
|
122
|
+
type InternalBooleanFindIndex<T extends readonly boolean[], Value extends boolean, Index extends readonly unknown[] = []> =
|
|
123
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
124
|
+
? (First extends Value ? Index['length'] : InternalBooleanFindIndex<Rest, Value, [...Index, unknown]>)
|
|
125
|
+
: -1
|
|
126
|
+
/**
|
|
127
|
+
* Find the index of the first occurrence of a boolean value in a tuple.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```
|
|
131
|
+
* // Expect: 1
|
|
132
|
+
* type Example1 = BooleanFindIndex<[false, true, false], true>
|
|
133
|
+
* // Expect: 0
|
|
134
|
+
* type Example2 = BooleanFindIndex<[false, true, false], false>
|
|
135
|
+
* // Expect: -1
|
|
136
|
+
* type Example3 = BooleanFindIndex<[false, false], true>
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export type BooleanFindIndex<T extends readonly boolean[], Value extends boolean> =
|
|
140
|
+
InternalBooleanFindIndex<T, Value>
|
|
141
|
+
|
|
142
|
+
// ============================================================================
|
|
143
|
+
// Manipulation
|
|
144
|
+
// ============================================================================
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Logical NOT operation.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```
|
|
151
|
+
* // Expect: true
|
|
152
|
+
* type Example1 = BooleanNot<false>
|
|
153
|
+
* // Expect: false
|
|
154
|
+
* type Example2 = BooleanNot<true>
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
export type BooleanNot<T extends boolean> = T extends true ? false : true
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Logical AND operation.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```
|
|
164
|
+
* // Expect: true
|
|
165
|
+
* type Example1 = BooleanAnd<true, true>
|
|
166
|
+
* // Expect: false
|
|
167
|
+
* type Example2 = BooleanAnd<true, false>
|
|
168
|
+
* // Expect: false
|
|
169
|
+
* type Example3 = BooleanAnd<false, true>
|
|
170
|
+
* // Expect: false
|
|
171
|
+
* type Example4 = BooleanAnd<false, false>
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
export type BooleanAnd<T extends boolean, U extends boolean> = T extends true ? (U extends true ? true : false) : false
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Logical NAND operation (NOT AND).
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```
|
|
181
|
+
* // Expect: false
|
|
182
|
+
* type Example1 = BooleanNand<true, true>
|
|
183
|
+
* // Expect: true
|
|
184
|
+
* type Example2 = BooleanNand<true, false>
|
|
185
|
+
* // Expect: true
|
|
186
|
+
* type Example3 = BooleanNand<false, true>
|
|
187
|
+
* // Expect: true
|
|
188
|
+
* type Example4 = BooleanNand<false, false>
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
export type BooleanNand<T extends boolean, U extends boolean> = T extends true ? (U extends true ? false : true) : true
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Logical OR operation.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```
|
|
198
|
+
* // Expect: true
|
|
199
|
+
* type Example1 = BooleanOr<true, false>
|
|
200
|
+
* // Expect: false
|
|
201
|
+
* type Example2 = BooleanOr<false, false>
|
|
202
|
+
* // Expect: true
|
|
203
|
+
* type Example3 = BooleanOr<false, true>
|
|
204
|
+
* // Expect: true
|
|
205
|
+
* type Example4 = BooleanOr<true, true>
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
export type BooleanOr<T extends boolean, U extends boolean> = T extends true ? true : (U extends true ? true : false)
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Logical NOR operation (NOT OR).
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```
|
|
215
|
+
* // Expect: false
|
|
216
|
+
* type Example1 = BooleanNor<true, false>
|
|
217
|
+
* // Expect: true
|
|
218
|
+
* type Example2 = BooleanNor<false, false>
|
|
219
|
+
* // Expect: false
|
|
220
|
+
* type Example3 = BooleanNor<false, true>
|
|
221
|
+
* // Expect: false
|
|
222
|
+
* type Example4 = BooleanNor<true, true>
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
export type BooleanNor<T extends boolean, U extends boolean> = T extends true ? false : (U extends true ? false : true)
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Logical XOR operation (exclusive OR).
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```
|
|
232
|
+
* // Expect: true
|
|
233
|
+
* type Example1 = BooleanXor<true, false>
|
|
234
|
+
* // Expect: false
|
|
235
|
+
* type Example2 = BooleanXor<false, false>
|
|
236
|
+
* // Expect: true
|
|
237
|
+
* type Example3 = BooleanXor<false, true>
|
|
238
|
+
* // Expect: false
|
|
239
|
+
* type Example4 = BooleanXor<true, true>
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
export type BooleanXor<T extends boolean, U extends boolean> =
|
|
243
|
+
T extends true ? (U extends true ? false : true) : (U extends true ? true : false)
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Logical XNOR operation (exclusive NOR).
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```
|
|
250
|
+
* // Expect: false
|
|
251
|
+
* type Example1 = BooleanXnor<true, false>
|
|
252
|
+
* // Expect: true
|
|
253
|
+
* type Example2 = BooleanXnor<false, false>
|
|
254
|
+
* // Expect: false
|
|
255
|
+
* type Example3 = BooleanXnor<false, true>
|
|
256
|
+
* // Expect: true
|
|
257
|
+
* type Example4 = BooleanXnor<true, true>
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
export type BooleanXnor<T extends boolean, U extends boolean> =
|
|
261
|
+
T extends true ? (U extends true ? true : false) : (U extends true ? false : true)
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Check if all boolean values in a tuple are true.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```
|
|
268
|
+
* // Expect: true
|
|
269
|
+
* type Example1 = BooleanAll<[true, true, true]>
|
|
270
|
+
* // Expect: false
|
|
271
|
+
* type Example2 = BooleanAll<[true, false, true]>
|
|
272
|
+
* // Expect: true
|
|
273
|
+
* type Example3 = BooleanAll<[]>
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
export type BooleanAll<T extends readonly boolean[]> =
|
|
277
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
278
|
+
? (First extends true ? BooleanAll<Rest> : false)
|
|
279
|
+
: true
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Alias for BooleanAll.
|
|
283
|
+
*/
|
|
284
|
+
export type BooleanEvery<T extends readonly boolean[]> = BooleanAll<T>
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Check if any boolean value in a tuple is true.
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```
|
|
291
|
+
* // Expect: true
|
|
292
|
+
* type Example1 = BooleanAny<[false, true, false]>
|
|
293
|
+
* // Expect: false
|
|
294
|
+
* type Example2 = BooleanAny<[false, false, false]>
|
|
295
|
+
* // Expect: false
|
|
296
|
+
* type Example3 = BooleanAny<[]>
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
export type BooleanAny<T extends readonly boolean[]> =
|
|
300
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
301
|
+
? (First extends true ? true : BooleanAny<Rest>)
|
|
302
|
+
: false
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Alias for BooleanAny.
|
|
306
|
+
*/
|
|
307
|
+
export type BooleanSome<T extends readonly boolean[]> = BooleanAny<T>
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Check if all boolean values in a tuple are false.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```
|
|
314
|
+
* // Expect: true
|
|
315
|
+
* type Example1 = BooleanNone<[false, false, false]>
|
|
316
|
+
* // Expect: false
|
|
317
|
+
* type Example2 = BooleanNone<[false, true, false]>
|
|
318
|
+
* // Expect: true
|
|
319
|
+
* type Example3 = BooleanNone<[]>
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
export type BooleanNone<T extends readonly boolean[]> =
|
|
323
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
324
|
+
? (First extends false ? BooleanNone<Rest> : false)
|
|
325
|
+
: true
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Filter a boolean tuple to keep only specified value (true or false).
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* ```
|
|
332
|
+
* // Expect: [true, true]
|
|
333
|
+
* type Example1 = BooleanFilter<[true, false, true, false], true>
|
|
334
|
+
* // Expect: [false, false]
|
|
335
|
+
* type Example2 = BooleanFilter<[true, false, true, false], false>
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
export type BooleanFilter<T extends readonly boolean[], Value extends boolean> =
|
|
339
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
340
|
+
? (First extends Value ? [First, ...BooleanFilter<Rest, Value>] : BooleanFilter<Rest, Value>)
|
|
341
|
+
: []
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Map a boolean tuple by applying NOT operation to each element.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```
|
|
348
|
+
* // Expect: [false, true, false]
|
|
349
|
+
* type Example1 = BooleanMapNot<[true, false, true]>
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
352
|
+
export type BooleanMapNot<T extends readonly boolean[]> =
|
|
353
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
354
|
+
? [BooleanNot<First>, ...BooleanMapNot<Rest>]
|
|
355
|
+
: []
|
|
356
|
+
|
|
357
|
+
type InternalBooleanReverse<T extends readonly boolean[], Acc extends readonly boolean[] = []> =
|
|
358
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
359
|
+
? InternalBooleanReverse<Rest, [First, ...Acc]>
|
|
360
|
+
: Acc
|
|
361
|
+
/**
|
|
362
|
+
* Reverse a boolean tuple.
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```
|
|
366
|
+
* // Expect: [false, true, true]
|
|
367
|
+
* type Example1 = BooleanReverse<[true, true, false]>
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
export type BooleanReverse<T extends readonly boolean[]> = InternalBooleanReverse<T>
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Partition a boolean tuple into two groups: true values and false values.
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```
|
|
377
|
+
* // Expect: [[true, true], [false, false]]
|
|
378
|
+
* type Example1 = BooleanPartition<[true, false, true, false]>
|
|
379
|
+
* ```
|
|
380
|
+
*/
|
|
381
|
+
export type BooleanPartition<T extends readonly boolean[]> = [BooleanFilter<T, true>, BooleanFilter<T, false>]
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Majority function - returns true if more than half of the values are true.
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```
|
|
388
|
+
* // Expect: true
|
|
389
|
+
* type Example1 = BooleanMajority<[true, true, false]>
|
|
390
|
+
* // Expect: false
|
|
391
|
+
* type Example2 = BooleanMajority<[true, false, false]>
|
|
392
|
+
* // Expect: false
|
|
393
|
+
* type Example3 = BooleanMajority<[true, false]>
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
396
|
+
export type BooleanMajority<T extends readonly boolean[]> =
|
|
397
|
+
BooleanCountTrue<T> extends infer TrueCount extends number
|
|
398
|
+
? (T['length'] extends infer Total extends number
|
|
399
|
+
? (TrueCount extends 0 ? false : (Total extends 0 ? false :
|
|
400
|
+
// Compare TrueCount * 2 > Total by building arrays
|
|
401
|
+
[...InternalBuildArray<TrueCount>, ...InternalBuildArray<TrueCount>]['length'] extends infer Double extends number
|
|
402
|
+
? (InternalIsGreaterThan<Double, Total> extends true ? true : false)
|
|
403
|
+
: false))
|
|
404
|
+
: false)
|
|
405
|
+
: false
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Parity function - returns true if an odd number of values are true.
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```
|
|
412
|
+
* // Expect: true
|
|
413
|
+
* type Example1 = BooleanParity<[true, false, false]>
|
|
414
|
+
* // Expect: false
|
|
415
|
+
* type Example2 = BooleanParity<[true, true, false]>
|
|
416
|
+
* // Expect: true
|
|
417
|
+
* type Example3 = BooleanParity<[true]>
|
|
418
|
+
* ```
|
|
419
|
+
*/
|
|
420
|
+
export type BooleanParity<T extends readonly boolean[]> =
|
|
421
|
+
T extends [infer First extends boolean, ...infer Rest extends boolean[]]
|
|
422
|
+
? (First extends true ? BooleanNot<BooleanParity<Rest>> : BooleanParity<Rest>)
|
|
423
|
+
: false
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Threshold function - returns true if at least N values are true.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```
|
|
430
|
+
* // Expect: true
|
|
431
|
+
* type Example1 = BooleanThreshold<[true, true, false], 2>
|
|
432
|
+
* // Expect: false
|
|
433
|
+
* type Example2 = BooleanThreshold<[true, false, false], 2>
|
|
434
|
+
* // Expect: true
|
|
435
|
+
* type Example3 = BooleanThreshold<[true, true, true], 2>
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
export type BooleanThreshold<T extends readonly boolean[], N extends number> =
|
|
439
|
+
BooleanCountTrue<T> extends infer TrueCount extends number
|
|
440
|
+
? (InternalBuildArray<TrueCount> extends [...InternalBuildArray<N>, ...unknown[]] ? true : false)
|
|
441
|
+
: false
|
|
442
|
+
|
|
443
|
+
// ============================================================================
|
|
444
|
+
// Boolean Conversion
|
|
445
|
+
// ============================================================================
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Convert a boolean value to a number literal (true → 1, false → 0).
|
|
449
|
+
*
|
|
450
|
+
* @example
|
|
451
|
+
* ```
|
|
452
|
+
* // Expect: 1
|
|
453
|
+
* type Example1 = BooleanToNumber<true>
|
|
454
|
+
* // Expect: 0
|
|
455
|
+
* type Example2 = BooleanToNumber<false>
|
|
456
|
+
* ```
|
|
457
|
+
*/
|
|
458
|
+
export type BooleanToNumber<T extends boolean> = T extends true ? 1 : 0
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Convert a boolean value to a string literal.
|
|
462
|
+
*
|
|
463
|
+
* @example
|
|
464
|
+
* ```
|
|
465
|
+
* // Expect: 'true'
|
|
466
|
+
* type Example1 = BooleanToString<true>
|
|
467
|
+
* // Expect: 'false'
|
|
468
|
+
* type Example2 = BooleanToString<false>
|
|
469
|
+
* ```
|
|
470
|
+
*/
|
|
471
|
+
export type BooleanToString<T extends boolean> = T extends true ? 'true' : 'false'
|