functype 1.27.1 → 2.0.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/README.md +63 -88
- package/dist/_virtual/_rolldown/runtime.js +1 -0
- package/dist/branded/ValidatedBrand.d.ts +122 -0
- package/dist/branded/ValidatedBrand.js +1 -0
- package/dist/branded/index.d.ts +52 -2
- package/dist/cli/data.d.ts +37 -0
- package/dist/cli/data.js +1 -0
- package/dist/cli/exports.d.ts +5 -46
- package/dist/cli/exports.js +1 -1
- package/dist/cli/formatters.js +4 -0
- package/dist/cli/full-interfaces.d.ts +6 -0
- package/dist/cli/full-interfaces.js +1139 -0
- package/dist/cli/index.js +3 -6
- package/dist/cli/interfaces.generated.js +1 -0
- package/dist/collections/index.d.ts +13 -0
- package/dist/companion/Companion.d.ts +23 -0
- package/dist/companion/Companion.js +1 -0
- package/dist/companion/CompanionTypes.d.ts +30 -0
- package/dist/{CompanionTypes-BVqO7Kc2.js → companion/CompanionTypes.js} +1 -1
- package/dist/companion/index.d.ts +2 -1
- package/dist/companion/index.js +1 -1
- package/dist/conditional/Cond.d.ts +147 -0
- package/dist/conditional/Cond.js +1 -0
- package/dist/conditional/Match.d.ts +295 -0
- package/dist/conditional/Match.js +1 -0
- package/dist/conditional/index.d.ts +3 -2
- package/dist/conditional/index.js +1 -1
- package/dist/core/base/Base.d.ts +12 -0
- package/dist/core/base/Base.js +1 -0
- package/dist/core/throwable/Throwable.d.ts +38 -0
- package/dist/core/throwable/Throwable.js +1 -0
- package/dist/decoder/Decoder.d.ts +15 -0
- package/dist/decoder/DecoderCompanion.js +1 -0
- package/dist/decoder/DecoderError.d.ts +56 -0
- package/dist/decoder/DecoderError.js +2 -0
- package/dist/decoder/Encoder.d.ts +15 -0
- package/dist/decoder/index.d.ts +71 -2
- package/dist/decoder/index.js +1 -1
- package/dist/decoder/tagged.js +1 -0
- package/dist/do/index.d.ts +246 -2
- package/dist/do/index.js +1 -1
- package/dist/do/protocol.d.ts +29 -0
- package/dist/either/index.d.ts +279 -2
- package/dist/either/index.js +1 -1
- package/dist/error/ErrorChain.d.ts +60 -0
- package/dist/error/ErrorChain.js +4 -0
- package/dist/error/ErrorFormatter.d.ts +67 -0
- package/dist/error/ErrorFormatter.js +6 -0
- package/dist/error/ParseError.d.ts +6 -0
- package/dist/error/ParseError.js +1 -0
- package/dist/error/typed/TypedError.d.ts +146 -0
- package/dist/error/typed/TypedError.js +1 -0
- package/dist/error/typed/Validation.d.ts +76 -0
- package/dist/error/typed/Validation.js +1 -0
- package/dist/extractable/Extractable.d.ts +62 -0
- package/dist/extractable/Extractable.js +1 -0
- package/dist/fetch/Http.d.ts +32 -0
- package/dist/fetch/Http.js +1 -0
- package/dist/fetch/HttpClient.d.ts +80 -0
- package/dist/fetch/HttpClient.js +1 -0
- package/dist/fetch/HttpError.d.ts +57 -0
- package/dist/fetch/HttpError.js +1 -0
- package/dist/fetch/HttpRequest.d.ts +103 -0
- package/dist/fetch/index.d.ts +5 -2
- package/dist/fetch/index.js +1 -1
- package/dist/foldable/Foldable.d.ts +27 -0
- package/dist/foldable/index.d.ts +55 -0
- package/dist/foldable/index.js +1 -0
- package/dist/functype/Functype.d.ts +76 -0
- package/dist/functype/FunctypeSum.d.ts +39 -0
- package/dist/functype/index.d.ts +4 -2
- package/dist/hkt/index.d.ts +68 -0
- package/dist/hkt/index.js +1 -0
- package/dist/identity/Identity.d.ts +34 -0
- package/dist/identity/Identity.js +1 -0
- package/dist/index.d.ts +74 -8
- package/dist/index.js +1 -1
- package/dist/internal/base64.js +1 -0
- package/dist/internal/call-alone.js +1 -0
- package/dist/internal/concurrency.d.ts +11 -0
- package/dist/internal/concurrency.js +1 -0
- package/dist/internal/define.js +1 -0
- package/dist/internal/mutation-utils.js +1 -0
- package/dist/internal/signal.js +1 -0
- package/dist/internal/stringify.js +1 -0
- package/dist/invariant/index.d.ts +32 -2
- package/dist/io/Context.d.ts +112 -0
- package/dist/io/Context.js +1 -0
- package/dist/io/Exit.d.ts +249 -0
- package/dist/io/Exit.js +1 -0
- package/dist/io/IO.d.ts +1243 -0
- package/dist/io/IO.js +1 -0
- package/dist/io/Layer.d.ts +139 -0
- package/dist/io/Layer.js +1 -0
- package/dist/io/SpanTracer.d.ts +78 -0
- package/dist/io/SpanTracer.js +1 -0
- package/dist/io/Tag.d.ts +75 -0
- package/dist/io/Tag.js +1 -0
- package/dist/io/TestClock.d.ts +133 -0
- package/dist/io/TestClock.js +1 -0
- package/dist/io/index.d.ts +41 -2
- package/dist/io/index.js +1 -1
- package/dist/lazy/Lazy.d.ts +312 -0
- package/dist/lazy/Lazy.js +1 -0
- package/dist/lazy/index.d.ts +2 -2
- package/dist/lazy/index.js +1 -1
- package/dist/list/LazyList.d.ts +182 -0
- package/dist/list/LazyList.js +1 -0
- package/dist/list/index.d.ts +137 -2
- package/dist/list/index.js +1 -1
- package/dist/logger/index.d.ts +24 -2
- package/dist/map/index.d.ts +95 -2
- package/dist/map/index.js +1 -1
- package/dist/map/shim.d.ts +13 -0
- package/dist/map/shim.js +1 -0
- package/dist/matchable/Matchable.d.ts +38 -0
- package/dist/matchable/Matchable.js +1 -0
- package/dist/obj/Obj.d.ts +212 -0
- package/dist/obj/Obj.js +1 -0
- package/dist/obj/index.d.ts +2 -2
- package/dist/obj/index.js +1 -1
- package/dist/option/index.d.ts +304 -2
- package/dist/option/index.js +1 -1
- package/dist/package.json.js +1 -0
- package/dist/pipe/index.d.ts +14 -0
- package/dist/ref/Ref.d.ts +77 -0
- package/dist/ref/Ref.js +1 -0
- package/dist/reshapeable/Reshapeable.d.ts +90 -0
- package/dist/serializable/Serializable.d.ts +39 -0
- package/dist/serialization/Serialization.d.ts +147 -0
- package/dist/serialization/Serialization.js +1 -0
- package/dist/serialization/SerializationCompanion.d.ts +125 -0
- package/dist/serialization/SerializationCompanion.js +1 -0
- package/dist/serialization/error-envelope.d.ts +44 -0
- package/dist/serialization/error-envelope.js +1 -0
- package/dist/serialization/index.d.ts +4 -2
- package/dist/serialization/index.js +1 -1
- package/dist/set/index.d.ts +106 -2
- package/dist/set/index.js +1 -1
- package/dist/stack/Stack.d.ts +120 -0
- package/dist/stack/Stack.js +1 -0
- package/dist/traced/TraceEvent.d.ts +27 -0
- package/dist/traced/TracedOption.d.ts +46 -0
- package/dist/traced/TracedOption.js +1 -0
- package/dist/traced/Tracer.d.ts +26 -0
- package/dist/traversable/KVTraversable.d.ts +14 -0
- package/dist/traversable/Traversable.d.ts +30 -0
- package/dist/try/index.d.ts +238 -2
- package/dist/try/index.js +1 -1
- package/dist/tuple/index.d.ts +92 -2
- package/dist/tuple/index.js +1 -1
- package/dist/typeable/Typeable.d.ts +40 -0
- package/dist/typeable/Typeable.js +1 -0
- package/dist/typeclass/ContainerOps.d.ts +131 -0
- package/dist/typeclass/Functor.d.ts +41 -0
- package/dist/typeclass/Promisable.d.ts +33 -0
- package/dist/typeclass/index.d.ts +4 -1
- package/dist/typeclass/index.js +1 -1
- package/dist/typeclass/variance.d.ts +54 -0
- package/dist/typeclass/variance.js +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/util/index.d.ts +1 -3
- package/dist/valuable/Valuable.d.ts +28 -0
- package/dist/valuable/Valuable.js +1 -0
- package/dist/wire/index.d.ts +40 -2
- package/package.json +7 -12
- package/dist/Brand-bfnGXuum.d.ts +0 -54
- package/dist/Companion-DiOMBHDG.js +0 -1
- package/dist/CompanionTypes-Cc90cQ4P.d.ts +0 -58
- package/dist/Invariant-DEan-SxV.d.ts +0 -35
- package/dist/Logger-BmprRS8v.d.ts +0 -27
- package/dist/Tuple-B8KM-H8Y.d.ts +0 -213
- package/dist/Wire-3hvw0Ogg.d.ts +0 -42
- package/dist/core/task/index.d.ts +0 -2
- package/dist/core/task/index.js +0 -1
- package/dist/full-interfaces-0XJEOHi_.js +0 -1202
- package/dist/index-CLYh2qZu.d.ts +0 -6649
- package/dist/rolldown-runtime-DK3Fl9T5.js +0 -1
- package/dist/serialization-9_SyrfdG.js +0 -3
- package/dist/src-BTLBc-KK.js +0 -10
- package/dist/typeclass-C61yDVYd.js +0 -1
package/dist/option/index.d.ts
CHANGED
|
@@ -1,2 +1,304 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Doable } from "../do/protocol.js";
|
|
2
|
+
import { Functype } from "../functype/Functype.js";
|
|
3
|
+
import { Reshapeable } from "../reshapeable/Reshapeable.js";
|
|
4
|
+
import { Promisable } from "../typeclass/Promisable.js";
|
|
5
|
+
import { Type } from "../types/index.js";
|
|
6
|
+
import { Either } from "../either/index.js";
|
|
7
|
+
import { List } from "../list/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Option type module
|
|
10
|
+
* @module Option
|
|
11
|
+
* @category Core
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* What every Option has, Some or None. `Option<T>` is the union of `SomeOf<T>` and `NoneOf<T>`, so
|
|
15
|
+
* TypeScript narrows it on `isSome()`, `isNone()` and `_tag`, as it does Either.
|
|
16
|
+
* @typeParam T - The type of the value contained in the Option
|
|
17
|
+
*/
|
|
18
|
+
export interface OptionBase<out T extends Type> extends Functype<T, "Some" | "None">, Promisable<T>, Doable<T>, Reshapeable<T> {
|
|
19
|
+
/** The contained value (undefined for None) */
|
|
20
|
+
readonly value: T | undefined;
|
|
21
|
+
/** Whether this Option contains no value */
|
|
22
|
+
readonly isEmpty: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Returns true if this Option is a Some (contains a value)
|
|
25
|
+
* @returns true if this Option contains a value, false otherwise
|
|
26
|
+
*/
|
|
27
|
+
isSome(): this is SomeOf<T>;
|
|
28
|
+
/**
|
|
29
|
+
* Returns true if this Option is a None (contains no value)
|
|
30
|
+
* @returns true if this Option is empty, false otherwise
|
|
31
|
+
*/
|
|
32
|
+
isNone(): this is NoneOf<T>;
|
|
33
|
+
/**
|
|
34
|
+
* Returns the contained value, or calls `defaultValue` if None. The default is lazy: it runs
|
|
35
|
+
* only on None. The default is typed against `T` first, so `() => "light"` on an
|
|
36
|
+
* `Option<"light" | "dark">` stays `"light" | "dark"`. A default of another type widens the
|
|
37
|
+
* result to `T | T2`, which keeps Option covariant in T.
|
|
38
|
+
* @param defaultValue - A function that returns the value to use if this Option is None
|
|
39
|
+
* @returns The contained value or the default, typed as `T | T2`
|
|
40
|
+
*/
|
|
41
|
+
getOrElse<T2 extends Type = never>(defaultValue: () => T | T2): T | T2;
|
|
42
|
+
/**
|
|
43
|
+
* Returns the contained value or throws an error if None
|
|
44
|
+
* @param error - Optional error to throw, or a builder that makes one (called only on None). If not
|
|
45
|
+
* provided, throws a default error
|
|
46
|
+
* @returns The contained value
|
|
47
|
+
* @throws The specified or built error, or a default error if the Option is None
|
|
48
|
+
*/
|
|
49
|
+
orThrow(error?: Error | (() => Error)): T;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the contained value, or calls the never-returning handler if None.
|
|
52
|
+
* Use this when you have a helper like `(msg) => fail(msg, 2)` that terminates the
|
|
53
|
+
* program — the result type is unconditionally `T`, so you avoid the TypeScript
|
|
54
|
+
* narrowing trap where `if (o.isNone()) fail(...)` fails to narrow `o.value` when
|
|
55
|
+
* `fail` is an arrow function typed as `(...): never`.
|
|
56
|
+
*/
|
|
57
|
+
expect(handler: () => never): T;
|
|
58
|
+
/**
|
|
59
|
+
* Returns this Option if it contains a value, otherwise calls `alternative` for another Option.
|
|
60
|
+
* The alternative is lazy: it runs only on None. It may hold a different type; the result
|
|
61
|
+
* widens to `Option<T | T2>`.
|
|
62
|
+
* @param alternative - A function that returns the Option to use if this is None
|
|
63
|
+
* @returns This Option or the alternative, typed as `Option<T | T2>`
|
|
64
|
+
*/
|
|
65
|
+
orElse<T2 extends Type>(alternative: () => Option<T2>): Option<T | T2>;
|
|
66
|
+
/**
|
|
67
|
+
* Returns the contained value or null if None
|
|
68
|
+
* @returns The contained value or null
|
|
69
|
+
*/
|
|
70
|
+
orNull(): T | null;
|
|
71
|
+
/**
|
|
72
|
+
* Returns the contained value or undefined if None
|
|
73
|
+
* @returns The contained value or undefined
|
|
74
|
+
*/
|
|
75
|
+
orUndefined(): T | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Maps the value inside the Option using the provided function
|
|
78
|
+
* @param f - The mapping function
|
|
79
|
+
* @returns A new Option containing the mapped value, or None if this Option is None
|
|
80
|
+
*/
|
|
81
|
+
map<U extends Type>(f: (value: T) => U): Option<U>;
|
|
82
|
+
/**
|
|
83
|
+
* Applies a wrapped function to a wrapped value (Applicative pattern)
|
|
84
|
+
* @param ff - An Option containing a function from T to U
|
|
85
|
+
* @returns A new Option containing the result of applying the function
|
|
86
|
+
*/
|
|
87
|
+
ap<U extends Type>(ff: Option<(value: T) => U>): Option<U>;
|
|
88
|
+
/**
|
|
89
|
+
* Returns this Option if it contains a value that satisfies the predicate, otherwise returns None
|
|
90
|
+
* @param predicate - The predicate function to test the value
|
|
91
|
+
* @returns This Option or None
|
|
92
|
+
*/
|
|
93
|
+
filter(predicate: (value: T) => boolean): Option<T>;
|
|
94
|
+
/**
|
|
95
|
+
* Maps the value using a function that returns an Option
|
|
96
|
+
* @param f - The mapping function returning an Option
|
|
97
|
+
* @returns The result of applying f to the contained value, or None if this Option is None
|
|
98
|
+
*/
|
|
99
|
+
flatMap<U extends Type>(f: (value: T) => Option<U>): Option<U>;
|
|
100
|
+
/**
|
|
101
|
+
* Maps the value using an async function that returns an Option
|
|
102
|
+
* @param f - The async mapping function returning an Option
|
|
103
|
+
* @returns Promise of the result of applying f to the contained value, or None if this Option is None
|
|
104
|
+
*/
|
|
105
|
+
flatMapAsync<U extends Type>(f: (value: T) => Promise<Option<U>>): Promise<Option<U>>;
|
|
106
|
+
/**
|
|
107
|
+
* Pattern matches over the Option, applying onNone if None and onSome if Some
|
|
108
|
+
* @param onNone - Function to apply if the Option is None
|
|
109
|
+
* @param onSome - Function to apply if the Option has a value
|
|
110
|
+
* @returns The result of applying the appropriate function
|
|
111
|
+
*/
|
|
112
|
+
fold<U>(onNone: () => U, onSome: (value: T) => U): U;
|
|
113
|
+
/**
|
|
114
|
+
* Async variant of fold. Accepts sync or async handlers on either branch and
|
|
115
|
+
* always returns a Promise.
|
|
116
|
+
*/
|
|
117
|
+
foldAsync<U>(onNone: () => U | Promise<U>, onSome: (value: T) => U | Promise<U>): Promise<U>;
|
|
118
|
+
/**
|
|
119
|
+
* Left-associative fold using the provided zero value and operation
|
|
120
|
+
* @param z - Zero/identity value
|
|
121
|
+
* @returns A function that takes an operation to apply
|
|
122
|
+
*/
|
|
123
|
+
foldLeft<B>(z: B): (op: (b: B, a: T) => B) => B;
|
|
124
|
+
/**
|
|
125
|
+
* Right-associative fold using the provided zero value and operation
|
|
126
|
+
* @param z - Zero/identity value
|
|
127
|
+
* @returns A function that takes an operation to apply
|
|
128
|
+
*/
|
|
129
|
+
foldRight<B>(z: B): (op: (a: T, b: B) => B) => B;
|
|
130
|
+
/**
|
|
131
|
+
* Checks if this Option contains the specified value
|
|
132
|
+
* @param value - The value to check for
|
|
133
|
+
* @returns true if this Option contains the value, false otherwise
|
|
134
|
+
*/
|
|
135
|
+
contains(value: T): boolean;
|
|
136
|
+
/**
|
|
137
|
+
* Tests whether the value, if there is one, satisfies the predicate. The dual of `exists`, and
|
|
138
|
+
* Scala's `forall`: `None` gives `true`, the same way `[].every(p)` does.
|
|
139
|
+
* @param p - The predicate to test the value with
|
|
140
|
+
* @returns `true` for None, otherwise `p(value)`
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* Option("abc").every((s) => s.length > 2) // true
|
|
144
|
+
* Option<string>(undefined).every((s) => s.length > 2) // true: nothing to fail
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
every(p: (a: T) => boolean): boolean;
|
|
148
|
+
/**
|
|
149
|
+
* Converts this Option to a List.
|
|
150
|
+
* @returns A List containing the value if Some, or empty List if None
|
|
151
|
+
*/
|
|
152
|
+
toList(): List<T>;
|
|
153
|
+
/**
|
|
154
|
+
* Converts this Option to a plain readonly array.
|
|
155
|
+
* @returns `[value]` if Some, `[]` if None. Symmetric with `toList()` but
|
|
156
|
+
* skips the List wrapper for code that just wants Array-prototype methods
|
|
157
|
+
* or spread into another array.
|
|
158
|
+
*/
|
|
159
|
+
toArray(): readonly T[];
|
|
160
|
+
/** The number of elements in this Option (0 or 1) */
|
|
161
|
+
size: number;
|
|
162
|
+
/**
|
|
163
|
+
* Converts this Option to an Either
|
|
164
|
+
* @param leftOrBuilder - The Left value to use if None, or a thunk producing it.
|
|
165
|
+
* Pass a thunk when the Left value is expensive to construct or carries
|
|
166
|
+
* context (e.g. `() => ConfigError(missing)`) so the cost is paid only
|
|
167
|
+
* on the None path. Eager value is fine for cheap errors.
|
|
168
|
+
* @returns Right(value) if Some, else Left(leftOrBuilder()) / Left(leftOrBuilder)
|
|
169
|
+
*/
|
|
170
|
+
toEither<E>(leftOrBuilder: E | (() => E)): Either<E, T>;
|
|
171
|
+
/**
|
|
172
|
+
* Returns a string representation of this Option
|
|
173
|
+
* @returns A string representation
|
|
174
|
+
*/
|
|
175
|
+
toString(): string;
|
|
176
|
+
/**
|
|
177
|
+
* Returns a simple object representation of this Option
|
|
178
|
+
* @returns An object with _tag and value properties
|
|
179
|
+
*/
|
|
180
|
+
toValue(): {
|
|
181
|
+
_tag: "Some" | "None";
|
|
182
|
+
value: T;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Custom JSON serialization producing the canonical `@functype`-marked
|
|
186
|
+
* envelope so native `JSON.stringify` recursion (e.g. inside a plain
|
|
187
|
+
* object) emits a round-trip-able shape.
|
|
188
|
+
*/
|
|
189
|
+
toJSON(): {
|
|
190
|
+
"@functype": "Option";
|
|
191
|
+
_tag: "Some" | "None";
|
|
192
|
+
value: T | null;
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* Pattern matches over the Option, applying a handler function based on the variant
|
|
196
|
+
* @param patterns - Object with handler functions for Some and None variants
|
|
197
|
+
* @returns The result of applying the matching handler function
|
|
198
|
+
*/
|
|
199
|
+
match<R>(patterns: {
|
|
200
|
+
Some: (value: T) => R;
|
|
201
|
+
None: () => R;
|
|
202
|
+
}): R;
|
|
203
|
+
}
|
|
204
|
+
/** An Option holding a value. Discriminated by `_tag: "Some"`. */
|
|
205
|
+
export interface SomeOf<out T extends Type> extends OptionBase<T> {
|
|
206
|
+
readonly _tag: "Some";
|
|
207
|
+
readonly value: T;
|
|
208
|
+
readonly isEmpty: false;
|
|
209
|
+
}
|
|
210
|
+
/** An Option holding nothing. Discriminated by `_tag: "None"`. */
|
|
211
|
+
export interface NoneOf<out T extends Type> extends OptionBase<T> {
|
|
212
|
+
readonly _tag: "None";
|
|
213
|
+
readonly value: undefined;
|
|
214
|
+
readonly isEmpty: true;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* A value that may or may not exist: a Some or a None. TypeScript narrows it across both branches of
|
|
218
|
+
* `isSome()` / `isNone()` and `_tag` checks. Variance comes from SomeOf and NoneOf, because a union
|
|
219
|
+
* type alias can't carry its own variance annotation.
|
|
220
|
+
* @typeParam T - The type of the value contained in the Option
|
|
221
|
+
*/
|
|
222
|
+
export type Option<T extends Type> = SomeOf<T> | NoneOf<T>;
|
|
223
|
+
/**
|
|
224
|
+
* Creates a Some variant of Option containing a value.
|
|
225
|
+
* @param value - The value to wrap in Some
|
|
226
|
+
* @returns A new Some instance containing the value
|
|
227
|
+
* @typeParam T - The type of the value
|
|
228
|
+
*/
|
|
229
|
+
export declare const Some: <T extends Type>(value: T) => Option<T>;
|
|
230
|
+
/**
|
|
231
|
+
* Creates a None variant of Option representing absence of a value.
|
|
232
|
+
* @returns A new None instance
|
|
233
|
+
* @typeParam T - The type that would be contained if this was a Some
|
|
234
|
+
*/
|
|
235
|
+
export declare const None: <T extends Type>() => Option<T>;
|
|
236
|
+
/**
|
|
237
|
+
* Safely wraps a value that might be null or undefined in an Option.
|
|
238
|
+
* Creates Some if the value is defined, None otherwise.
|
|
239
|
+
* @param value - The value to wrap (might be null/undefined)
|
|
240
|
+
* @returns Some(value) if value is defined, None otherwise
|
|
241
|
+
* @typeParam T - The type of the value
|
|
242
|
+
*/
|
|
243
|
+
export declare const OptionConstructor: <T extends Type>(value: T | null | undefined) => Option<T>;
|
|
244
|
+
declare const OptionStatics: {
|
|
245
|
+
/**
|
|
246
|
+
* Creates an Option from any value. Alias for Option function.
|
|
247
|
+
* @param value - The value to wrap
|
|
248
|
+
* @returns Some(value) if value is defined, None otherwise
|
|
249
|
+
* @typeParam T - The type of the value
|
|
250
|
+
*/
|
|
251
|
+
from: <T>(value: T) => Option<T>;
|
|
252
|
+
/**
|
|
253
|
+
* Returns a None instance. Alias for None function.
|
|
254
|
+
* @returns A None instance
|
|
255
|
+
* @typeParam T - The type that would be contained if this was a Some
|
|
256
|
+
*/
|
|
257
|
+
none: <T>() => Option<T>;
|
|
258
|
+
/**
|
|
259
|
+
* Type guard to check if an Option is Some
|
|
260
|
+
* @param option - The Option to check
|
|
261
|
+
* @returns True if Option is Some
|
|
262
|
+
*/
|
|
263
|
+
isSome: <T>(option: Option<T>) => option is SomeOf<T>;
|
|
264
|
+
/**
|
|
265
|
+
* Type guard to check if an Option is None
|
|
266
|
+
* @param option - The Option to check
|
|
267
|
+
* @returns True if Option is None
|
|
268
|
+
*/
|
|
269
|
+
isNone: <T>(option: Option<T>) => option is NoneOf<T>;
|
|
270
|
+
/**
|
|
271
|
+
* Creates an Option from JSON string
|
|
272
|
+
* @param json - The JSON string
|
|
273
|
+
* @returns Option instance
|
|
274
|
+
*/
|
|
275
|
+
fromJSON: <T>(json: string) => Option<T>;
|
|
276
|
+
/**
|
|
277
|
+
* Creates an Option from binary string
|
|
278
|
+
* @param binary - The binary string
|
|
279
|
+
* @returns Option instance
|
|
280
|
+
*/
|
|
281
|
+
fromBinary: <T>(binary: string) => Option<T>;
|
|
282
|
+
/**
|
|
283
|
+
* Combines an array of Options into a single Option containing an array.
|
|
284
|
+
* Short-circuits on the first None.
|
|
285
|
+
* @param options - Array of Option values
|
|
286
|
+
* @returns Some with array of values, or None if any element is None
|
|
287
|
+
*/
|
|
288
|
+
sequence: <T extends Type>(options: ReadonlyArray<Option<T>>) => Option<T[]>;
|
|
289
|
+
/**
|
|
290
|
+
* Maps an array through a function returning Option, then sequences the results.
|
|
291
|
+
* Short-circuits on the first None — `f` is not invoked for elements after the
|
|
292
|
+
* first None result.
|
|
293
|
+
* @param arr - Array of values
|
|
294
|
+
* @param f - Function returning Option
|
|
295
|
+
* @returns Some with array of mapped values, or None if any application returned None
|
|
296
|
+
*/
|
|
297
|
+
traverse: <T extends Type, U extends Type>(arr: ReadonlyArray<T>, f: (value: T, index: number) => Option<U>) => Option<U[]>;
|
|
298
|
+
};
|
|
299
|
+
export declare const Option: OptionCompanion;
|
|
300
|
+
/** The type of `Option`: its constructor and its statics, named so that errors and hovers print `OptionCompanion`. */
|
|
301
|
+
export interface OptionCompanion extends OptionConstructorType, OptionStaticsType {}
|
|
302
|
+
type OptionConstructorType = typeof OptionConstructor;
|
|
303
|
+
type OptionStaticsType = typeof OptionStatics;
|
|
304
|
+
export {};
|
package/dist/option/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{Companion as e}from"../companion/Companion.js";import{base64ToUtf8 as t}from"../internal/base64.js";import{define as n}from"../internal/define.js";import{showValue as r}from"../internal/stringify.js";import{createSerializer as i,expectMarker as a}from"../serialization/SerializationCompanion.js";import{Left as o,Right as s}from"../either/index.js";import{List as c}from"../list/index.js";import{Try as l}from"../try/index.js";const u=n()(`Some`,{[Symbol.toStringTag]:`Option`,isEmpty:!1,size:1,isSome(){return!0},isNone(){return!1},getOrElse(){return this.value},orThrow(){return this.value},expect(){return this.value},orElse(){return this},orNull(){return this.value},orUndefined(){return this.value},map(e){return f(e(this.value))},ap(e){return e._tag===`Some`&&e.value?f(e.value(this.value)):p},filter(e){return e(this.value)?this:p},count(e){return+!!e(this.value)},find(e){return e(this.value)?this:p},exists(e){return e(this.value)},every(e){return e(this.value)},forEach(e){e(this.value)},fold(e,t){return t(this.value)},async foldAsync(e,t){return t(this.value)},match(e){return e.Some(this.value)},flatMap(e){return e(this.value)},async flatMapAsync(e){return e(this.value)},reduce(){return this.value},reduceRight(){return this.value},foldLeft(e){return t=>t(e,this.value)},foldRight(e){return t=>t(this.value,e)},contains(e){return e===this.value},toList(){return c([this.value])},toArray(){return[this.value]},toOption(){return this},toEither(){return s(this.value)},toTry(){return l(()=>this.value)},toPromise(){return Promise.resolve(this.value)},toString(){return`Some(${r(this.value)})`},toValue(){return{_tag:`Some`,value:this.value}},toJSON(){return{"@functype":`Option`,_tag:`Some`,value:this.value}},pipe(e){return e(this.value)},serialize(){return i(`Option`,`Some`,this.value)},doUnwrap(){return{ok:!0,value:this.value}}}),d=n()(`None`,{[Symbol.toStringTag]:`Option`,isEmpty:!0,size:0,isSome(){return!1},isNone(){return!0},getOrElse(e){return e()},orThrow(e){throw e===void 0?/* @__PURE__ */ Error(`Cannot extract value from None`):typeof e==`function`?e():e},expect(e){return e()},orElse(e){return e()},orNull(){return null},orUndefined(){},map(){return p},ap(){return p},filter(){return p},count(){return 0},find(){return p},exists(){return!1},every(){return!0},forEach(){},fold(e){return e()},async foldAsync(e){return e()},match(e){return e.None()},flatMap(){return p},flatMapAsync(){return Promise.resolve(p)},reduce(){throw Error(`Cannot reduce an empty Option`)},reduceRight(){throw Error(`Cannot reduceRight an empty Option`)},foldLeft(e){return()=>e},foldRight(e){return()=>e},contains(){return!1},toList(){return c([])},toArray(){return[]},toOption(){return p},toEither(e){return o(typeof e==`function`?e():e)},toTry(){return l(()=>{throw Error(`None`)})},toPromise(){return Promise.reject(/* @__PURE__ */ Error(`Cannot convert None to Promise`))},toString(){return`None`},toValue(){return{_tag:`None`,value:void 0}},toJSON(){return{"@functype":`Option`,_tag:`None`,value:null}},pipe(e){return e(void 0)},serialize(){return i(`Option`,`None`,null)},doUnwrap(){return{ok:!1,empty:!0}}}),f=e=>{let t=Object.create(u);return t._tag=`Some`,t.value=e,t},p=(()=>{let e=Object.create(d);return e._tag=`None`,e.value=void 0,e})(),m=()=>p,h=e=>e==null?m():f(e),g=(e,t)=>(a(e,`Option`,t),e._tag===`Some`?f(e.value):m()),_={from:e=>v(e),none:()=>m(),isSome:e=>e.isSome(),isNone:e=>e.isNone(),fromJSON:e=>g(JSON.parse(e),`fromJSON`),fromBinary:e=>{let n=t(e);return _.fromJSON(n)},sequence:e=>e.reduce((e,t)=>e.isEmpty||t.isEmpty?m():f([...e.value,t.value]),f([])),traverse:(e,t)=>e.reduce((e,n,r)=>e.flatMap(e=>t(n,r).map(t=>[...e,t])),f([]))},v=e(h,_);export{m as None,v as Option,h as OptionConstructor,f as Some};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=`2.0.0`;export{e as version};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Type } from "../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Pipe interface for functional data structures
|
|
4
|
+
* @typeParam T - The type of value to pipe
|
|
5
|
+
*/
|
|
6
|
+
export interface Pipe<out T extends Type> {
|
|
7
|
+
/**
|
|
8
|
+
* Pipes the value through the provided function
|
|
9
|
+
* @param f - The function to apply to the value
|
|
10
|
+
* @returns The result of applying the function to the value
|
|
11
|
+
* @typeParam U - The return type of the function
|
|
12
|
+
*/
|
|
13
|
+
pipe<U extends Type>(f: (value: T) => U): U;
|
|
14
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Type } from "../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* A mutable reference container that holds a value of type A.
|
|
4
|
+
* This provides controlled mutability in a functional context.
|
|
5
|
+
*
|
|
6
|
+
* **Variance: invariant in A by design.** Ref is a mutable cell — `set(A)`
|
|
7
|
+
* writes A (contravariant) and `get(): A` reads A (covariant), so A is
|
|
8
|
+
* genuinely invariant. Unlike the other containers in functype, Ref has no
|
|
9
|
+
* `<out A>` annotation and cannot be widened via subtyping. This mirrors
|
|
10
|
+
* Scala's `scala.concurrent.stm.Ref` and reflects the fundamental nature of
|
|
11
|
+
* mutable state: you can't safely treat a `Ref[Narrow]` as a `Ref[Wide]`
|
|
12
|
+
* because someone could `set` a Wide value that isn't a Narrow.
|
|
13
|
+
*
|
|
14
|
+
* Its methods are shared, so call them on the Ref: `const { get } = ref` loses `this`, and the
|
|
15
|
+
* detached call throws a `TypeError`. Pass `() => ref.get()` instead.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const counter = Ref(0)
|
|
19
|
+
* counter.get() // 0
|
|
20
|
+
* counter.set(5)
|
|
21
|
+
* counter.get() // 5
|
|
22
|
+
* counter.update(n => n + 1)
|
|
23
|
+
* counter.get() // 6
|
|
24
|
+
*/
|
|
25
|
+
export interface Ref<A> {
|
|
26
|
+
readonly _tag: "Ref";
|
|
27
|
+
/**
|
|
28
|
+
* Get the current value
|
|
29
|
+
*/
|
|
30
|
+
get(): A;
|
|
31
|
+
/**
|
|
32
|
+
* Set a new value
|
|
33
|
+
*/
|
|
34
|
+
set(value: A): void;
|
|
35
|
+
/**
|
|
36
|
+
* Update the value using a function
|
|
37
|
+
*/
|
|
38
|
+
update(f: (current: A) => A): void;
|
|
39
|
+
/**
|
|
40
|
+
* Update and return the old value
|
|
41
|
+
*/
|
|
42
|
+
getAndSet(value: A): A;
|
|
43
|
+
/**
|
|
44
|
+
* Update and return the new value
|
|
45
|
+
*/
|
|
46
|
+
updateAndGet(f: (current: A) => A): A;
|
|
47
|
+
/**
|
|
48
|
+
* Update and return the old value
|
|
49
|
+
*/
|
|
50
|
+
getAndUpdate(f: (current: A) => A): A;
|
|
51
|
+
/**
|
|
52
|
+
* Compare and swap - only updates if current value equals expected
|
|
53
|
+
*/
|
|
54
|
+
compareAndSet(expected: A, newValue: A): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Modify the value and return a result
|
|
57
|
+
*/
|
|
58
|
+
modify<B>(f: (current: A) => [A, B]): B;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates a new mutable reference containing the given value
|
|
62
|
+
*/
|
|
63
|
+
declare const RefConstructor: <A extends Type>(initial: A) => Ref<A>;
|
|
64
|
+
declare const RefStatics: {
|
|
65
|
+
/**
|
|
66
|
+
* Creates a Ref. Alias for Ref constructor.
|
|
67
|
+
* @param initial - The initial value
|
|
68
|
+
* @returns Ref instance
|
|
69
|
+
*/
|
|
70
|
+
of: <A extends Type>(initial: A) => Ref<A>;
|
|
71
|
+
};
|
|
72
|
+
export declare const Ref: RefCompanion;
|
|
73
|
+
/** The type of `Ref`: its constructor and its statics, named so that errors and hovers print `RefCompanion`. */
|
|
74
|
+
export interface RefCompanion extends RefConstructorType, RefStaticsType {}
|
|
75
|
+
type RefConstructorType = typeof RefConstructor;
|
|
76
|
+
type RefStaticsType = typeof RefStatics;
|
|
77
|
+
export {};
|
package/dist/ref/Ref.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Companion as e}from"../companion/Companion.js";import{define as t}from"../internal/define.js";const n=t()(`Ref`,{get(){return this.value},set(e){this.value=e},update(e){this.value=e(this.value)},getAndSet(e){let t=this.value;return this.value=e,t},updateAndGet(e){return this.value=e(this.value),this.value},getAndUpdate(e){let t=this.value;return this.value=e(this.value),t},compareAndSet(e,t){return this.value===e&&(this.value=t,!0)},modify(e){let[t,n]=e(this.value);return this.value=t,n}}),r=e=>{let t=Object.create(n);return t._tag=`Ref`,t.value=e,t},i=e(r,{of:e=>r(e)});export{i as Ref};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Either } from "../either/index.js";
|
|
2
|
+
import { List } from "../list/index.js";
|
|
3
|
+
import { Option } from "../option/index.js";
|
|
4
|
+
import { Try } from "../try/index.js";
|
|
5
|
+
import { Type } from "../types/index.js";
|
|
6
|
+
/**
|
|
7
|
+
* Interface for types that can be reshaped (converted) between different monadic containers.
|
|
8
|
+
* Provides standard conversion methods to transform between Option, Either, List, and Try types.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T - The type of the value contained in the monad
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Convert Option to Either
|
|
14
|
+
* const opt = Option(5)
|
|
15
|
+
* const either = opt.toEither("None value") // Right(5)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Convert Either to Option
|
|
19
|
+
* const right = Right(10)
|
|
20
|
+
* const option = right.toOption() // Some(10)
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Convert List to Try
|
|
24
|
+
* const list = List([1, 2, 3])
|
|
25
|
+
* const tryVal = list.toTry() // Success(1) - uses first element
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // Use with Do comprehensions
|
|
29
|
+
* const result = Do(function* () {
|
|
30
|
+
* const x = yield* $(Option(5))
|
|
31
|
+
* const y = yield* $(Right<string, number>(10))
|
|
32
|
+
* return x + y
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* // Convert to desired type for chaining
|
|
36
|
+
* const asOption = result.toOption()
|
|
37
|
+
* asOption.map(x => x * 2).getOrElse(() => 0)
|
|
38
|
+
*/
|
|
39
|
+
export interface Reshapeable<out T extends Type> {
|
|
40
|
+
/**
|
|
41
|
+
* Converts this monad to an Option.
|
|
42
|
+
*
|
|
43
|
+
* Conversion rules:
|
|
44
|
+
* - Option: returns self
|
|
45
|
+
* - Either: Right → Some, Left → None
|
|
46
|
+
* - List: non-empty → Some(head), empty → None
|
|
47
|
+
* - Try: Success → Some, Failure → None
|
|
48
|
+
*
|
|
49
|
+
* @returns An Option containing the value if present, None otherwise
|
|
50
|
+
*/
|
|
51
|
+
toOption(): Option<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Converts this monad to an Either.
|
|
54
|
+
*
|
|
55
|
+
* Conversion rules:
|
|
56
|
+
* - Option: Some → Right, None → Left(leftValue)
|
|
57
|
+
* - Either: returns self
|
|
58
|
+
* - List: non-empty → Right(head), empty → Left(leftValue)
|
|
59
|
+
* - Try: Success → Right, Failure → Left(leftValue). Try also accepts a builder
|
|
60
|
+
* `(err: Error) => E` to thread the underlying Error's context into the Left.
|
|
61
|
+
*
|
|
62
|
+
* @param leftValue - The value to use for the Left case when the source is empty/none/failure
|
|
63
|
+
* @returns An Either with the value as Right or the provided leftValue as Left
|
|
64
|
+
*/
|
|
65
|
+
toEither<E extends Type>(leftValue: E): Either<E, T>;
|
|
66
|
+
/**
|
|
67
|
+
* Converts this monad to a List.
|
|
68
|
+
*
|
|
69
|
+
* Conversion rules:
|
|
70
|
+
* - Option: Some → List([value]), None → List([])
|
|
71
|
+
* - Either: Right → List([value]), Left → List([])
|
|
72
|
+
* - List: returns self
|
|
73
|
+
* - Try: Success → List([value]), Failure → List([])
|
|
74
|
+
*
|
|
75
|
+
* @returns A List containing the value(s) if present, empty List otherwise
|
|
76
|
+
*/
|
|
77
|
+
toList(): List<T>;
|
|
78
|
+
/**
|
|
79
|
+
* Converts this monad to a Try.
|
|
80
|
+
*
|
|
81
|
+
* Conversion rules:
|
|
82
|
+
* - Option: Some → Success, None → Failure(Error("None"))
|
|
83
|
+
* - Either: Right → Success, Left → Failure(Error(leftValue))
|
|
84
|
+
* - List: non-empty → Success(head), empty → Failure(Error("Empty list"))
|
|
85
|
+
* - Try: returns self
|
|
86
|
+
*
|
|
87
|
+
* @returns A Try containing Success with the value or Failure with an appropriate error
|
|
88
|
+
*/
|
|
89
|
+
toTry(): Try<T>;
|
|
90
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Methods for different serialization formats
|
|
3
|
+
*/
|
|
4
|
+
export interface SerializationMethods<T> {
|
|
5
|
+
toJSON(): string;
|
|
6
|
+
toBinary(): string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Shape of the envelope object emitted by a Serializable's instance-level
|
|
10
|
+
* `toJSON()` (the method native `JSON.stringify` picks up via protocol).
|
|
11
|
+
* Concrete types narrow this with their specific marker/tag literals.
|
|
12
|
+
*
|
|
13
|
+
* The `@functype` marker is the type discriminator (defends against
|
|
14
|
+
* `_tag`-collision with Effect/fp-ts). `_tag` is the variant discriminator
|
|
15
|
+
* within that type — kept across the board (variant-less types repeat the
|
|
16
|
+
* marker for back-compat with 1.1.0 readers that did `parsed._tag === "List"`).
|
|
17
|
+
*
|
|
18
|
+
* Payload shape is type-specific: canonical types carry `value`; failure
|
|
19
|
+
* branches (`Try.Failure`, `Lazy` with thrown thunk) carry
|
|
20
|
+
* `error: SerializedError` instead.
|
|
21
|
+
*/
|
|
22
|
+
export interface SerializableEnvelope {
|
|
23
|
+
readonly "@functype": string;
|
|
24
|
+
readonly _tag: string;
|
|
25
|
+
readonly [key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
export interface Serializable<out T> {
|
|
28
|
+
serialize(): SerializationMethods<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Custom JSON serialization. Returns the canonical `@functype`-marked
|
|
31
|
+
* envelope object so native `JSON.stringify` (and the rest of the JSON
|
|
32
|
+
* `toJSON` protocol) emits a round-trip-able shape that
|
|
33
|
+
* `Serialization.deserialize` can reconstruct.
|
|
34
|
+
*
|
|
35
|
+
* Added in 1.2.0. Concrete types narrow the return to their specific
|
|
36
|
+
* marker/tag literals.
|
|
37
|
+
*/
|
|
38
|
+
toJSON(): SerializableEnvelope;
|
|
39
|
+
}
|