functype 0.18.0 → 0.20.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 (67) hide show
  1. package/README.md +13 -11
  2. package/README.processed.md +2 -11
  3. package/dist/Brand-B-0nKo7I.d.ts +55 -0
  4. package/dist/Brand-Cfr5zy8F.js +2 -0
  5. package/dist/Brand-Cfr5zy8F.js.map +1 -0
  6. package/dist/Tuple-CKxIyX7l.d.ts +178 -0
  7. package/dist/Tuple-CgX4p79w.js +2 -0
  8. package/dist/Tuple-CgX4p79w.js.map +1 -0
  9. package/dist/branded/index.d.ts +2 -54
  10. package/dist/branded/index.js +1 -0
  11. package/dist/cli/index.d.ts +1 -0
  12. package/dist/cli/index.js +660 -0
  13. package/dist/cli/index.js.map +1 -0
  14. package/dist/do/index.d.ts +4 -240
  15. package/dist/do/index.js +1 -0
  16. package/dist/either/index.d.ts +4 -2
  17. package/dist/either/index.js +1 -0
  18. package/dist/fpromise/index.d.ts +4 -373
  19. package/dist/fpromise/index.js +1 -0
  20. package/dist/index-Bnjlo4cT.d.ts +3575 -0
  21. package/dist/index.d.ts +4 -1934
  22. package/dist/index.js +1 -0
  23. package/dist/list/index.d.ts +4 -2
  24. package/dist/list/index.js +1 -0
  25. package/dist/map/index.d.ts +4 -58
  26. package/dist/map/index.js +1 -0
  27. package/dist/option/index.d.ts +4 -2
  28. package/dist/option/index.js +1 -0
  29. package/dist/set/index.d.ts +4 -2
  30. package/dist/set/index.js +1 -0
  31. package/dist/src-BOxI0-73.js +20 -0
  32. package/dist/src-BOxI0-73.js.map +1 -0
  33. package/dist/try/index.d.ts +4 -2
  34. package/dist/try/index.js +1 -0
  35. package/dist/tuple/index.d.ts +2 -75
  36. package/dist/tuple/index.js +1 -0
  37. package/package.json +31 -42
  38. package/dist/Either-Ccg0S1uS.d.ts +0 -1015
  39. package/dist/Typeable-DiGVtDnq.d.ts +0 -96
  40. package/dist/branded/index.mjs +0 -2
  41. package/dist/branded/index.mjs.map +0 -1
  42. package/dist/chunk-6ZIR6EKC.mjs +0 -38
  43. package/dist/chunk-6ZIR6EKC.mjs.map +0 -1
  44. package/dist/chunk-7JJIUQXK.mjs +0 -3
  45. package/dist/chunk-7JJIUQXK.mjs.map +0 -1
  46. package/dist/chunk-OR6V4TCO.mjs +0 -2
  47. package/dist/chunk-OR6V4TCO.mjs.map +0 -1
  48. package/dist/do/index.mjs +0 -2
  49. package/dist/do/index.mjs.map +0 -1
  50. package/dist/either/index.mjs +0 -2
  51. package/dist/either/index.mjs.map +0 -1
  52. package/dist/fpromise/index.mjs +0 -2
  53. package/dist/fpromise/index.mjs.map +0 -1
  54. package/dist/index.mjs +0 -2
  55. package/dist/index.mjs.map +0 -1
  56. package/dist/list/index.mjs +0 -2
  57. package/dist/list/index.mjs.map +0 -1
  58. package/dist/map/index.mjs +0 -2
  59. package/dist/map/index.mjs.map +0 -1
  60. package/dist/option/index.mjs +0 -2
  61. package/dist/option/index.mjs.map +0 -1
  62. package/dist/set/index.mjs +0 -2
  63. package/dist/set/index.mjs.map +0 -1
  64. package/dist/try/index.mjs +0 -2
  65. package/dist/try/index.mjs.map +0 -1
  66. package/dist/tuple/index.mjs +0 -2
  67. package/dist/tuple/index.mjs.map +0 -1
@@ -1,1015 +0,0 @@
1
- import { T as Type, P as Pipe, a as Typeable, S as Serializable, F as Foldable } from './Typeable-DiGVtDnq.js';
2
-
3
- /**
4
- * Protocol definitions for Do-notation
5
- * Separated from main Do module to avoid circular dependencies
6
- */
7
- /**
8
- * Result type for Do-notation unwrapping
9
- * Indicates whether unwrapping succeeded and provides the value or error
10
- */
11
- type DoResult<T> = {
12
- ok: true;
13
- value: T;
14
- } | {
15
- ok: false;
16
- empty: true;
17
- } | {
18
- ok: false;
19
- empty: false;
20
- error: unknown;
21
- };
22
- /**
23
- * Interface for types that support Do-notation
24
- * Implementing this interface allows a type to be yielded in Do-comprehensions
25
- *
26
- * The doUnwrap method should return a DoResult indicating success/failure
27
- * and the contained value or error information
28
- */
29
- interface Doable<T> {
30
- doUnwrap(): DoResult<T>;
31
- }
32
-
33
- /**
34
- * Interface for operations that can fail with exceptions.
35
- * These methods should be used with care as they can throw at runtime.
36
- *
37
- * @interface Unsafe
38
- * @template T The type of value that can be extracted
39
- */
40
- interface Unsafe<T extends Type> {
41
- /**
42
- * Extract the value or throw an error
43
- * @param error Optional custom error to throw. If not provided, uses type-appropriate default error
44
- * @throws {Error} The specified error, container's error, or a sensible default
45
- * @returns The contained value
46
- */
47
- orThrow(error?: Error): T;
48
- }
49
- /**
50
- * Extractable type class for data structures that can extract their values
51
- * with various fallback strategies.
52
- *
53
- * This interface is implemented by Option, Either, and other types that
54
- * wrap values and need both safe and fallible extraction methods.
55
- *
56
- * Extends Unsafe to provide exception-throwing operations alongside safe alternatives.
57
- */
58
- interface Extractable<T extends Type> extends Unsafe<T> {
59
- /**
60
- * Returns the contained value or a default value
61
- * @param defaultValue - The value to return if extraction fails
62
- * @returns The contained value or defaultValue
63
- */
64
- orElse(defaultValue: T): T;
65
- /**
66
- * Returns this container if it has a value, otherwise returns the alternative container
67
- * @param alternative - The alternative container
68
- * @returns This container or the alternative
69
- */
70
- or(alternative: Extractable<T>): Extractable<T>;
71
- /**
72
- * Returns the contained value or null
73
- * @returns The contained value or null
74
- */
75
- orNull(): T | null;
76
- /**
77
- * Returns the contained value or undefined
78
- * @returns The contained value or undefined
79
- */
80
- orUndefined(): T | undefined;
81
- }
82
- /**
83
- * Type guard to check if a value implements Extractable
84
- */
85
- declare function isExtractable<T extends Type>(value: unknown): value is Extractable<T>;
86
-
87
- /**
88
- * Universal operations that work on any container (single-value or collection).
89
- * These operations make sense for Option, Either, Try, List, Set, etc.
90
- *
91
- * @typeParam A - The type of value(s) in the container
92
- */
93
- interface ContainerOps<A extends Type> {
94
- /**
95
- * Counts elements that satisfy the predicate.
96
- * For single-value containers: returns 0 or 1
97
- * For collections: returns the count of matching elements
98
- */
99
- count(p: (x: A) => boolean): number;
100
- /**
101
- * Finds the first element that satisfies the predicate.
102
- * For single-value containers: returns Some(value) if predicate matches, None otherwise
103
- * For collections: returns the first matching element wrapped in Option
104
- */
105
- find(p: (a: A) => boolean): Option<A>;
106
- /**
107
- * Tests whether any element satisfies the predicate.
108
- * For single-value containers: tests the single value
109
- * For collections: returns true if any element matches
110
- */
111
- exists(p: (a: A) => boolean): boolean;
112
- /**
113
- * Applies an effect function to each element.
114
- * For single-value containers: applies to the value if present
115
- * For collections: applies to each element
116
- */
117
- forEach(f: (a: A) => void): void;
118
- }
119
- /**
120
- * Operations specific to collections (List, Set, etc.).
121
- * These operations don't make sense for single-value containers.
122
- *
123
- * @typeParam A - The element type
124
- * @typeParam Self - The collection type itself for proper return types
125
- */
126
- interface CollectionOps<A extends Type, Self> {
127
- /**
128
- * Drops the first n elements from the collection.
129
- */
130
- drop(n: number): Self;
131
- /**
132
- * Drops the last n elements from the collection.
133
- */
134
- dropRight(n: number): Self;
135
- /**
136
- * Drops elements from the start while the predicate is true.
137
- */
138
- dropWhile(p: (a: A) => boolean): Self;
139
- /**
140
- * Flattens a collection of collections into a single collection.
141
- */
142
- flatten<B>(): Self;
143
- /**
144
- * Gets the first element of the collection.
145
- */
146
- get head(): A | undefined;
147
- /**
148
- * Gets the first element wrapped in Option.
149
- */
150
- get headOption(): Option<A>;
151
- /**
152
- * Converts the collection to an array.
153
- */
154
- toArray<B = A>(): B[];
155
- }
156
-
157
- /**
158
- * Functor type class - supports mapping over wrapped values
159
- *
160
- * Laws:
161
- * - Identity: fa.map(x => x) ≡ fa
162
- * - Composition: fa.map(f).map(g) ≡ fa.map(x => g(f(x)))
163
- */
164
- interface Functor<A extends Type> {
165
- map<B extends Type>(f: (value: A) => B): Functor<B>;
166
- }
167
- /**
168
- * Applicative functor - supports applying wrapped functions to wrapped values
169
- *
170
- * Laws:
171
- * - Identity: pure(x => x).ap(v) ≡ v
172
- * - Composition: pure(compose).ap(u).ap(v).ap(w) ≡ u.ap(v.ap(w))
173
- * - Homomorphism: pure(f).ap(pure(x)) ≡ pure(f(x))
174
- * - Interchange: u.ap(pure(y)) ≡ pure(f => f(y)).ap(u)
175
- */
176
- interface Applicative<A extends Type> extends Functor<A> {
177
- ap<B extends Type>(ff: Applicative<(value: A) => B>): Applicative<B>;
178
- }
179
- /**
180
- * Monad type class - supports flat mapping (chaining) operations
181
- *
182
- * Laws:
183
- * - Left identity: pure(a).flatMap(f) ≡ f(a)
184
- * - Right identity: m.flatMap(pure) ≡ m
185
- * - Associativity: m.flatMap(f).flatMap(g) ≡ m.flatMap(x => f(x).flatMap(g))
186
- */
187
- interface Monad<A extends Type> extends Applicative<A> {
188
- flatMap<B extends Type>(f: (value: A) => Monad<B>): Monad<B>;
189
- }
190
- /**
191
- * Async monad - supports asynchronous monadic operations
192
- * Extends Monad so it has map, ap, and flatMap in addition to flatMapAsync
193
- */
194
- interface AsyncMonad<A extends Type> extends Monad<A> {
195
- flatMapAsync<B extends Type>(f: (value: A) => PromiseLike<AsyncMonad<B>>): PromiseLike<AsyncMonad<B>>;
196
- }
197
-
198
- /**
199
- * Promisable trait - supports conversion to Promise
200
- *
201
- * Represents containers or values that can be converted to Promise form.
202
- * This enables integration with async/await patterns and Promise-based APIs
203
- * while maintaining functional programming principles.
204
- *
205
- * @template A - The type of value contained within the Promise
206
- *
207
- * @example
208
- * ```typescript
209
- * const either: Either<string, number> = Right(42)
210
- * const promise: Promise<number> = either.toPromise()
211
- * // Promise resolves with 42
212
- *
213
- * const leftEither: Either<string, number> = Left("error")
214
- * const failedPromise: Promise<number> = leftEither.toPromise()
215
- * // Promise rejects with "error"
216
- * ```
217
- */
218
- interface Promisable<A extends Type> {
219
- /**
220
- * Converts this container to a Promise
221
- *
222
- * The behavior depends on the implementing container:
223
- * - Success/Right/Some containers resolve with their value
224
- * - Failure/Left/None containers reject with their error/default error
225
- *
226
- * @returns A Promise that resolves or rejects based on the container's state
227
- */
228
- toPromise(): Promise<A>;
229
- }
230
-
231
- /**
232
- * Possible types of Try instances
233
- */
234
- type TypeNames = "Success" | "Failure";
235
- interface Try<T> extends FunctypeBase<T, TypeNames>, Extractable<T>, Pipe<T>, Promisable<T>, Doable<T>, Reshapeable<T> {
236
- readonly _tag: TypeNames;
237
- readonly error: Error | undefined;
238
- isSuccess(): this is Try<T> & {
239
- readonly _tag: "Success";
240
- error: undefined;
241
- };
242
- isFailure(): this is Try<T> & {
243
- readonly _tag: "Failure";
244
- error: Error;
245
- };
246
- orElse: (defaultValue: T) => T;
247
- orThrow: (error?: Error) => T;
248
- or: (alternative: Try<T>) => Try<T>;
249
- orNull: () => T | null;
250
- orUndefined: () => T | undefined;
251
- toOption: () => Option<T>;
252
- toEither: <E extends Type>(leftValue: E) => Either<E, T>;
253
- toList: () => List<T>;
254
- toTry: () => Try<T>;
255
- map: <U>(f: (value: T) => U) => Try<U>;
256
- ap: <U>(ff: Try<(value: T) => U>) => Try<U>;
257
- flatMap: <U>(f: (value: T) => Try<U>) => Try<U>;
258
- flatMapAsync: <U>(f: (value: T) => Promise<Try<U>>) => Promise<Try<U>>;
259
- /**
260
- * Pattern matches over the Try, applying onFailure if Failure and onSuccess if Success
261
- * @param onFailure - Function to apply if the Try is Failure
262
- * @param onSuccess - Function to apply if the Try is Success
263
- * @returns The result of applying the appropriate function
264
- */
265
- fold: <U extends Type>(onFailure: (error: Error) => U, onSuccess: (value: T) => U) => U;
266
- toString: () => string;
267
- /**
268
- * Pattern matches over the Try, applying a handler function based on the variant
269
- * @param patterns - Object with handler functions for Success and Failure variants
270
- * @returns The result of applying the matching handler function
271
- */
272
- match<R>(patterns: {
273
- Success: (value: T) => R;
274
- Failure: (error: Error) => R;
275
- }): R;
276
- toValue(): {
277
- _tag: TypeNames;
278
- value: T | Error;
279
- };
280
- }
281
- declare const Try: (<T>(f: () => T) => Try<T>) & {
282
- /**
283
- * Type guard to check if a Try is Success
284
- * @param tryValue - The Try to check
285
- * @returns True if Try is Success
286
- */
287
- isSuccess: <T>(tryValue: Try<T>) => tryValue is Try<T> & {
288
- readonly _tag: "Success";
289
- error: undefined;
290
- };
291
- /**
292
- * Type guard to check if a Try is Failure
293
- * @param tryValue - The Try to check
294
- * @returns True if Try is Failure
295
- */
296
- isFailure: <T>(tryValue: Try<T>) => tryValue is Try<T> & {
297
- readonly _tag: "Failure";
298
- error: Error;
299
- };
300
- /**
301
- * Creates a Try from JSON string
302
- * @param json - The JSON string
303
- * @returns Try instance
304
- */
305
- fromJSON: <T>(json: string) => Try<T>;
306
- /**
307
- * Creates a Try from YAML string
308
- * @param yaml - The YAML string
309
- * @returns Try instance
310
- */
311
- fromYAML: <T>(yaml: string) => Try<T>;
312
- /**
313
- * Creates a Try from binary string
314
- * @param binary - The binary string
315
- * @returns Try instance
316
- */
317
- fromBinary: <T>(binary: string) => Try<T>;
318
- };
319
-
320
- /**
321
- * Interface for types that can be reshaped (converted) between different monadic containers.
322
- * Provides standard conversion methods to transform between Option, Either, List, and Try types.
323
- *
324
- * @typeParam T - The type of the value contained in the monad
325
- *
326
- * @example
327
- * // Convert Option to Either
328
- * const opt = Option(5)
329
- * const either = opt.toEither("None value") // Right(5)
330
- *
331
- * @example
332
- * // Convert Either to Option
333
- * const right = Right(10)
334
- * const option = right.toOption() // Some(10)
335
- *
336
- * @example
337
- * // Convert List to Try
338
- * const list = List([1, 2, 3])
339
- * const tryVal = list.toTry() // Success(1) - uses first element
340
- *
341
- * @example
342
- * // Use with Do comprehensions
343
- * const result = Do(function* () {
344
- * const x = yield* $(Option(5))
345
- * const y = yield* $(Right<string, number>(10))
346
- * return x + y
347
- * })
348
- *
349
- * // Convert to desired type for chaining
350
- * const asOption = result.toOption()
351
- * asOption.map(x => x * 2).orElse(0)
352
- */
353
- interface Reshapeable<T extends Type> {
354
- /**
355
- * Converts this monad to an Option.
356
- *
357
- * Conversion rules:
358
- * - Option: returns self
359
- * - Either: Right → Some, Left → None
360
- * - List: non-empty → Some(head), empty → None
361
- * - Try: Success → Some, Failure → None
362
- *
363
- * @returns An Option containing the value if present, None otherwise
364
- */
365
- toOption(): Option<T>;
366
- /**
367
- * Converts this monad to an Either.
368
- *
369
- * Conversion rules:
370
- * - Option: Some → Right, None → Left(leftValue)
371
- * - Either: returns self
372
- * - List: non-empty → Right(head), empty → Left(leftValue)
373
- * - Try: Success → Right, Failure → Left(error)
374
- *
375
- * @param leftValue - The value to use for the Left case when the source is empty/none/failure
376
- * @returns An Either with the value as Right or the provided leftValue as Left
377
- */
378
- toEither<E extends Type>(leftValue: E): Either<E, T>;
379
- /**
380
- * Converts this monad to a List.
381
- *
382
- * Conversion rules:
383
- * - Option: Some → List([value]), None → List([])
384
- * - Either: Right → List([value]), Left → List([])
385
- * - List: returns self
386
- * - Try: Success → List([value]), Failure → List([])
387
- *
388
- * @returns A List containing the value(s) if present, empty List otherwise
389
- */
390
- toList(): List<T>;
391
- /**
392
- * Converts this monad to a Try.
393
- *
394
- * Conversion rules:
395
- * - Option: Some → Success, None → Failure(Error("None"))
396
- * - Either: Right → Success, Left → Failure(Error(leftValue))
397
- * - List: non-empty → Success(head), empty → Failure(Error("Empty list"))
398
- * - Try: returns self
399
- *
400
- * @returns A Try containing Success with the value or Failure with an appropriate error
401
- */
402
- toTry(): Try<T>;
403
- }
404
-
405
- /**
406
- * Creates a Some variant of Option containing a value.
407
- * @param value - The value to wrap in Some
408
- * @returns A new Some instance containing the value
409
- * @typeParam T - The type of the value
410
- */
411
- declare const Some: <T extends Type>(value: T) => Option<T>;
412
- /**
413
- * Creates a None variant of Option representing absence of a value.
414
- * @returns A new None instance
415
- * @typeParam T - The type that would be contained if this was a Some
416
- */
417
- declare const None: <T extends Type>() => Option<T>;
418
- /**
419
- * Safely wraps a value that might be null or undefined in an Option.
420
- * Creates Some if the value is defined, None otherwise.
421
- * @param value - The value to wrap (might be null/undefined)
422
- * @returns Some(value) if value is defined, None otherwise
423
- * @typeParam T - The type of the value
424
- */
425
- declare const OptionConstructor: <T extends Type>(value: T | null | undefined) => Option<T>;
426
- /**
427
- * Option type module
428
- * @module Option
429
- * @category Core
430
- */
431
- /**
432
- * The Option type represents a value that may or may not exist.
433
- * It's used to handle potentially null or undefined values in a type-safe way.
434
- * @typeParam T - The type of the value contained in the Option
435
- */
436
- interface Option<T extends Type> extends Functype<T, "Some" | "None">, Promisable<T>, Doable<T>, Reshapeable<T> {
437
- /** The contained value (undefined for None) */
438
- readonly value: T | undefined;
439
- /** Whether this Option contains no value */
440
- isEmpty: boolean;
441
- /**
442
- * Returns true if this Option is a Some (contains a value)
443
- * @returns true if this Option contains a value, false otherwise
444
- */
445
- isSome(): this is Option<T> & {
446
- value: T;
447
- isEmpty: false;
448
- };
449
- /**
450
- * Returns true if this Option is a None (contains no value)
451
- * @returns true if this Option is empty, false otherwise
452
- */
453
- isNone(): this is Option<T> & {
454
- value: undefined;
455
- isEmpty: true;
456
- };
457
- /**
458
- * Returns the contained value or a default value if None
459
- * @param defaultValue - The value to return if this Option is None
460
- * @returns The contained value or defaultValue
461
- */
462
- orElse(defaultValue: T): T;
463
- /**
464
- * Returns the contained value or throws an error if None
465
- * @param error - Optional custom error to throw. If not provided, throws a default error
466
- * @returns The contained value
467
- * @throws The specified error or a default error if the Option is None
468
- */
469
- orThrow(error?: Error): T;
470
- /**
471
- * Returns this Option if it contains a value, otherwise returns the alternative container
472
- * @param alternative - The alternative Option to return if this is None
473
- * @returns This Option or the alternative
474
- */
475
- or(alternative: Option<T>): Option<T>;
476
- /**
477
- * Returns the contained value or null if None
478
- * @returns The contained value or null
479
- */
480
- orNull(): T | null;
481
- /**
482
- * Returns the contained value or undefined if None
483
- * @returns The contained value or undefined
484
- */
485
- orUndefined(): T | undefined;
486
- /**
487
- * Maps the value inside the Option using the provided function
488
- * @param f - The mapping function
489
- * @returns A new Option containing the mapped value, or None if this Option is None
490
- */
491
- map<U extends Type>(f: (value: T) => U): Option<U>;
492
- /**
493
- * Applies a wrapped function to a wrapped value (Applicative pattern)
494
- * @param ff - An Option containing a function from T to U
495
- * @returns A new Option containing the result of applying the function
496
- */
497
- ap<U extends Type>(ff: Option<(value: T) => U>): Option<U>;
498
- /**
499
- * Returns this Option if it contains a value that satisfies the predicate, otherwise returns None
500
- * @param predicate - The predicate function to test the value
501
- * @returns This Option or None
502
- */
503
- filter(predicate: (value: T) => boolean): Option<T>;
504
- /**
505
- * Maps the value using a function that returns an Option
506
- * @param f - The mapping function returning an Option
507
- * @returns The result of applying f to the contained value, or None if this Option is None
508
- */
509
- flatMap<U extends Type>(f: (value: T) => Option<U>): Option<U>;
510
- /**
511
- * Maps the value using an async function that returns an Option
512
- * @param f - The async mapping function returning an Option
513
- * @returns Promise of the result of applying f to the contained value, or None if this Option is None
514
- */
515
- flatMapAsync<U extends Type>(f: (value: T) => Promise<Option<U>>): Promise<Option<U>>;
516
- /**
517
- * Applies a binary operator to a start value and the contained value
518
- * @param f - The binary operator
519
- * @returns The result of the reduction
520
- */
521
- reduce<U>(f: (acc: U, value: T) => U): U;
522
- /**
523
- * Applies a binary operator to the contained value and a start value
524
- * @param f - The binary operator
525
- * @returns The result of the reduction
526
- */
527
- reduceRight<U>(f: (acc: U, value: T) => U): U;
528
- /**
529
- * Pattern matches over the Option, applying onNone if None and onSome if Some
530
- * @param onNone - Function to apply if the Option is None
531
- * @param onSome - Function to apply if the Option has a value
532
- * @returns The result of applying the appropriate function
533
- */
534
- fold<U>(onNone: () => U, onSome: (value: T) => U): U;
535
- /**
536
- * Left-associative fold using the provided zero value and operation
537
- * @param z - Zero/identity value
538
- * @returns A function that takes an operation to apply
539
- */
540
- foldLeft<B>(z: B): (op: (b: B, a: T) => B) => B;
541
- /**
542
- * Right-associative fold using the provided zero value and operation
543
- * @param z - Zero/identity value
544
- * @returns A function that takes an operation to apply
545
- */
546
- foldRight<B>(z: B): (op: (a: T, b: B) => B) => B;
547
- /**
548
- * Converts this Option to a List
549
- * @returns A List containing the value if Some, or empty List if None
550
- */
551
- toList(): List<T>;
552
- /**
553
- * Checks if this Option contains the specified value
554
- * @param value - The value to check for
555
- * @returns true if this Option contains the value, false otherwise
556
- */
557
- contains(value: T): boolean;
558
- /** The number of elements in this Option (0 or 1) */
559
- size: number;
560
- /**
561
- * Converts this Option to an Either
562
- * @param left - The value to use for Left if this Option is None
563
- * @returns Either.Right with the contained value if Some, or Either.Left with left if None
564
- */
565
- toEither<E>(left: E): Either<E, T>;
566
- /**
567
- * Returns a string representation of this Option
568
- * @returns A string representation
569
- */
570
- toString(): string;
571
- /**
572
- * Returns a simple object representation of this Option
573
- * @returns An object with _tag and value properties
574
- */
575
- toValue(): {
576
- _tag: "Some" | "None";
577
- value: T;
578
- };
579
- /**
580
- * Pattern matches over the Option, applying a handler function based on the variant
581
- * @param patterns - Object with handler functions for Some and None variants
582
- * @returns The result of applying the matching handler function
583
- */
584
- match<R>(patterns: {
585
- Some: (value: T) => R;
586
- None: () => R;
587
- }): R;
588
- }
589
- declare const Option: (<T extends Type>(value: T | null | undefined) => Option<T>) & {
590
- /**
591
- * Creates an Option from any value. Alias for Option function.
592
- * @param value - The value to wrap
593
- * @returns Some(value) if value is defined, None otherwise
594
- * @typeParam T - The type of the value
595
- */
596
- from: <T>(value: T) => Option<T>;
597
- /**
598
- * Returns a None instance. Alias for None function.
599
- * @returns A None instance
600
- * @typeParam T - The type that would be contained if this was a Some
601
- */
602
- none: <T>() => Option<T>;
603
- /**
604
- * Type guard to check if an Option is Some
605
- * @param option - The Option to check
606
- * @returns True if Option is Some
607
- */
608
- isSome: <T>(option: Option<T>) => option is Option<T> & {
609
- value: T;
610
- isEmpty: false;
611
- };
612
- /**
613
- * Type guard to check if an Option is None
614
- * @param option - The Option to check
615
- * @returns True if Option is None
616
- */
617
- isNone: <T>(option: Option<T>) => option is Option<T> & {
618
- value: undefined;
619
- isEmpty: true;
620
- };
621
- /**
622
- * Creates an Option from JSON string
623
- * @param json - The JSON string
624
- * @returns Option instance
625
- */
626
- fromJSON: <T>(json: string) => Option<T>;
627
- /**
628
- * Creates an Option from YAML string
629
- * @param yaml - The YAML string
630
- * @returns Option instance
631
- */
632
- fromYAML: <T>(yaml: string) => Option<T>;
633
- /**
634
- * Creates an Option from binary string
635
- * @param binary - The binary string
636
- * @returns Option instance
637
- */
638
- fromBinary: <T>(binary: string) => Option<T>;
639
- };
640
-
641
- interface List<A> extends FunctypeCollection<A, "List">, Doable<A>, Reshapeable<A> {
642
- readonly length: number;
643
- readonly [Symbol.iterator]: () => Iterator<A>;
644
- map: <B>(f: (a: A) => B) => List<B>;
645
- ap: <B>(ff: List<(value: A) => B>) => List<B>;
646
- flatMap: <B>(f: (a: A) => Iterable<B>) => List<B>;
647
- flatMapAsync: <B>(f: (a: A) => PromiseLike<Iterable<B>>) => PromiseLike<List<B>>;
648
- filter<S extends A>(predicate: (a: A) => a is S): List<S>;
649
- filter(predicate: (a: A) => unknown): List<A>;
650
- filterNot: (p: (a: A) => boolean) => List<A>;
651
- /** @internal */
652
- filterType: <T extends Typeable<string, unknown>>(tag: string) => List<T & A>;
653
- remove: (value: A) => List<A>;
654
- removeAt: (index: number) => List<A>;
655
- add: (item: A) => List<A>;
656
- get: (index: number) => Option<A>;
657
- concat: (other: List<A>) => List<A>;
658
- /**
659
- * Pattern matches over the List, applying a handler function based on whether it's empty
660
- * @param patterns - Object with handler functions for Empty and NonEmpty variants
661
- * @returns The result of applying the matching handler function
662
- */
663
- match<R>(patterns: {
664
- Empty: () => R;
665
- NonEmpty: (values: A[]) => R;
666
- }): R;
667
- }
668
- declare const List: (<A>(values?: Iterable<A>) => List<A>) & {
669
- /**
670
- * Creates a List from JSON string
671
- * @param json - The JSON string
672
- * @returns List instance
673
- */
674
- fromJSON: <A>(json: string) => List<A>;
675
- /**
676
- * Creates a List from YAML string
677
- * @param yaml - The YAML string
678
- * @returns List instance
679
- */
680
- fromYAML: <A>(yaml: string) => List<A>;
681
- /**
682
- * Creates a List from binary string
683
- * @param binary - The binary string
684
- * @returns List instance
685
- */
686
- fromBinary: <A>(binary: string) => List<A>;
687
- };
688
-
689
- interface Set<A> extends FunctypeCollection<A, "Set">, Collection<A> {
690
- add: (value: A) => Set<A>;
691
- remove: (value: A) => Set<A>;
692
- contains: (value: A) => boolean;
693
- has: (value: A) => boolean;
694
- map: <B>(f: (a: A) => B) => Set<B>;
695
- flatMap: <B>(f: (a: A) => Iterable<B>) => Set<B>;
696
- filter: (p: (a: A) => boolean) => Set<A>;
697
- filterNot: (p: (a: A) => boolean) => Set<A>;
698
- fold: <U extends Type>(onEmpty: () => U, onValue: (value: A) => U) => U;
699
- toList: () => List<A>;
700
- toSet: () => Set<A>;
701
- toArray: <B = A>() => B[];
702
- toString: () => string;
703
- }
704
- declare const Set: (<A>(iterable?: Iterable<A>) => Set<A>) & {
705
- /**
706
- * Creates a Set from JSON string
707
- * @param json - The JSON string
708
- * @returns Set instance
709
- */
710
- fromJSON: <A>(json: string) => Set<A>;
711
- /**
712
- * Creates a Set from YAML string
713
- * @param yaml - The YAML string
714
- * @returns Set instance
715
- */
716
- fromYAML: <A>(yaml: string) => Set<A>;
717
- /**
718
- * Creates a Set from binary string
719
- * @param binary - The binary string
720
- * @returns Set instance
721
- */
722
- fromBinary: <A>(binary: string) => Set<A>;
723
- };
724
-
725
- /**
726
- * Represents a collection with conversion capabilities
727
- * @interface
728
- * @module Collections
729
- * @category Core
730
- */
731
- interface Collection<A> {
732
- toList(): List<A>;
733
- toSet(): Set<A>;
734
- toString(): string;
735
- }
736
-
737
- /**
738
- * Pattern matching interface for functional data types.
739
- *
740
- * @typeParam A - The type of elements in the data structure
741
- * @typeParam Tags - The type of tags used for pattern matching
742
- */
743
- interface Matchable<A, Tags extends string = string> {
744
- /**
745
- * Pattern matches against this data structure, applying handlers for each variant based on tag.
746
- * Similar to fold but with stronger type safety for tag-based variants.
747
- *
748
- * The return type is inferred from the pattern handlers when not explicitly specified.
749
- *
750
- * @param patterns - An object containing handler functions for each variant
751
- * @returns The result of applying the matching handler function
752
- */
753
- match<R>(patterns: Record<Tags, (value: A) => R>): R;
754
- }
755
- /**
756
- * Utility functions for working with Matchable data structures
757
- */
758
- declare const MatchableUtils: {
759
- /**
760
- * Helper function to create a default case for pattern matching
761
- *
762
- * @param handler - The default handler function to apply
763
- * @returns A function that always applies the default handler
764
- */
765
- default: <A, R>(handler: (value: A) => R) => (value: A) => R;
766
- /**
767
- * Helper function to create a match pattern that guards based on a predicate
768
- *
769
- * @param predicate - The predicate function for guarding
770
- * @param handler - The handler function to apply if the predicate passes
771
- * @returns A function that applies the handler only if the predicate passes
772
- */
773
- when: <A, R>(predicate: (value: A) => boolean, handler: (value: A) => R) => (value: A) => R | undefined;
774
- };
775
-
776
- /**
777
- * Traversable typeclass for data structures that can be traversed through
778
- */
779
- interface Traversable<A extends Type> extends AsyncMonad<A> {
780
- get size(): number;
781
- get isEmpty(): boolean;
782
- contains(value: A): boolean;
783
- reduce(f: (b: A, a: A) => A): A;
784
- reduceRight(f: (b: A, a: A) => A): A;
785
- }
786
-
787
- /**
788
- * Base interface for all functype data structures.
789
- * This provides a standard contract with core functional programming traits.
790
- *
791
- * @typeParam A - The type of value contained in the functor
792
- * @typeParam Tag - The type tag for pattern matching (e.g., "Some" | "None" for Option)
793
- *
794
- * @example
795
- * ```typescript
796
- * // Implementing FunctypeBase for a custom data structure
797
- * class MyContainer<T> implements FunctypeBase<T, "Empty" | "Full"> {
798
- * // Implementation of all required methods...
799
- * }
800
- * ```
801
- */
802
- interface FunctypeBase<A, Tag extends string = string> extends AsyncMonad<A>, Traversable<A>, Serializable<A>, Foldable<A>, Typeable<Tag>, ContainerOps<A> {
803
- readonly _tag: Tag;
804
- }
805
- /**
806
- * Interface for single-value containers like Option, Either, Try.
807
- * Extends FunctypeBase with extraction methods and Pipe.
808
- *
809
- * @typeParam A - The type of value contained
810
- * @typeParam Tag - The type tag for pattern matching
811
- */
812
- interface Functype<A, Tag extends string = string> extends FunctypeBase<A, Tag>, Extractable<A>, Pipe<A>, Matchable<A, Tag> {
813
- toValue(): {
814
- _tag: Tag;
815
- value: A;
816
- };
817
- }
818
- /**
819
- * A version of Functype for collection types that need iteration support.
820
- * Extends FunctypeBase with Iterable protocol but without Extractable.
821
- *
822
- * @typeParam A - The element type of the collection
823
- * @typeParam Tag - The type tag for pattern matching
824
- */
825
- 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>> {
826
- toValue(): {
827
- _tag: Tag;
828
- value: A[];
829
- };
830
- flatMap<B extends Type>(f: (value: A) => Iterable<B>): FunctypeCollection<B, Tag>;
831
- flatMapAsync<B extends Type>(f: (value: A) => PromiseLike<Iterable<B>>): PromiseLike<FunctypeCollection<B, Tag>>;
832
- }
833
-
834
- type TestEither<L extends Type, R extends Type> = Either<L, R> & AsyncMonad<R>;
835
- declare const Right: <L extends Type, R extends Type>(value: R) => Either<L, R>;
836
- declare const Left: <L extends Type, R extends Type>(value: L) => Either<L, R>;
837
- declare const isRight: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
838
- value: R;
839
- };
840
- declare const isLeft: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
841
- value: L;
842
- };
843
- declare const tryCatch: <L extends Type, R extends Type>(f: () => R, onError: (error: unknown) => L) => Either<L, R>;
844
- declare const TypeCheckRight: <L extends Type, R extends Type>(value: R) => TestEither<L, R>;
845
- declare const TypeCheckLeft: <L extends Type, R extends Type>(value: L) => TestEither<L, R>;
846
- declare const tryCatchAsync: <L extends Type, R extends Type>(f: () => Promise<R>, onError: (error: unknown) => L) => Promise<Either<L, R>>;
847
- /**
848
- * Either type module
849
- * @module Either
850
- * @category Core
851
- */
852
- interface Either<L extends Type, R extends Type> extends FunctypeBase<R, "Left" | "Right">, Promisable<R>, Doable<R>, Reshapeable<R>, Extractable<R> {
853
- readonly _tag: "Left" | "Right";
854
- value: L | R;
855
- isLeft(): this is Either<L, R> & {
856
- readonly _tag: "Left";
857
- value: L;
858
- };
859
- isRight(): this is Either<L, R> & {
860
- readonly _tag: "Right";
861
- value: R;
862
- };
863
- orElse: (defaultValue: R) => R;
864
- orThrow: (error?: Error) => R;
865
- or(alternative: Either<L, R>): Either<L, R>;
866
- orNull: () => R | null;
867
- orUndefined: () => R | undefined;
868
- readonly map: <U extends Type>(f: (value: R) => U) => Either<L, U>;
869
- ap: <U extends Type>(ff: Either<L, (value: R) => U>) => Either<L, U>;
870
- merge: <L1 extends Type, R1 extends Type>(other: Either<L1, R1>) => Either<L | L1, [R, R1]>;
871
- mapAsync: <U extends Type>(f: (value: R) => Promise<U>) => Promise<Either<L, U>>;
872
- flatMap: <U extends Type>(f: (value: R) => Either<L, U>) => Either<L, U>;
873
- flatMapAsync: <U extends Type>(f: (value: R) => Promise<Either<L, U>>) => Promise<Either<L, U>>;
874
- toOption: () => Option<R>;
875
- toList: () => List<R>;
876
- toString: () => string;
877
- [Symbol.iterator]: () => Iterator<R>;
878
- yield: () => Generator<R, void, unknown>;
879
- traverse: <U extends Type>(f: (value: R) => Either<L, U>) => Either<L, U[]>;
880
- lazyMap: <U extends Type>(f: (value: R) => U) => Generator<Either<L, U>, void, unknown>;
881
- tap: (f: (value: R) => void) => Either<L, R>;
882
- tapLeft: (f: (value: L) => void) => Either<L, R>;
883
- mapLeft: <L2 extends Type>(f: (value: L) => L2) => Either<L2, R>;
884
- bimap: <L2 extends Type, R2 extends Type>(fl: (value: L) => L2, fr: (value: R) => R2) => Either<L2, R2>;
885
- fold: <T extends Type>(onLeft: (value: L) => T, onRight: (value: R) => T) => T;
886
- swap: () => Either<R, L>;
887
- /**
888
- * Pipes the value through the provided function based on whether this is a Left or Right
889
- * @param onLeft - The function to apply if this is a Left
890
- * @param onRight - The function to apply if this is a Right
891
- * @returns The result of applying the appropriate function
892
- */
893
- pipeEither<U extends Type>(onLeft: (value: L) => U, onRight: (value: R) => U): U;
894
- /**
895
- * Pipes the Either value through the provided function
896
- * @param f - The function to apply to the value (Left or Right)
897
- * @returns The result of applying the function to the value
898
- */
899
- pipe<U extends Type>(f: (value: L | R) => U): U;
900
- /**
901
- * Pattern matches over the Either, applying a handler function based on the variant
902
- * @param patterns - Object with handler functions for Left and Right variants
903
- * @returns The result of applying the matching handler function
904
- */
905
- match<T>(patterns: {
906
- Left: (value: L) => T;
907
- Right: (value: R) => T;
908
- }): T;
909
- /**
910
- * Returns the value and tag for inspection
911
- */
912
- toValue(): {
913
- _tag: "Left" | "Right";
914
- value: L | R;
915
- };
916
- /**
917
- * Custom JSON serialization that excludes getter properties
918
- */
919
- toJSON(): {
920
- _tag: "Left" | "Right";
921
- value: L | R;
922
- };
923
- }
924
- declare const Either: (<L extends Type, R extends Type>(value: R | L, isRight: boolean) => Either<L, R>) & {
925
- /**
926
- * Creates a Left instance
927
- * @param value - The left value
928
- * @returns Left Either
929
- */
930
- left: <L extends Type, R extends Type>(value: L) => Either<L, R>;
931
- /**
932
- * Creates a Right instance
933
- * @param value - The right value
934
- * @returns Right Either
935
- */
936
- right: <L extends Type, R extends Type>(value: R) => Either<L, R>;
937
- /**
938
- * Type guard to check if an Either is Right
939
- * @param either - The Either to check
940
- * @returns True if Either is Right
941
- */
942
- isRight: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
943
- value: R;
944
- };
945
- /**
946
- * Type guard to check if an Either is Left
947
- * @param either - The Either to check
948
- * @returns True if Either is Left
949
- */
950
- isLeft: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
951
- value: L;
952
- };
953
- /**
954
- * Combines an array of Eithers into a single Either containing an array
955
- * @param eithers - Array of Either values
956
- * @returns Either with array of values or first Left encountered
957
- */
958
- sequence: <L extends Type, R extends Type>(eithers: Either<L, R>[]) => Either<L, R[]>;
959
- /**
960
- * Maps an array through a function that returns Either, then sequences the results
961
- * @param arr - Array of values
962
- * @param f - Function that returns Either
963
- * @returns Either with array of results or first Left encountered
964
- */
965
- traverse: <L extends Type, R extends Type, U extends Type>(arr: R[], f: (value: R) => Either<L, U>) => Either<L, U[]>;
966
- /**
967
- * Creates an Either from a nullable value
968
- * @param value - The value that might be null or undefined
969
- * @param leftValue - The value to use for Left if value is null/undefined
970
- * @returns Right if value is not null/undefined, Left otherwise
971
- */
972
- fromNullable: <L extends Type, R extends Type>(value: R | null | undefined, leftValue: L) => Either<L, R>;
973
- /**
974
- * Creates an Either based on a predicate
975
- * @param value - The value to test
976
- * @param predicate - The predicate function
977
- * @param leftValue - The value to use for Left if predicate fails
978
- * @returns Right if predicate passes, Left otherwise
979
- */
980
- fromPredicate: <L extends Type, R extends Type>(value: R, predicate: (value: R) => boolean, leftValue: L) => Either<L, R>;
981
- /**
982
- * Applicative apply - applies a wrapped function to a wrapped value
983
- * @param eitherF - Either containing a function
984
- * @param eitherV - Either containing a value
985
- * @returns Either with function applied to value
986
- */
987
- ap: <L extends Type, R extends Type, U extends Type>(eitherF: Either<L, (value: R) => U>, eitherV: Either<L, R>) => Either<L, U>;
988
- /**
989
- * Creates an Either from a Promise
990
- * @param promise - The Promise to convert
991
- * @param onRejected - Function to convert rejection reason to Left value
992
- * @returns Promise that resolves to Either
993
- */
994
- fromPromise: <L, R>(promise: Promise<R>, onRejected: (reason: unknown) => L) => Promise<Either<L, R>>;
995
- /**
996
- * Creates an Either from JSON string
997
- * @param json - The JSON string
998
- * @returns Either instance
999
- */
1000
- fromJSON: <L extends Type, R extends Type>(json: string) => Either<L, R>;
1001
- /**
1002
- * Creates an Either from YAML string
1003
- * @param yaml - The YAML string
1004
- * @returns Either instance
1005
- */
1006
- fromYAML: <L extends Type, R extends Type>(yaml: string) => Either<L, R>;
1007
- /**
1008
- * Creates an Either from binary string
1009
- * @param binary - The binary string
1010
- * @returns Either instance
1011
- */
1012
- fromBinary: <L extends Type, R extends Type>(binary: string) => Either<L, R>;
1013
- };
1014
-
1015
- export { type AsyncMonad as A, type Collection as C, type DoResult as D, Either as E, type FunctypeBase as F, List as L, type Matchable as M, None as N, Option as O, type Promisable as P, Right as R, Some as S, Try as T, type Extractable as a, type Doable as b, type Traversable as c, type TestEither as d, Left as e, isLeft as f, TypeCheckRight as g, TypeCheckLeft as h, isRight as i, tryCatchAsync as j, isExtractable as k, type Functype as l, type FunctypeCollection as m, MatchableUtils as n, OptionConstructor as o, Set as p, type TypeNames as q, type CollectionOps as r, type ContainerOps as s, tryCatch as t, type Applicative as u, type Functor as v, type Monad as w, type Reshapeable as x };