functype 0.8.81 → 0.8.83
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/dist/Either-CncND8cm.d.ts +709 -0
- package/dist/Tuple-ZYh96cGE.d.ts +41 -0
- package/dist/Typeable-BBXrKPTY.d.ts +14 -0
- package/dist/branded/index.d.ts +48 -0
- package/dist/branded/index.mjs +2 -0
- package/dist/branded/index.mjs.map +1 -0
- package/dist/chunk-B6FR572T.mjs +2 -0
- package/dist/chunk-B6FR572T.mjs.map +1 -0
- package/dist/chunk-JMCOLAJY.mjs +42 -0
- package/dist/chunk-JMCOLAJY.mjs.map +1 -0
- package/dist/chunk-TQJDL6YW.mjs +2 -0
- package/dist/chunk-TQJDL6YW.mjs.map +1 -0
- package/dist/either/index.d.ts +2 -0
- package/dist/either/index.mjs +2 -0
- package/dist/either/index.mjs.map +1 -0
- package/dist/fpromise/index.d.ts +373 -0
- package/dist/fpromise/index.mjs +2 -0
- package/dist/fpromise/index.mjs.map +1 -0
- package/dist/index.d.ts +1354 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/list/index.d.ts +2 -0
- package/dist/list/index.mjs +2 -0
- package/dist/list/index.mjs.map +1 -0
- package/dist/map/index.d.ts +58 -0
- package/dist/map/index.mjs +2 -0
- package/dist/map/index.mjs.map +1 -0
- package/dist/option/index.d.ts +2 -0
- package/dist/option/index.mjs +2 -0
- package/dist/option/index.mjs.map +1 -0
- package/dist/set/index.d.ts +2 -0
- package/dist/set/index.mjs +2 -0
- package/dist/set/index.mjs.map +1 -0
- package/dist/try/index.d.ts +68 -0
- package/dist/try/index.mjs +2 -0
- package/dist/try/index.mjs.map +1 -0
- package/dist/tuple/index.d.ts +2 -0
- package/dist/tuple/index.mjs +2 -0
- package/dist/tuple/index.mjs.map +1 -0
- package/package.json +1 -2
|
@@ -0,0 +1,709 @@
|
|
|
1
|
+
import { T as Type, a as Typeable } from './Typeable-BBXrKPTY.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a Some variant of Option containing a value.
|
|
5
|
+
* @param value - The value to wrap in Some
|
|
6
|
+
* @returns A new Some instance containing the value
|
|
7
|
+
* @typeParam T - The type of the value
|
|
8
|
+
*/
|
|
9
|
+
declare const Some: <T extends Type>(value: T) => Option<T>;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a None variant of Option representing absence of a value.
|
|
12
|
+
* @returns A new None instance
|
|
13
|
+
* @typeParam T - The type that would be contained if this was a Some
|
|
14
|
+
*/
|
|
15
|
+
declare const None: <T extends Type>() => Option<T>;
|
|
16
|
+
/**
|
|
17
|
+
* Safely wraps a value that might be null or undefined in an Option.
|
|
18
|
+
* Creates Some if the value is defined, None otherwise.
|
|
19
|
+
* @param value - The value to wrap (might be null/undefined)
|
|
20
|
+
* @returns Some(value) if value is defined, None otherwise
|
|
21
|
+
* @typeParam T - The type of the value
|
|
22
|
+
*/
|
|
23
|
+
declare const OptionConstructor: <T extends Type>(value: T | null | undefined) => Option<T>;
|
|
24
|
+
/**
|
|
25
|
+
* Option type module
|
|
26
|
+
* @module Option
|
|
27
|
+
* @category Core
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* The Option type represents a value that may or may not exist.
|
|
31
|
+
* It's used to handle potentially null or undefined values in a type-safe way.
|
|
32
|
+
* @typeParam T - The type of the value contained in the Option
|
|
33
|
+
*/
|
|
34
|
+
interface Option<T extends Type> extends Functype<T, "Some" | "None"> {
|
|
35
|
+
/** The contained value (undefined for None) */
|
|
36
|
+
readonly value: T | undefined;
|
|
37
|
+
/** Whether this Option contains no value */
|
|
38
|
+
isEmpty: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Extracts the value if present
|
|
41
|
+
* @throws Error if the Option is None
|
|
42
|
+
* @returns The contained value
|
|
43
|
+
*/
|
|
44
|
+
get(): T;
|
|
45
|
+
/**
|
|
46
|
+
* Returns the contained value or a default value if None
|
|
47
|
+
* @param defaultValue - The value to return if this Option is None
|
|
48
|
+
* @returns The contained value or defaultValue
|
|
49
|
+
*/
|
|
50
|
+
getOrElse(defaultValue: T): T;
|
|
51
|
+
/**
|
|
52
|
+
* Returns the contained value or throws a specified error if None
|
|
53
|
+
* @param error - The error to throw if this Option is None
|
|
54
|
+
* @returns The contained value
|
|
55
|
+
* @throws The specified error if the Option is None
|
|
56
|
+
*/
|
|
57
|
+
getOrThrow(error: Error): T;
|
|
58
|
+
/**
|
|
59
|
+
* Returns this Option if it contains a value, otherwise returns the alternative
|
|
60
|
+
* @param alternative - The alternative Option to return if this is None
|
|
61
|
+
* @returns This Option or the alternative
|
|
62
|
+
*/
|
|
63
|
+
orElse(alternative: Option<T>): Option<T>;
|
|
64
|
+
/**
|
|
65
|
+
* Returns the contained value or null if None
|
|
66
|
+
* @returns The contained value or null
|
|
67
|
+
*/
|
|
68
|
+
orNull(): T | null;
|
|
69
|
+
/**
|
|
70
|
+
* Returns the contained value or undefined if None
|
|
71
|
+
* @returns The contained value or undefined
|
|
72
|
+
*/
|
|
73
|
+
orUndefined(): T | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Maps the value inside the Option using the provided function
|
|
76
|
+
* @param f - The mapping function
|
|
77
|
+
* @returns A new Option containing the mapped value, or None if this Option is None
|
|
78
|
+
*/
|
|
79
|
+
map<U extends Type>(f: (value: T) => U): Option<U>;
|
|
80
|
+
/**
|
|
81
|
+
* Applies a wrapped function to a wrapped value (Applicative pattern)
|
|
82
|
+
* @param ff - An Option containing a function from T to U
|
|
83
|
+
* @returns A new Option containing the result of applying the function
|
|
84
|
+
*/
|
|
85
|
+
ap<U extends Type>(ff: Option<(value: T) => U>): Option<U>;
|
|
86
|
+
/**
|
|
87
|
+
* Returns this Option if it contains a value that satisfies the predicate, otherwise returns None
|
|
88
|
+
* @param predicate - The predicate function to test the value
|
|
89
|
+
* @returns This Option or None
|
|
90
|
+
*/
|
|
91
|
+
filter(predicate: (value: T) => boolean): Option<T>;
|
|
92
|
+
/**
|
|
93
|
+
* Maps the value using a function that returns an Option
|
|
94
|
+
* @param f - The mapping function returning an Option
|
|
95
|
+
* @returns The result of applying f to the contained value, or None if this Option is None
|
|
96
|
+
*/
|
|
97
|
+
flatMap<U extends Type>(f: (value: T) => Option<U>): Option<U>;
|
|
98
|
+
/**
|
|
99
|
+
* Maps the value using an async function that returns an Option
|
|
100
|
+
* @param f - The async mapping function returning an Option
|
|
101
|
+
* @returns Promise of the result of applying f to the contained value, or None if this Option is None
|
|
102
|
+
*/
|
|
103
|
+
flatMapAsync<U extends Type>(f: (value: T) => Promise<Option<U>>): Promise<Option<U>>;
|
|
104
|
+
/**
|
|
105
|
+
* Applies a binary operator to a start value and the contained value
|
|
106
|
+
* @param f - The binary operator
|
|
107
|
+
* @returns The result of the reduction
|
|
108
|
+
*/
|
|
109
|
+
reduce<U>(f: (acc: U, value: T) => U): U;
|
|
110
|
+
/**
|
|
111
|
+
* Applies a binary operator to the contained value and a start value
|
|
112
|
+
* @param f - The binary operator
|
|
113
|
+
* @returns The result of the reduction
|
|
114
|
+
*/
|
|
115
|
+
reduceRight<U>(f: (acc: U, value: T) => U): U;
|
|
116
|
+
/**
|
|
117
|
+
* Pattern matches over the Option, applying onNone if None and onSome if Some
|
|
118
|
+
* @param onNone - Function to apply if the Option is None
|
|
119
|
+
* @param onSome - Function to apply if the Option has a value
|
|
120
|
+
* @returns The result of applying the appropriate function
|
|
121
|
+
*/
|
|
122
|
+
fold<U>(onNone: () => U, onSome: (value: T) => U): U;
|
|
123
|
+
/**
|
|
124
|
+
* Left-associative fold using the provided zero value and operation
|
|
125
|
+
* @param z - Zero/identity value
|
|
126
|
+
* @returns A function that takes an operation to apply
|
|
127
|
+
*/
|
|
128
|
+
foldLeft<B>(z: B): (op: (b: B, a: T) => B) => B;
|
|
129
|
+
/**
|
|
130
|
+
* Right-associative fold using the provided zero value and operation
|
|
131
|
+
* @param z - Zero/identity value
|
|
132
|
+
* @returns A function that takes an operation to apply
|
|
133
|
+
*/
|
|
134
|
+
foldRight<B>(z: B): (op: (a: T, b: B) => B) => B;
|
|
135
|
+
/**
|
|
136
|
+
* Converts this Option to a List
|
|
137
|
+
* @returns A List containing the value if Some, or empty List if None
|
|
138
|
+
*/
|
|
139
|
+
toList(): List<T>;
|
|
140
|
+
/**
|
|
141
|
+
* Checks if this Option contains the specified value
|
|
142
|
+
* @param value - The value to check for
|
|
143
|
+
* @returns true if this Option contains the value, false otherwise
|
|
144
|
+
*/
|
|
145
|
+
contains(value: T): boolean;
|
|
146
|
+
/** The number of elements in this Option (0 or 1) */
|
|
147
|
+
size: number;
|
|
148
|
+
/**
|
|
149
|
+
* Converts this Option to an Either
|
|
150
|
+
* @param left - The value to use for Left if this Option is None
|
|
151
|
+
* @returns Either.Right with the contained value if Some, or Either.Left with left if None
|
|
152
|
+
*/
|
|
153
|
+
toEither<E>(left: E): Either<E, T>;
|
|
154
|
+
/**
|
|
155
|
+
* Returns a string representation of this Option
|
|
156
|
+
* @returns A string representation
|
|
157
|
+
*/
|
|
158
|
+
toString(): string;
|
|
159
|
+
/**
|
|
160
|
+
* Returns a simple object representation of this Option
|
|
161
|
+
* @returns An object with _tag and value properties
|
|
162
|
+
*/
|
|
163
|
+
toValue(): {
|
|
164
|
+
_tag: "Some" | "None";
|
|
165
|
+
value: T;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Pattern matches over the Option, applying a handler function based on the variant
|
|
169
|
+
* @param patterns - Object with handler functions for Some and None variants
|
|
170
|
+
* @returns The result of applying the matching handler function
|
|
171
|
+
*/
|
|
172
|
+
match<R>(patterns: {
|
|
173
|
+
Some: (value: T) => R;
|
|
174
|
+
None: () => R;
|
|
175
|
+
}): R;
|
|
176
|
+
}
|
|
177
|
+
declare const Option: (<T extends Type>(value: T | null | undefined) => Option<T>) & {
|
|
178
|
+
/**
|
|
179
|
+
* Creates an Option from any value. Alias for Option function.
|
|
180
|
+
* @param value - The value to wrap
|
|
181
|
+
* @returns Some(value) if value is defined, None otherwise
|
|
182
|
+
* @typeParam T - The type of the value
|
|
183
|
+
*/
|
|
184
|
+
from: <T>(value: T) => Option<T>;
|
|
185
|
+
/**
|
|
186
|
+
* Returns a None instance. Alias for None function.
|
|
187
|
+
* @returns A None instance
|
|
188
|
+
* @typeParam T - The type that would be contained if this was a Some
|
|
189
|
+
*/
|
|
190
|
+
none: <T>() => Option<T>;
|
|
191
|
+
/**
|
|
192
|
+
* Creates an Option from JSON string
|
|
193
|
+
* @param json - The JSON string
|
|
194
|
+
* @returns Option instance
|
|
195
|
+
*/
|
|
196
|
+
fromJSON: <T>(json: string) => Option<T>;
|
|
197
|
+
/**
|
|
198
|
+
* Creates an Option from YAML string
|
|
199
|
+
* @param yaml - The YAML string
|
|
200
|
+
* @returns Option instance
|
|
201
|
+
*/
|
|
202
|
+
fromYAML: <T>(yaml: string) => Option<T>;
|
|
203
|
+
/**
|
|
204
|
+
* Creates an Option from binary string
|
|
205
|
+
* @param binary - The binary string
|
|
206
|
+
* @returns Option instance
|
|
207
|
+
*/
|
|
208
|
+
fromBinary: <T>(binary: string) => Option<T>;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
interface List<A> extends FunctypeCollection<A, "List"> {
|
|
212
|
+
readonly length: number;
|
|
213
|
+
readonly [Symbol.iterator]: () => Iterator<A>;
|
|
214
|
+
map: <B>(f: (a: A) => B) => List<B>;
|
|
215
|
+
ap: <B>(ff: List<(value: A) => B>) => List<B>;
|
|
216
|
+
flatMap: <B>(f: (a: A) => Iterable<B>) => List<B>;
|
|
217
|
+
flatMapAsync: <B>(f: (a: A) => PromiseLike<Iterable<B>>) => PromiseLike<List<B>>;
|
|
218
|
+
filter<S extends A>(predicate: (a: A) => a is S): List<S>;
|
|
219
|
+
filter(predicate: (a: A) => unknown): List<A>;
|
|
220
|
+
filterNot: (p: (a: A) => boolean) => List<A>;
|
|
221
|
+
filterType: <T extends Typeable<string, unknown>>(tag: string) => List<T & A>;
|
|
222
|
+
remove: (value: A) => List<A>;
|
|
223
|
+
removeAt: (index: number) => List<A>;
|
|
224
|
+
add: (item: A) => List<A>;
|
|
225
|
+
get: (index: number) => Option<A>;
|
|
226
|
+
concat: (other: List<A>) => List<A>;
|
|
227
|
+
/**
|
|
228
|
+
* Pattern matches over the List, applying a handler function based on whether it's empty
|
|
229
|
+
* @param patterns - Object with handler functions for Empty and NonEmpty variants
|
|
230
|
+
* @returns The result of applying the matching handler function
|
|
231
|
+
*/
|
|
232
|
+
match<R>(patterns: {
|
|
233
|
+
Empty: () => R;
|
|
234
|
+
NonEmpty: (values: A[]) => R;
|
|
235
|
+
}): R;
|
|
236
|
+
}
|
|
237
|
+
declare const List: (<A>(values?: Iterable<A>) => List<A>) & {
|
|
238
|
+
/**
|
|
239
|
+
* Creates a List from JSON string
|
|
240
|
+
* @param json - The JSON string
|
|
241
|
+
* @returns List instance
|
|
242
|
+
*/
|
|
243
|
+
fromJSON: <A>(json: string) => List<A>;
|
|
244
|
+
/**
|
|
245
|
+
* Creates a List from YAML string
|
|
246
|
+
* @param yaml - The YAML string
|
|
247
|
+
* @returns List instance
|
|
248
|
+
*/
|
|
249
|
+
fromYAML: <A>(yaml: string) => List<A>;
|
|
250
|
+
/**
|
|
251
|
+
* Creates a List from binary string
|
|
252
|
+
* @param binary - The binary string
|
|
253
|
+
* @returns List instance
|
|
254
|
+
*/
|
|
255
|
+
fromBinary: <A>(binary: string) => List<A>;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
interface Set<A> extends FunctypeCollection<A, "Set">, Collection<A> {
|
|
259
|
+
add: (value: A) => Set<A>;
|
|
260
|
+
remove: (value: A) => Set<A>;
|
|
261
|
+
contains: (value: A) => boolean;
|
|
262
|
+
has: (value: A) => boolean;
|
|
263
|
+
map: <B>(f: (a: A) => B) => Set<B>;
|
|
264
|
+
flatMap: <B>(f: (a: A) => Iterable<B>) => Set<B>;
|
|
265
|
+
filter: (p: (a: A) => boolean) => Set<A>;
|
|
266
|
+
filterNot: (p: (a: A) => boolean) => Set<A>;
|
|
267
|
+
fold: <U extends Type>(onEmpty: () => U, onValue: (value: A) => U) => U;
|
|
268
|
+
toList: () => List<A>;
|
|
269
|
+
toSet: () => Set<A>;
|
|
270
|
+
toArray: <B = A>() => readonly B[];
|
|
271
|
+
toString: () => string;
|
|
272
|
+
}
|
|
273
|
+
declare const Set: (<A>(iterable?: Iterable<A>) => Set<A>) & {
|
|
274
|
+
/**
|
|
275
|
+
* Creates a Set from JSON string
|
|
276
|
+
* @param json - The JSON string
|
|
277
|
+
* @returns Set instance
|
|
278
|
+
*/
|
|
279
|
+
fromJSON: <A>(json: string) => Set<A>;
|
|
280
|
+
/**
|
|
281
|
+
* Creates a Set from YAML string
|
|
282
|
+
* @param yaml - The YAML string
|
|
283
|
+
* @returns Set instance
|
|
284
|
+
*/
|
|
285
|
+
fromYAML: <A>(yaml: string) => Set<A>;
|
|
286
|
+
/**
|
|
287
|
+
* Creates a Set from binary string
|
|
288
|
+
* @param binary - The binary string
|
|
289
|
+
* @returns Set instance
|
|
290
|
+
*/
|
|
291
|
+
fromBinary: <A>(binary: string) => Set<A>;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Represents a collection with conversion capabilities
|
|
296
|
+
* @interface
|
|
297
|
+
* @module Collections
|
|
298
|
+
* @category Core
|
|
299
|
+
*/
|
|
300
|
+
interface Collection<A> {
|
|
301
|
+
toList(): List<A>;
|
|
302
|
+
toSet(): Set<A>;
|
|
303
|
+
toString(): string;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Extractable type class for data structures that can extract their values
|
|
308
|
+
* with various fallback strategies.
|
|
309
|
+
*
|
|
310
|
+
* This interface is implemented by Option, Either, and other types that
|
|
311
|
+
* wrap values and need safe extraction methods.
|
|
312
|
+
*/
|
|
313
|
+
interface Extractable<T extends Type> {
|
|
314
|
+
/**
|
|
315
|
+
* Extracts the value unsafely
|
|
316
|
+
* @throws Error if the container is empty
|
|
317
|
+
* @returns The contained value
|
|
318
|
+
*/
|
|
319
|
+
get(): T;
|
|
320
|
+
/**
|
|
321
|
+
* Returns the contained value or a default value
|
|
322
|
+
* @param defaultValue - The value to return if extraction fails
|
|
323
|
+
* @returns The contained value or defaultValue
|
|
324
|
+
*/
|
|
325
|
+
getOrElse(defaultValue: T): T;
|
|
326
|
+
/**
|
|
327
|
+
* Returns the contained value or throws an error
|
|
328
|
+
* @param error - Optional error to throw (implementations may have defaults)
|
|
329
|
+
* @returns The contained value
|
|
330
|
+
* @throws The specified error if extraction fails
|
|
331
|
+
*/
|
|
332
|
+
getOrThrow(error?: Error): T;
|
|
333
|
+
/**
|
|
334
|
+
* Returns this container if it has a value, otherwise returns the alternative
|
|
335
|
+
* @param alternative - The alternative container
|
|
336
|
+
* @returns This container or the alternative
|
|
337
|
+
*/
|
|
338
|
+
orElse(alternative: Extractable<T>): Extractable<T>;
|
|
339
|
+
/**
|
|
340
|
+
* Returns the contained value or null
|
|
341
|
+
* @returns The contained value or null
|
|
342
|
+
*/
|
|
343
|
+
orNull(): T | null;
|
|
344
|
+
/**
|
|
345
|
+
* Returns the contained value or undefined
|
|
346
|
+
* @returns The contained value or undefined
|
|
347
|
+
*/
|
|
348
|
+
orUndefined(): T | undefined;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Foldable type class represents data structures that can be folded to a summary value.
|
|
353
|
+
*
|
|
354
|
+
* @typeParam A - The type of elements in the data structure
|
|
355
|
+
*/
|
|
356
|
+
interface Foldable<A> {
|
|
357
|
+
/**
|
|
358
|
+
* Pattern matches over the structure, applying specific handlers for each variant
|
|
359
|
+
* @param onEmpty - Function to apply if the structure is empty or has no value
|
|
360
|
+
* @param onValue - Function to apply if the structure has a value
|
|
361
|
+
* @returns The result of applying the appropriate function
|
|
362
|
+
*/
|
|
363
|
+
fold<B>(onEmpty: () => B, onValue: (value: A) => B): B;
|
|
364
|
+
/**
|
|
365
|
+
* Left-associative fold using the provided zero value and operation
|
|
366
|
+
* @param z - Zero/identity value
|
|
367
|
+
* @returns A function that takes an operation to apply
|
|
368
|
+
*/
|
|
369
|
+
foldLeft<B>(z: B): (op: (b: B, a: A) => B) => B;
|
|
370
|
+
/**
|
|
371
|
+
* Right-associative fold using the provided zero value and operation
|
|
372
|
+
* @param z - Zero/identity value
|
|
373
|
+
* @returns A function that takes an operation to apply
|
|
374
|
+
*/
|
|
375
|
+
foldRight<B>(z: B): (op: (a: A, b: B) => B) => B;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Pattern matching interface for functional data types.
|
|
380
|
+
*
|
|
381
|
+
* @typeParam A - The type of elements in the data structure
|
|
382
|
+
* @typeParam Tags - The type of tags used for pattern matching
|
|
383
|
+
*/
|
|
384
|
+
interface Matchable<A, Tags extends string = string> {
|
|
385
|
+
/**
|
|
386
|
+
* Pattern matches against this data structure, applying handlers for each variant based on tag.
|
|
387
|
+
* Similar to fold but with stronger type safety for tag-based variants.
|
|
388
|
+
*
|
|
389
|
+
* The return type is inferred from the pattern handlers when not explicitly specified.
|
|
390
|
+
*
|
|
391
|
+
* @param patterns - An object containing handler functions for each variant
|
|
392
|
+
* @returns The result of applying the matching handler function
|
|
393
|
+
*/
|
|
394
|
+
match<R>(patterns: Record<Tags, (value: A) => R>): R;
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Utility functions for working with Matchable data structures
|
|
398
|
+
*/
|
|
399
|
+
declare const MatchableUtils: {
|
|
400
|
+
/**
|
|
401
|
+
* Helper function to create a default case for pattern matching
|
|
402
|
+
*
|
|
403
|
+
* @param handler - The default handler function to apply
|
|
404
|
+
* @returns A function that always applies the default handler
|
|
405
|
+
*/
|
|
406
|
+
default: <A, R>(handler: (value: A) => R) => (value: A) => R;
|
|
407
|
+
/**
|
|
408
|
+
* Helper function to create a match pattern that guards based on a predicate
|
|
409
|
+
*
|
|
410
|
+
* @param predicate - The predicate function for guarding
|
|
411
|
+
* @param handler - The handler function to apply if the predicate passes
|
|
412
|
+
* @returns A function that applies the handler only if the predicate passes
|
|
413
|
+
*/
|
|
414
|
+
when: <A, R>(predicate: (value: A) => boolean, handler: (value: A) => R) => (value: A) => R | undefined;
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Pipe interface for functional data structures
|
|
419
|
+
* @typeParam T - The type of value to pipe
|
|
420
|
+
*/
|
|
421
|
+
interface Pipe<T extends Type> {
|
|
422
|
+
/**
|
|
423
|
+
* Pipes the value through the provided function
|
|
424
|
+
* @param f - The function to apply to the value
|
|
425
|
+
* @returns The result of applying the function to the value
|
|
426
|
+
* @typeParam U - The return type of the function
|
|
427
|
+
*/
|
|
428
|
+
pipe<U extends Type>(f: (value: T) => U): U;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Methods for different serialization formats
|
|
433
|
+
*/
|
|
434
|
+
interface SerializationMethods<T> {
|
|
435
|
+
toJSON(): string;
|
|
436
|
+
toYAML(): string;
|
|
437
|
+
toBinary(): string;
|
|
438
|
+
}
|
|
439
|
+
interface Serializable<T> {
|
|
440
|
+
serialize(): SerializationMethods<T>;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Universal operations that work on any container (single-value or collection).
|
|
445
|
+
* These operations make sense for Option, Either, Try, List, Set, etc.
|
|
446
|
+
*
|
|
447
|
+
* @typeParam A - The type of value(s) in the container
|
|
448
|
+
*/
|
|
449
|
+
interface ContainerOps<A extends Type> {
|
|
450
|
+
/**
|
|
451
|
+
* Counts elements that satisfy the predicate.
|
|
452
|
+
* For single-value containers: returns 0 or 1
|
|
453
|
+
* For collections: returns the count of matching elements
|
|
454
|
+
*/
|
|
455
|
+
count(p: (x: A) => boolean): number;
|
|
456
|
+
/**
|
|
457
|
+
* Finds the first element that satisfies the predicate.
|
|
458
|
+
* For single-value containers: returns Some(value) if predicate matches, None otherwise
|
|
459
|
+
* For collections: returns the first matching element wrapped in Option
|
|
460
|
+
*/
|
|
461
|
+
find(p: (a: A) => boolean): Option<A>;
|
|
462
|
+
/**
|
|
463
|
+
* Tests whether any element satisfies the predicate.
|
|
464
|
+
* For single-value containers: tests the single value
|
|
465
|
+
* For collections: returns true if any element matches
|
|
466
|
+
*/
|
|
467
|
+
exists(p: (a: A) => boolean): boolean;
|
|
468
|
+
/**
|
|
469
|
+
* Applies an effect function to each element.
|
|
470
|
+
* For single-value containers: applies to the value if present
|
|
471
|
+
* For collections: applies to each element
|
|
472
|
+
*/
|
|
473
|
+
forEach(f: (a: A) => void): void;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Operations specific to collections (List, Set, etc.).
|
|
477
|
+
* These operations don't make sense for single-value containers.
|
|
478
|
+
*
|
|
479
|
+
* @typeParam A - The element type
|
|
480
|
+
* @typeParam Self - The collection type itself for proper return types
|
|
481
|
+
*/
|
|
482
|
+
interface CollectionOps<A extends Type, Self> {
|
|
483
|
+
/**
|
|
484
|
+
* Drops the first n elements from the collection.
|
|
485
|
+
*/
|
|
486
|
+
drop(n: number): Self;
|
|
487
|
+
/**
|
|
488
|
+
* Drops the last n elements from the collection.
|
|
489
|
+
*/
|
|
490
|
+
dropRight(n: number): Self;
|
|
491
|
+
/**
|
|
492
|
+
* Drops elements from the start while the predicate is true.
|
|
493
|
+
*/
|
|
494
|
+
dropWhile(p: (a: A) => boolean): Self;
|
|
495
|
+
/**
|
|
496
|
+
* Flattens a collection of collections into a single collection.
|
|
497
|
+
*/
|
|
498
|
+
flatten<B>(): Self;
|
|
499
|
+
/**
|
|
500
|
+
* Gets the first element of the collection.
|
|
501
|
+
*/
|
|
502
|
+
get head(): A | undefined;
|
|
503
|
+
/**
|
|
504
|
+
* Gets the first element wrapped in Option.
|
|
505
|
+
*/
|
|
506
|
+
get headOption(): Option<A>;
|
|
507
|
+
/**
|
|
508
|
+
* Converts the collection to an array.
|
|
509
|
+
*/
|
|
510
|
+
toArray<B = A>(): readonly B[];
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Functor type class - supports mapping over wrapped values
|
|
515
|
+
*
|
|
516
|
+
* Laws:
|
|
517
|
+
* - Identity: fa.map(x => x) ≡ fa
|
|
518
|
+
* - Composition: fa.map(f).map(g) ≡ fa.map(x => g(f(x)))
|
|
519
|
+
*/
|
|
520
|
+
interface Functor<A extends Type> {
|
|
521
|
+
map<B extends Type>(f: (value: A) => B): Functor<B>;
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Applicative functor - supports applying wrapped functions to wrapped values
|
|
525
|
+
*
|
|
526
|
+
* Laws:
|
|
527
|
+
* - Identity: pure(x => x).ap(v) ≡ v
|
|
528
|
+
* - Composition: pure(compose).ap(u).ap(v).ap(w) ≡ u.ap(v.ap(w))
|
|
529
|
+
* - Homomorphism: pure(f).ap(pure(x)) ≡ pure(f(x))
|
|
530
|
+
* - Interchange: u.ap(pure(y)) ≡ pure(f => f(y)).ap(u)
|
|
531
|
+
*/
|
|
532
|
+
interface Applicative<A extends Type> extends Functor<A> {
|
|
533
|
+
ap<B extends Type>(ff: Applicative<(value: A) => B>): Applicative<B>;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Monad type class - supports flat mapping (chaining) operations
|
|
537
|
+
*
|
|
538
|
+
* Laws:
|
|
539
|
+
* - Left identity: pure(a).flatMap(f) ≡ f(a)
|
|
540
|
+
* - Right identity: m.flatMap(pure) ≡ m
|
|
541
|
+
* - Associativity: m.flatMap(f).flatMap(g) ≡ m.flatMap(x => f(x).flatMap(g))
|
|
542
|
+
*/
|
|
543
|
+
interface Monad<A extends Type> extends Applicative<A> {
|
|
544
|
+
flatMap<B extends Type>(f: (value: A) => Monad<B>): Monad<B>;
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Async monad - supports asynchronous monadic operations
|
|
548
|
+
* Extends Monad so it has map, ap, and flatMap in addition to flatMapAsync
|
|
549
|
+
*/
|
|
550
|
+
interface AsyncMonad<A extends Type> extends Monad<A> {
|
|
551
|
+
flatMapAsync<B extends Type>(f: (value: A) => PromiseLike<AsyncMonad<B>>): PromiseLike<AsyncMonad<B>>;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Traversable typeclass for data structures that can be traversed through
|
|
556
|
+
*/
|
|
557
|
+
interface Traversable<A extends Type> extends AsyncMonad<A> {
|
|
558
|
+
get size(): number;
|
|
559
|
+
get isEmpty(): boolean;
|
|
560
|
+
contains(value: A): boolean;
|
|
561
|
+
reduce(f: (b: A, a: A) => A): A;
|
|
562
|
+
reduceRight(f: (b: A, a: A) => A): A;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Base interface for all functype data structures.
|
|
567
|
+
* This provides a standard contract with core functional programming traits.
|
|
568
|
+
*
|
|
569
|
+
* @typeParam A - The type of value contained in the functor
|
|
570
|
+
* @typeParam Tag - The type tag for pattern matching (e.g., "Some" | "None" for Option)
|
|
571
|
+
*
|
|
572
|
+
* @example
|
|
573
|
+
* ```typescript
|
|
574
|
+
* // Implementing FunctypeBase for a custom data structure
|
|
575
|
+
* class MyContainer<T> implements FunctypeBase<T, "Empty" | "Full"> {
|
|
576
|
+
* // Implementation of all required methods...
|
|
577
|
+
* }
|
|
578
|
+
* ```
|
|
579
|
+
*/
|
|
580
|
+
interface FunctypeBase<A, Tag extends string = string> extends AsyncMonad<A>, Traversable<A>, Serializable<A>, Foldable<A>, Typeable<Tag>, ContainerOps<A> {
|
|
581
|
+
readonly _tag: Tag;
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Interface for single-value containers like Option, Either, Try.
|
|
585
|
+
* Extends FunctypeBase with extraction methods and Pipe.
|
|
586
|
+
*
|
|
587
|
+
* @typeParam A - The type of value contained
|
|
588
|
+
* @typeParam Tag - The type tag for pattern matching
|
|
589
|
+
*/
|
|
590
|
+
interface Functype<A, Tag extends string = string> extends FunctypeBase<A, Tag>, Extractable<A>, Pipe<A>, Matchable<A, Tag> {
|
|
591
|
+
toValue(): {
|
|
592
|
+
_tag: Tag;
|
|
593
|
+
value: A;
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* A version of Functype for collection types that need iteration support.
|
|
598
|
+
* Extends FunctypeBase with Iterable protocol but without Extractable.
|
|
599
|
+
*
|
|
600
|
+
* @typeParam A - The element type of the collection
|
|
601
|
+
* @typeParam Tag - The type tag for pattern matching
|
|
602
|
+
*/
|
|
603
|
+
interface FunctypeCollection<A, Tag extends string = string> extends Omit<FunctypeBase<A, Tag>, "flatMapAsync" | "flatMap">, Iterable<A>, Pipe<A[]>, Collection<A>, CollectionOps<A, FunctypeCollection<A, Tag>> {
|
|
604
|
+
toValue(): {
|
|
605
|
+
_tag: Tag;
|
|
606
|
+
value: A[];
|
|
607
|
+
};
|
|
608
|
+
flatMap<B extends Type>(f: (value: A) => Iterable<B>): FunctypeCollection<B, Tag>;
|
|
609
|
+
flatMapAsync<B extends Type>(f: (value: A) => PromiseLike<Iterable<B>>): PromiseLike<FunctypeCollection<B, Tag>>;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
type TestEither<L extends Type, R extends Type> = Either<L, R> & AsyncMonad<R>;
|
|
613
|
+
declare const Right: <L extends Type, R extends Type>(value: R) => Either<L, R>;
|
|
614
|
+
declare const Left: <L extends Type, R extends Type>(value: L) => Either<L, R>;
|
|
615
|
+
declare const isRight: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
|
|
616
|
+
value: R;
|
|
617
|
+
};
|
|
618
|
+
declare const isLeft: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
|
|
619
|
+
value: L;
|
|
620
|
+
};
|
|
621
|
+
declare const tryCatch: <L extends Type, R extends Type>(f: () => R, onError: (error: unknown) => L) => Either<L, R>;
|
|
622
|
+
declare const TypeCheckRight: <L extends Type, R extends Type>(value: R) => TestEither<L, R>;
|
|
623
|
+
declare const TypeCheckLeft: <L extends Type, R extends Type>(value: L) => TestEither<L, R>;
|
|
624
|
+
declare const tryCatchAsync: <L extends Type, R extends Type>(f: () => Promise<R>, onError: (error: unknown) => L) => Promise<Either<L, R>>;
|
|
625
|
+
/**
|
|
626
|
+
* Either type module
|
|
627
|
+
* @module Either
|
|
628
|
+
* @category Core
|
|
629
|
+
*/
|
|
630
|
+
interface Either<L extends Type, R extends Type> extends FunctypeBase<R, "Left" | "Right">, PromiseLike<R> {
|
|
631
|
+
readonly _tag: "Left" | "Right";
|
|
632
|
+
value: L | R;
|
|
633
|
+
isLeft: () => boolean;
|
|
634
|
+
isRight: () => boolean;
|
|
635
|
+
get: () => R;
|
|
636
|
+
getOrElse: (defaultValue: R) => R;
|
|
637
|
+
getOrThrow: (error?: Error) => R;
|
|
638
|
+
orElse(alternative: Either<L, R>): Either<L, R>;
|
|
639
|
+
orNull: () => R | null;
|
|
640
|
+
orUndefined: () => R | undefined;
|
|
641
|
+
map: <U extends Type>(f: (value: R) => U) => Either<L, U>;
|
|
642
|
+
ap: <U extends Type>(ff: Either<L, (value: R) => U>) => Either<L, U>;
|
|
643
|
+
merge: <L1 extends Type, R1 extends Type>(other: Either<L1, R1>) => Either<L | L1, [R, R1]>;
|
|
644
|
+
mapAsync: <U extends Type>(f: (value: R) => Promise<U>) => Promise<Either<L, U>>;
|
|
645
|
+
flatMap: <U extends Type>(f: (value: R) => Either<L, U>) => Either<L, U>;
|
|
646
|
+
flatMapAsync: <U extends Type>(f: (value: R) => Promise<Either<L, U>>) => Promise<Either<L, U>>;
|
|
647
|
+
toOption: () => Option<R>;
|
|
648
|
+
toList: () => List<R>;
|
|
649
|
+
toString: () => string;
|
|
650
|
+
[Symbol.iterator]: () => Iterator<R>;
|
|
651
|
+
yield: () => Generator<R, void, unknown>;
|
|
652
|
+
traverse: <U extends Type>(f: (value: R) => Either<L, U>) => Either<L, U[]>;
|
|
653
|
+
lazyMap: <U extends Type>(f: (value: R) => U) => Generator<Either<L, U>, void, unknown>;
|
|
654
|
+
tap: (f: (value: R) => void) => Either<L, R>;
|
|
655
|
+
tapLeft: (f: (value: L) => void) => Either<L, R>;
|
|
656
|
+
mapLeft: <L2 extends Type>(f: (value: L) => L2) => Either<L2, R>;
|
|
657
|
+
bimap: <L2 extends Type, R2 extends Type>(fl: (value: L) => L2, fr: (value: R) => R2) => Either<L2, R2>;
|
|
658
|
+
fold: <T extends Type>(onLeft: (value: L) => T, onRight: (value: R) => T) => T;
|
|
659
|
+
swap: () => Either<R, L>;
|
|
660
|
+
/**
|
|
661
|
+
* Pipes the value through the provided function based on whether this is a Left or Right
|
|
662
|
+
* @param onLeft - The function to apply if this is a Left
|
|
663
|
+
* @param onRight - The function to apply if this is a Right
|
|
664
|
+
* @returns The result of applying the appropriate function
|
|
665
|
+
*/
|
|
666
|
+
pipeEither<U extends Type>(onLeft: (value: L) => U, onRight: (value: R) => U): U;
|
|
667
|
+
/**
|
|
668
|
+
* Pipes the Either value through the provided function
|
|
669
|
+
* @param f - The function to apply to the value (Left or Right)
|
|
670
|
+
* @returns The result of applying the function to the value
|
|
671
|
+
*/
|
|
672
|
+
pipe<U extends Type>(f: (value: L | R) => U): U;
|
|
673
|
+
/**
|
|
674
|
+
* Pattern matches over the Either, applying a handler function based on the variant
|
|
675
|
+
* @param patterns - Object with handler functions for Left and Right variants
|
|
676
|
+
* @returns The result of applying the matching handler function
|
|
677
|
+
*/
|
|
678
|
+
match<T>(patterns: {
|
|
679
|
+
Left: (value: L) => T;
|
|
680
|
+
Right: (value: R) => T;
|
|
681
|
+
}): T;
|
|
682
|
+
/**
|
|
683
|
+
* Returns the value and tag for inspection
|
|
684
|
+
*/
|
|
685
|
+
toValue(): {
|
|
686
|
+
_tag: "Left" | "Right";
|
|
687
|
+
value: L | R;
|
|
688
|
+
};
|
|
689
|
+
/**
|
|
690
|
+
* Custom JSON serialization that excludes getter properties
|
|
691
|
+
*/
|
|
692
|
+
toJSON(): {
|
|
693
|
+
_tag: "Left" | "Right";
|
|
694
|
+
value: L | R;
|
|
695
|
+
};
|
|
696
|
+
}
|
|
697
|
+
declare const Either: {
|
|
698
|
+
sequence: <L extends Type, R extends Type>(eithers: Either<L, R>[]) => Either<L, R[]>;
|
|
699
|
+
traverse: <L extends Type, R extends Type, U extends Type>(arr: R[], f: (value: R) => Either<L, U>) => Either<L, U[]>;
|
|
700
|
+
fromNullable: <L extends Type, R extends Type>(value: R | null | undefined, leftValue: L) => Either<L, R>;
|
|
701
|
+
fromPredicate: <L extends Type, R extends Type>(value: R, predicate: (value: R) => boolean, leftValue: L) => Either<L, R>;
|
|
702
|
+
ap: <L extends Type, R extends Type, U extends Type>(eitherF: Either<L, (value: R) => U>, eitherV: Either<L, R>) => Either<L, U>;
|
|
703
|
+
fromPromise: <L, R>(promise: Promise<R>, onRejected: (reason: unknown) => L) => Promise<Either<L, R>>;
|
|
704
|
+
fromJSON: <L extends Type, R extends Type>(json: string) => Either<L, R>;
|
|
705
|
+
fromYAML: <L extends Type, R extends Type>(yaml: string) => Either<L, R>;
|
|
706
|
+
fromBinary: <L extends Type, R extends Type>(binary: string) => Either<L, R>;
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
export { type AsyncMonad as A, type Collection as C, Either as E, type Foldable as F, List as L, type Matchable as M, None as N, Option as O, type Pipe as P, Right as R, type Serializable as S, type Traversable as T, type FunctypeBase as a, type Extractable as b, type TestEither as c, Left as d, isLeft as e, TypeCheckRight as f, TypeCheckLeft as g, tryCatchAsync as h, isRight as i, type Functype as j, type FunctypeCollection as k, MatchableUtils as l, Some as m, OptionConstructor as n, type SerializationMethods as o, Set as p, type CollectionOps as q, type ContainerOps as r, type Applicative as s, tryCatch as t, type Functor as u, type Monad as v };
|