ok-fp 0.2.2 → 0.3.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 +6 -6
- package/dist/index.d.mts +1328 -0
- package/dist/index.mjs +636 -0
- package/package.json +8 -21
- package/dist/constructors-pI0lvrQE.mjs +0 -142
- package/dist/either-CWllQk7N.d.mts +0 -213
- package/dist/either.d.mts +0 -3
- package/dist/either.mjs +0 -65
- package/dist/helpers-Bw8McftR.d.mts +0 -455
- package/dist/option.d.mts +0 -2
- package/dist/option.mjs +0 -177
- package/dist/task-D9kWv4yq.d.mts +0 -100
- package/dist/task-DQTXV7xv.mjs +0 -23
- package/dist/task.d.mts +0 -49
- package/dist/task.mjs +0 -57
- package/dist/taskEither.d.mts +0 -212
- package/dist/taskEither.mjs +0 -80
- package/dist/validation.d.mts +0 -233
- package/dist/validation.mjs +0 -165
|
@@ -1,455 +0,0 @@
|
|
|
1
|
-
import { t as Either } from "./either-CWllQk7N.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/option/model.d.ts
|
|
4
|
-
type Some<T> = {
|
|
5
|
-
some: T;
|
|
6
|
-
};
|
|
7
|
-
type None = symbol;
|
|
8
|
-
type OptionValue<T> = Some<T> | None;
|
|
9
|
-
declare const NONE: unique symbol;
|
|
10
|
-
//#endregion
|
|
11
|
-
//#region src/option/option.d.ts
|
|
12
|
-
type Option<T> = {
|
|
13
|
-
/**
|
|
14
|
-
* Filters the Option based on a predicate function.
|
|
15
|
-
* If this Option is Some and the predicate returns true, returns this Option.
|
|
16
|
-
* Otherwise, returns None.
|
|
17
|
-
*
|
|
18
|
-
* @param predicate - Function that tests the contained value
|
|
19
|
-
* @returns The same Option if predicate passes, None otherwise
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```typescript
|
|
23
|
-
* some(5).filter(x => x > 3) // Some(5)
|
|
24
|
-
* some(2).filter(x => x > 3) // None
|
|
25
|
-
* none().filter(x => x > 3) // None
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
filter: (predicate: (value: T) => boolean) => Option<T>;
|
|
29
|
-
/**
|
|
30
|
-
* Transforms the value inside the Option using a mapping function.
|
|
31
|
-
* If this Option is None, returns None without calling the mapper.
|
|
32
|
-
*
|
|
33
|
-
* @param mapper - Function to transform the contained value
|
|
34
|
-
* @returns New Option containing the transformed value, or None
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```typescript
|
|
38
|
-
* some(5).map(x => x * 2) // Some(10)
|
|
39
|
-
* none<number>().map(x => x * 2) // None
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
map: <U>(mapper: (value: T) => U) => Option<U>;
|
|
43
|
-
/**
|
|
44
|
-
* Returns this Option if it contains a value, otherwise returns the fallback Option.
|
|
45
|
-
* The fallback is lazily evaluated to avoid unnecessary computation.
|
|
46
|
-
*
|
|
47
|
-
* @param fallback - Function that provides the alternative Option
|
|
48
|
-
* @returns This Option if Some, otherwise the fallback Option
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```typescript
|
|
52
|
-
* some(5).orElse(() => some(10)) // Some(5)
|
|
53
|
-
* none().orElse(() => some(10)) // Some(10)
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
orElse: (fallback: () => Option<T>) => Option<T>;
|
|
57
|
-
/**
|
|
58
|
-
* Applies a function wrapped in an Option to a value wrapped in an Option.
|
|
59
|
-
* This is useful for applying functions that are also optional.
|
|
60
|
-
*
|
|
61
|
-
* @param optA - Option containing the argument to apply the function to
|
|
62
|
-
* @returns Option containing the result, or None if either Option is None
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```typescript
|
|
66
|
-
* const add = (x: number) => (y: number) => x + y;
|
|
67
|
-
* some(add(5)).ap(some(3)) // Some(8)
|
|
68
|
-
* some(add(5)).ap(none()) // None
|
|
69
|
-
* none().ap(some(3)) // None
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
ap: <A, U>(this: Option<(arg: A) => U>, optA: Option<A>) => Option<U>;
|
|
73
|
-
/**
|
|
74
|
-
* Combines this Option with another Option into a tuple.
|
|
75
|
-
* If both Options contain values, returns Some containing a tuple of both values.
|
|
76
|
-
* If either Option is None, returns None.
|
|
77
|
-
*
|
|
78
|
-
* @param optA - The Option to combine with this one
|
|
79
|
-
* @returns Option containing a tuple of both values, or None if either is None
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```typescript
|
|
83
|
-
* const name = some("Alice");
|
|
84
|
-
* const age = some(30);
|
|
85
|
-
* name.zip(age) // Some(["Alice", 30])
|
|
86
|
-
*
|
|
87
|
-
* const missing = none<number>();
|
|
88
|
-
* name.zip(missing) // None
|
|
89
|
-
* none().zip(age) // None
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
zip: <A>(optA: Option<A>) => Option<readonly [T, A]>;
|
|
93
|
-
/**
|
|
94
|
-
* Flattens a nested Option structure by removing one level of nesting.
|
|
95
|
-
* If this Option contains another Option, returns the inner Option.
|
|
96
|
-
* If this Option is None, returns None.
|
|
97
|
-
*
|
|
98
|
-
* @returns The inner Option if this Option contains one, or None
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```typescript
|
|
102
|
-
* some(some(5)).flatten() // Some(5)
|
|
103
|
-
* some(none<number>()).flatten() // None
|
|
104
|
-
* ```
|
|
105
|
-
*/
|
|
106
|
-
flatten: <U>(this: Option<Option<U>>) => Option<U>;
|
|
107
|
-
/**
|
|
108
|
-
* Chains Option-returning operations together (monadic bind).
|
|
109
|
-
* If this Option is Some, applies the mapper and returns the result.
|
|
110
|
-
* If this Option is None, returns None without calling the mapper.
|
|
111
|
-
*
|
|
112
|
-
* @param mapper - Function that returns an Option
|
|
113
|
-
* @returns The Option returned by mapper, or None
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```typescript
|
|
117
|
-
* const safeDivide = (x: number) => x === 0 ? none() : some(10 / x);
|
|
118
|
-
* some(2).flatMap(safeDivide) // Some(5)
|
|
119
|
-
* some(0).flatMap(safeDivide) // None
|
|
120
|
-
* none().flatMap(safeDivide) // None
|
|
121
|
-
* ```
|
|
122
|
-
*/
|
|
123
|
-
flatMap: <U>(mapper: (value: T) => Option<U>) => Option<U>;
|
|
124
|
-
/**
|
|
125
|
-
* Performs a side effect if this Option contains a value.
|
|
126
|
-
* Returns the original Option unchanged.
|
|
127
|
-
*
|
|
128
|
-
* @returns The same Option instance
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* ```typescript
|
|
132
|
-
* some(5).tap((value) => console.log("Has value", value)) // Some(5), logs message
|
|
133
|
-
* none().tap((value) => console.log("Has value", value)) // None, no log
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
tap: (sideEffect: (value: T) => unknown) => Option<T>;
|
|
137
|
-
/**
|
|
138
|
-
* Performs a side effect if this Option is None.
|
|
139
|
-
* Returns the original Option unchanged.
|
|
140
|
-
*
|
|
141
|
-
* @returns The same Option instance
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* ```typescript
|
|
145
|
-
* some(5).tapNone(() => console.log("No value")) // Some(5), no log
|
|
146
|
-
* none().tapNone(() => console.log("No value")) // None, logs message
|
|
147
|
-
* ```
|
|
148
|
-
*/
|
|
149
|
-
tapNone: (sideEffect: () => unknown) => Option<T>;
|
|
150
|
-
/**
|
|
151
|
-
* Pattern matches on the Option, executing different functions based on its state.
|
|
152
|
-
*
|
|
153
|
-
* @param onNone - Function to execute if Option is None
|
|
154
|
-
* @param onSome - Function to execute if Option is Some
|
|
155
|
-
* @returns The result of the executed function
|
|
156
|
-
*
|
|
157
|
-
* @example
|
|
158
|
-
* ```typescript
|
|
159
|
-
* some(5).match(() => "empty", x => `value: ${x}`) // "value: 5"
|
|
160
|
-
* none().match(() => "empty", x => `value: ${x}`) // "empty"
|
|
161
|
-
* ```
|
|
162
|
-
*/
|
|
163
|
-
match: <U>(onNone: () => U, onSome: (value: T) => U) => U;
|
|
164
|
-
/**
|
|
165
|
-
* Extracts the value from the Option, or returns a fallback value if None.
|
|
166
|
-
*
|
|
167
|
-
* @param fallback - Function that provides the default value
|
|
168
|
-
* @returns The value or the fallback value
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ```typescript
|
|
172
|
-
* some(5).getOrElse(() => 0) // 5
|
|
173
|
-
* none().getOrElse(() => 0) // 0
|
|
174
|
-
* ```
|
|
175
|
-
*/
|
|
176
|
-
getOrElse: (fallback: () => T) => T;
|
|
177
|
-
/**
|
|
178
|
-
* Converts the Option to a nullable value.
|
|
179
|
-
* Returns the contained value if Some, or null if None.
|
|
180
|
-
*
|
|
181
|
-
* @returns The value or null
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* ```typescript
|
|
185
|
-
* some(5).toNullable() // 5
|
|
186
|
-
* none().toNullable() // null
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
toNullable: () => T | null;
|
|
190
|
-
/**
|
|
191
|
-
* Converts the Option to an array.
|
|
192
|
-
* If this Option contains a value, returns an array with that single value.
|
|
193
|
-
* If this Option is None, returns an empty array.
|
|
194
|
-
*
|
|
195
|
-
* @returns Array containing the value if Some, or empty array if None
|
|
196
|
-
*
|
|
197
|
-
* @example
|
|
198
|
-
* ```typescript
|
|
199
|
-
* some(42).toArray() // [42]
|
|
200
|
-
* none().toArray() // []
|
|
201
|
-
* ```
|
|
202
|
-
*/
|
|
203
|
-
toArray: () => readonly T[];
|
|
204
|
-
};
|
|
205
|
-
//#endregion
|
|
206
|
-
//#region src/option/constructors.d.ts
|
|
207
|
-
/**
|
|
208
|
-
* Creates an {@link Option} that contains a value.
|
|
209
|
-
*
|
|
210
|
-
* Use this to wrap an existing value into an `Option<T>` representing presence
|
|
211
|
-
* (as opposed to `none`, which represents absence).
|
|
212
|
-
*
|
|
213
|
-
* @typeParam T - Type of the wrapped value.
|
|
214
|
-
* @param some - The value to store in the option.
|
|
215
|
-
* @returns An {@link Option} containing the provided value.
|
|
216
|
-
*/
|
|
217
|
-
declare function some<T>(some: T): Option<T>;
|
|
218
|
-
/**
|
|
219
|
-
* Creates an {@link Option} representing the absence of a value.
|
|
220
|
-
*
|
|
221
|
-
* @typeParam T - The type of the value that would be contained if present.
|
|
222
|
-
* @returns An {@link Option} in the `None` state.
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* ```ts
|
|
226
|
-
* const value = none<number>();
|
|
227
|
-
* ```
|
|
228
|
-
*/
|
|
229
|
-
declare function none<T>(): Option<T>;
|
|
230
|
-
/**
|
|
231
|
-
* Creates an {@link Option} from a nullable value.
|
|
232
|
-
*
|
|
233
|
-
* If {@link nullable} is `null` or `undefined`, returns {@link none}.
|
|
234
|
-
* Otherwise, wraps the provided value in {@link some}.
|
|
235
|
-
*
|
|
236
|
-
* @example
|
|
237
|
-
* fromNullable(0).getOrElse(() => 123); // 0
|
|
238
|
-
* fromNullable(null).toNullable(); // null
|
|
239
|
-
*
|
|
240
|
-
* @typeParam T - The non-null value type to wrap.
|
|
241
|
-
* @param nullable - A value that may be `null` or `undefined`.
|
|
242
|
-
* @returns An {@link Option} that is `Some` when the value is present, otherwise `None`.
|
|
243
|
-
*/
|
|
244
|
-
declare function fromNullable$1<T>(nullable: null | undefined | T): Option<T>;
|
|
245
|
-
/**
|
|
246
|
-
* Creates an {@link Option} from an {@link Either} by extracting the Right value.
|
|
247
|
-
*
|
|
248
|
-
* If the Either is Right, returns Some containing the Right value.
|
|
249
|
-
* If the Either is Left, returns None (discarding the Left value).
|
|
250
|
-
*
|
|
251
|
-
* @typeParam L - The type of the Left value (error type) that will be discarded
|
|
252
|
-
* @typeParam R - The type of the Right value that will be preserved in the Option
|
|
253
|
-
* @param either - The Either to convert to an Option
|
|
254
|
-
* @returns Some containing the Right value, or None if the Either was Left
|
|
255
|
-
*
|
|
256
|
-
* @example
|
|
257
|
-
* ```typescript
|
|
258
|
-
* const success = right(42);
|
|
259
|
-
* fromEither(success) // Some(42)
|
|
260
|
-
*
|
|
261
|
-
* const failure = left("error message");
|
|
262
|
-
* fromEither(failure) // None
|
|
263
|
-
* ```
|
|
264
|
-
*/
|
|
265
|
-
declare function fromEither<L, R>(either: Either<L, R>): Option<R>;
|
|
266
|
-
//#endregion
|
|
267
|
-
//#region src/option/helpers.d.ts
|
|
268
|
-
/**
|
|
269
|
-
* Combines two Options using a mapping function.
|
|
270
|
-
* Only applies the mapper if both Options contain values.
|
|
271
|
-
*
|
|
272
|
-
* @param optA - First Option to combine
|
|
273
|
-
* @param optB - Second Option to combine
|
|
274
|
-
* @param mapper - Function that combines both values
|
|
275
|
-
* @returns Option containing the combined result, or None if either Option is None
|
|
276
|
-
*
|
|
277
|
-
* @example
|
|
278
|
-
* ```typescript
|
|
279
|
-
* const greet = (first, last) => `Hi ${first} ${last}!`
|
|
280
|
-
* map2(some("John"), some("Doe"), greet) // Some("John Doe")
|
|
281
|
-
* map2(some("John"), none<string>(), greet) // None
|
|
282
|
-
* ```
|
|
283
|
-
*/
|
|
284
|
-
declare function map2$1<A, B, C>(optA: Option<A>, optB: Option<B>, mapper: (a: A, b: B) => C): Option<C>;
|
|
285
|
-
/**
|
|
286
|
-
* Combines three Options using a mapping function.
|
|
287
|
-
* Only applies the mapper if all three Options contain values.
|
|
288
|
-
*
|
|
289
|
-
* @param optA - First Option to combine
|
|
290
|
-
* @param optB - Second Option to combine
|
|
291
|
-
* @param optC - Third Option to combine
|
|
292
|
-
* @param mapper - Function that combines all three values
|
|
293
|
-
* @returns Option containing the combined result, or None if any Option is None
|
|
294
|
-
*
|
|
295
|
-
* @example
|
|
296
|
-
* ```typescript
|
|
297
|
-
* const toDateString = (d, m, y) => `${d}/${m}/${y}`
|
|
298
|
-
* map3(some(15), some(6), some(2023), toDateString) // Some("15/6/2023")
|
|
299
|
-
* map3(some(15), some(6), none<number>(), toDateString) // None
|
|
300
|
-
* ```
|
|
301
|
-
*/
|
|
302
|
-
declare function map3$1<A, B, C, D>(optA: Option<A>, optB: Option<B>, optC: Option<C>, mapper: (a: A, b: B, c: C) => D): Option<D>;
|
|
303
|
-
/**
|
|
304
|
-
* Converts an array of Options into an Option of array.
|
|
305
|
-
* If all Options in the array contain values, returns Some containing an array of all values.
|
|
306
|
-
* If any Option in the array is None, returns None.
|
|
307
|
-
*
|
|
308
|
-
* @param opts - Array of Options to sequence
|
|
309
|
-
* @returns Some containing array of all values if all Options are Some, otherwise None
|
|
310
|
-
*
|
|
311
|
-
* @example
|
|
312
|
-
* ```typescript
|
|
313
|
-
* sequence([some(1), some(2), some(3)]) // Some([1, 2, 3])
|
|
314
|
-
* sequence([some(1), none(), some(3)]) // None
|
|
315
|
-
* ```
|
|
316
|
-
*/
|
|
317
|
-
declare function sequence$1<T>(opts: Option<T>[]): Option<T[]>;
|
|
318
|
-
//#endregion
|
|
319
|
-
//#region src/either/constructors.d.ts
|
|
320
|
-
/**
|
|
321
|
-
* Creates an Either containing an error value (Left).
|
|
322
|
-
*
|
|
323
|
-
* @typeParam E - The type of the error value
|
|
324
|
-
* @typeParam T - The type of the success value (defaults to never since this is an error)
|
|
325
|
-
* @param left - The error value to wrap
|
|
326
|
-
* @returns Either containing the error value on the Left side
|
|
327
|
-
*
|
|
328
|
-
* @example
|
|
329
|
-
* ```typescript
|
|
330
|
-
* const error = left("Something went wrong"); // Either<string, never>
|
|
331
|
-
* ```
|
|
332
|
-
*/
|
|
333
|
-
declare function left<E, T = never>(left: E): Either<E, T>;
|
|
334
|
-
/**
|
|
335
|
-
* Creates an Either containing a success value (Right).
|
|
336
|
-
*
|
|
337
|
-
* @typeParam T - The type of the success value
|
|
338
|
-
* @typeParam E - The type of the error value (defaults to never since this is a success)
|
|
339
|
-
* @param right - The success value to wrap
|
|
340
|
-
* @returns Either containing the success value on the Right side
|
|
341
|
-
*
|
|
342
|
-
* @example
|
|
343
|
-
* ```typescript
|
|
344
|
-
* const success = right(42); // Either<never, number>
|
|
345
|
-
* ```
|
|
346
|
-
*/
|
|
347
|
-
declare function right<T, E = never>(right: T): Either<E, T>;
|
|
348
|
-
/**
|
|
349
|
-
* Creates an Either from a potentially nullable value.
|
|
350
|
-
* If the value is null or undefined, creates a Left with the provided error.
|
|
351
|
-
* If the value is present, creates a Right with the value.
|
|
352
|
-
*
|
|
353
|
-
* @typeParam E - The type of the error value
|
|
354
|
-
* @typeParam T - The type of the success value
|
|
355
|
-
* @param nullable - The potentially null/undefined value
|
|
356
|
-
* @param onNullish - Function that provides the error value when nullable is null/undefined
|
|
357
|
-
* @returns Either containing the value if present, or the error if null/undefined
|
|
358
|
-
*
|
|
359
|
-
* @example
|
|
360
|
-
* ```typescript
|
|
361
|
-
* fromNullable("hello", () => "empty") // Right("hello")
|
|
362
|
-
* fromNullable(null, () => "empty") // Left("empty")
|
|
363
|
-
* fromNullable(undefined, () => "empty") // Left("empty")
|
|
364
|
-
* ```
|
|
365
|
-
*/
|
|
366
|
-
declare function fromNullable<E, T>(nullable: T | null | undefined, onNullish: () => E): Either<E, T>;
|
|
367
|
-
/**
|
|
368
|
-
* Safely executes a function that might throw, converting exceptions to Either.
|
|
369
|
-
*
|
|
370
|
-
* @typeParam E - The type of the processed error value
|
|
371
|
-
* @typeParam T - The type of the success value
|
|
372
|
-
* @param fn - The function to execute that might throw
|
|
373
|
-
* @param onThrow - Function that processes the caught error into the desired error type
|
|
374
|
-
* @returns Either containing the result if successful, or the processed error if thrown
|
|
375
|
-
*
|
|
376
|
-
* @example
|
|
377
|
-
* ```typescript
|
|
378
|
-
* const parseNumber = (str: string) =>
|
|
379
|
-
* tryCatch(
|
|
380
|
-
* () => JSON.parse(str),
|
|
381
|
-
* (err) => `Parse error: ${err.message}`
|
|
382
|
-
* );
|
|
383
|
-
*
|
|
384
|
-
* parseNumber("42") // Right(42)
|
|
385
|
-
* parseNumber("invalid") // Left("Parse error: ...")
|
|
386
|
-
* ```
|
|
387
|
-
*/
|
|
388
|
-
declare function tryCatch<E, T>(fn: () => T, onThrow: (err: unknown) => E): Either<E, T>;
|
|
389
|
-
/**
|
|
390
|
-
* Creates an Either from an Option by providing an error value for the None case.
|
|
391
|
-
*
|
|
392
|
-
* @typeParam E - The type of the error value
|
|
393
|
-
* @typeParam T - The type of the success value
|
|
394
|
-
* @param opt - The Option to convert
|
|
395
|
-
* @param onNone - Function that provides the error value when Option is None
|
|
396
|
-
* @returns Either containing the Option's value if Some, or the error if None
|
|
397
|
-
*
|
|
398
|
-
* @example
|
|
399
|
-
* ```typescript
|
|
400
|
-
* fromOption(some(42), () => "empty") // Right(42)
|
|
401
|
-
* fromOption(none(), () => "empty") // Left("empty")
|
|
402
|
-
* ```
|
|
403
|
-
*/
|
|
404
|
-
declare function fromOption<E, T>(opt: Option<T>, onNone: () => E): Either<E, T>;
|
|
405
|
-
//#endregion
|
|
406
|
-
//#region src/either/helpers.d.ts
|
|
407
|
-
/**
|
|
408
|
-
* Combines two Eithers using a mapping function.
|
|
409
|
-
* Returns the mapped result if both Eithers are Right, otherwise the first Left.
|
|
410
|
-
*
|
|
411
|
-
* @param eitherA - First Either to combine
|
|
412
|
-
* @param eitherB - Second Either to combine
|
|
413
|
-
* @param mapper - Function to combine both Right values
|
|
414
|
-
* @returns Either containing the mapped result, or the first Left
|
|
415
|
-
*
|
|
416
|
-
* @example
|
|
417
|
-
* ```typescript
|
|
418
|
-
* map2(right("John"), right("Doe"), (first, last) => `${first} ${last}`)
|
|
419
|
-
* // Right("John Doe")
|
|
420
|
-
* ```
|
|
421
|
-
*/
|
|
422
|
-
declare function map2<EA, A, EB, B, C>(eitherA: Either<EA, A>, eitherB: Either<EB, B>, mapper: (a: A, b: B) => C): Either<EA | EB, C>;
|
|
423
|
-
/**
|
|
424
|
-
* Combines three Eithers using a mapping function.
|
|
425
|
-
* Returns the mapped result if all Eithers are Right, otherwise the first Left.
|
|
426
|
-
*
|
|
427
|
-
* @param eitherA - First Either to combine
|
|
428
|
-
* @param eitherB - Second Either to combine
|
|
429
|
-
* @param eitherC - Third Either to combine
|
|
430
|
-
* @param mapper - Function to combine all three Right values
|
|
431
|
-
* @returns Either containing the mapped result, or the first Left
|
|
432
|
-
*
|
|
433
|
-
* @example
|
|
434
|
-
* ```typescript
|
|
435
|
-
* map3(right(1), right(2), right(3), (a, b, c) => a + b + c)
|
|
436
|
-
* // Right(6)
|
|
437
|
-
* ```
|
|
438
|
-
*/
|
|
439
|
-
declare function map3<EA, A, EB, B, EC, C, D>(eitherA: Either<EA, A>, eitherB: Either<EB, B>, eitherC: Either<EC, C>, mapper: (a: A, b: B, c: C) => D): Either<EA | EB | EC, D>;
|
|
440
|
-
/**
|
|
441
|
-
* Converts an array of Eithers into an Either of array.
|
|
442
|
-
* Returns Right with all values if all Eithers are Right, otherwise the first Left.
|
|
443
|
-
*
|
|
444
|
-
* @param eithers - Array of Eithers to sequence
|
|
445
|
-
* @returns Either containing array of all values, or the first Left
|
|
446
|
-
*
|
|
447
|
-
* @example
|
|
448
|
-
* ```typescript
|
|
449
|
-
* sequence([right(1), right(2), right(3)]) // Right([1, 2, 3])
|
|
450
|
-
* sequence([right(1), left("error")]) // Left("error")
|
|
451
|
-
* ```
|
|
452
|
-
*/
|
|
453
|
-
declare function sequence<E, T>(eithers: Either<E, T>[]): Either<E, T[]>;
|
|
454
|
-
//#endregion
|
|
455
|
-
export { NONE as _, fromOption as a, Some as b, tryCatch as c, sequence$1 as d, fromEither as f, Option as g, some as h, fromNullable as i, map2$1 as l, none as m, map3 as n, left as o, fromNullable$1 as p, sequence as r, right as s, map2 as t, map3$1 as u, None as v, OptionValue as y };
|
package/dist/option.d.mts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { _ as NONE, b as Some, d as sequence, f as fromEither, g as Option, h as some, l as map2, m as none, p as fromNullable, u as map3, v as None, y as OptionValue } from "./helpers-Bw8McftR.mjs";
|
|
2
|
-
export { NONE, None, type Option, OptionValue, Some, fromEither, fromNullable, map2, map3, none, sequence, some };
|
package/dist/option.mjs
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
//#region src/option/model.ts
|
|
2
|
-
const NONE = Symbol("None");
|
|
3
|
-
|
|
4
|
-
//#endregion
|
|
5
|
-
//#region src/option/option.ts
|
|
6
|
-
function createOption(optionValue) {
|
|
7
|
-
const option = {
|
|
8
|
-
filter: (predicate) => option.flatMap((value) => predicate(value) ? option : createOption(NONE)),
|
|
9
|
-
map: (mapper) => option.flatMap((value) => createOption({ some: mapper(value) })),
|
|
10
|
-
orElse: (fallback) => option.match(fallback, () => option),
|
|
11
|
-
ap: function(optA) {
|
|
12
|
-
return this.flatMap((fn) => optA.match(() => createOption(NONE), (arg) => createOption({ some: fn(arg) })));
|
|
13
|
-
},
|
|
14
|
-
zip: (optA) => option.map((value) => (a) => [value, a]).ap(optA),
|
|
15
|
-
flatten: function() {
|
|
16
|
-
return this.flatMap((value) => value);
|
|
17
|
-
},
|
|
18
|
-
flatMap: (mapper) => option.match(() => forceCast(option), (value) => mapper(value)),
|
|
19
|
-
tap: (sideEffect) => {
|
|
20
|
-
option.match(() => {}, sideEffect);
|
|
21
|
-
return option;
|
|
22
|
-
},
|
|
23
|
-
tapNone: (sideEffect) => {
|
|
24
|
-
option.match(sideEffect, () => {});
|
|
25
|
-
return option;
|
|
26
|
-
},
|
|
27
|
-
match: (onNone, onSome) => isSome(optionValue) ? onSome(optionValue.some) : onNone(),
|
|
28
|
-
getOrElse: (fallback) => option.match(fallback, (value) => value),
|
|
29
|
-
toNullable: () => option.match(() => null, (value) => value),
|
|
30
|
-
toArray: () => option.match(() => [], (val) => [val])
|
|
31
|
-
};
|
|
32
|
-
return option;
|
|
33
|
-
}
|
|
34
|
-
function isSome(option) {
|
|
35
|
-
return typeof option === "object" && "some" in option;
|
|
36
|
-
}
|
|
37
|
-
function forceCast(option) {
|
|
38
|
-
return option;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
//#endregion
|
|
42
|
-
//#region src/option/constructors.ts
|
|
43
|
-
/**
|
|
44
|
-
* Creates an {@link Option} that contains a value.
|
|
45
|
-
*
|
|
46
|
-
* Use this to wrap an existing value into an `Option<T>` representing presence
|
|
47
|
-
* (as opposed to `none`, which represents absence).
|
|
48
|
-
*
|
|
49
|
-
* @typeParam T - Type of the wrapped value.
|
|
50
|
-
* @param some - The value to store in the option.
|
|
51
|
-
* @returns An {@link Option} containing the provided value.
|
|
52
|
-
*/
|
|
53
|
-
function some(some) {
|
|
54
|
-
return createOption({ some });
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Creates an {@link Option} representing the absence of a value.
|
|
58
|
-
*
|
|
59
|
-
* @typeParam T - The type of the value that would be contained if present.
|
|
60
|
-
* @returns An {@link Option} in the `None` state.
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* ```ts
|
|
64
|
-
* const value = none<number>();
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
function none() {
|
|
68
|
-
return createOption(NONE);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Creates an {@link Option} from a nullable value.
|
|
72
|
-
*
|
|
73
|
-
* If {@link nullable} is `null` or `undefined`, returns {@link none}.
|
|
74
|
-
* Otherwise, wraps the provided value in {@link some}.
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* fromNullable(0).getOrElse(() => 123); // 0
|
|
78
|
-
* fromNullable(null).toNullable(); // null
|
|
79
|
-
*
|
|
80
|
-
* @typeParam T - The non-null value type to wrap.
|
|
81
|
-
* @param nullable - A value that may be `null` or `undefined`.
|
|
82
|
-
* @returns An {@link Option} that is `Some` when the value is present, otherwise `None`.
|
|
83
|
-
*/
|
|
84
|
-
function fromNullable(nullable) {
|
|
85
|
-
return nullable == null ? none() : some(nullable);
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Creates an {@link Option} from an {@link Either} by extracting the Right value.
|
|
89
|
-
*
|
|
90
|
-
* If the Either is Right, returns Some containing the Right value.
|
|
91
|
-
* If the Either is Left, returns None (discarding the Left value).
|
|
92
|
-
*
|
|
93
|
-
* @typeParam L - The type of the Left value (error type) that will be discarded
|
|
94
|
-
* @typeParam R - The type of the Right value that will be preserved in the Option
|
|
95
|
-
* @param either - The Either to convert to an Option
|
|
96
|
-
* @returns Some containing the Right value, or None if the Either was Left
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```typescript
|
|
100
|
-
* const success = right(42);
|
|
101
|
-
* fromEither(success) // Some(42)
|
|
102
|
-
*
|
|
103
|
-
* const failure = left("error message");
|
|
104
|
-
* fromEither(failure) // None
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
function fromEither(either) {
|
|
108
|
-
return either.match(() => none(), (right) => some(right));
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
//#endregion
|
|
112
|
-
//#region src/option/helpers.ts
|
|
113
|
-
/**
|
|
114
|
-
* Combines two Options using a mapping function.
|
|
115
|
-
* Only applies the mapper if both Options contain values.
|
|
116
|
-
*
|
|
117
|
-
* @param optA - First Option to combine
|
|
118
|
-
* @param optB - Second Option to combine
|
|
119
|
-
* @param mapper - Function that combines both values
|
|
120
|
-
* @returns Option containing the combined result, or None if either Option is None
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```typescript
|
|
124
|
-
* const greet = (first, last) => `Hi ${first} ${last}!`
|
|
125
|
-
* map2(some("John"), some("Doe"), greet) // Some("John Doe")
|
|
126
|
-
* map2(some("John"), none<string>(), greet) // None
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
|
-
function map2(optA, optB, mapper) {
|
|
130
|
-
return optA.map((a) => (b) => mapper(a, b)).ap(optB);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Combines three Options using a mapping function.
|
|
134
|
-
* Only applies the mapper if all three Options contain values.
|
|
135
|
-
*
|
|
136
|
-
* @param optA - First Option to combine
|
|
137
|
-
* @param optB - Second Option to combine
|
|
138
|
-
* @param optC - Third Option to combine
|
|
139
|
-
* @param mapper - Function that combines all three values
|
|
140
|
-
* @returns Option containing the combined result, or None if any Option is None
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* ```typescript
|
|
144
|
-
* const toDateString = (d, m, y) => `${d}/${m}/${y}`
|
|
145
|
-
* map3(some(15), some(6), some(2023), toDateString) // Some("15/6/2023")
|
|
146
|
-
* map3(some(15), some(6), none<number>(), toDateString) // None
|
|
147
|
-
* ```
|
|
148
|
-
*/
|
|
149
|
-
function map3(optA, optB, optC, mapper) {
|
|
150
|
-
return optA.map((a) => (b) => (c) => mapper(a, b, c)).ap(optB).ap(optC);
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Converts an array of Options into an Option of array.
|
|
154
|
-
* If all Options in the array contain values, returns Some containing an array of all values.
|
|
155
|
-
* If any Option in the array is None, returns None.
|
|
156
|
-
*
|
|
157
|
-
* @param opts - Array of Options to sequence
|
|
158
|
-
* @returns Some containing array of all values if all Options are Some, otherwise None
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* ```typescript
|
|
162
|
-
* sequence([some(1), some(2), some(3)]) // Some([1, 2, 3])
|
|
163
|
-
* sequence([some(1), none(), some(3)]) // None
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
function sequence(opts) {
|
|
167
|
-
const out = [];
|
|
168
|
-
for (const opt of opts) {
|
|
169
|
-
const [value] = opt.toArray();
|
|
170
|
-
if (!value) return none();
|
|
171
|
-
out.push(value);
|
|
172
|
-
}
|
|
173
|
-
return some(out);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
//#endregion
|
|
177
|
-
export { NONE, fromEither, fromNullable, map2, map3, none, sequence, some };
|