functype 0.11.2 → 0.14.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 +124 -21
- package/dist/{Either-DgkP4DUw.d.ts → Either-D4P39LPj.d.ts} +244 -54
- package/dist/chunk-7VZBQDNM.mjs +43 -0
- package/dist/chunk-7VZBQDNM.mjs.map +1 -0
- package/dist/do/index.d.ts +240 -0
- package/dist/do/index.mjs +2 -0
- package/dist/do/index.mjs.map +1 -0
- package/dist/either/index.d.ts +2 -2
- package/dist/either/index.mjs +1 -1
- package/dist/fpromise/index.d.ts +2 -2
- package/dist/fpromise/index.mjs +1 -1
- package/dist/index.d.ts +177 -151
- package/dist/index.mjs +1 -1
- package/dist/list/index.d.ts +2 -2
- package/dist/list/index.mjs +1 -1
- package/dist/map/index.d.ts +2 -2
- package/dist/map/index.mjs +1 -1
- package/dist/option/index.d.ts +2 -2
- package/dist/option/index.mjs +1 -1
- package/dist/set/index.d.ts +2 -2
- package/dist/set/index.mjs +1 -1
- package/dist/try/index.d.ts +2 -74
- package/dist/try/index.mjs +1 -1
- package/dist/tuple/index.d.ts +1 -1
- package/package.json +13 -7
- package/dist/chunk-PPFDDUSD.mjs +0 -43
- package/dist/chunk-PPFDDUSD.mjs.map +0 -1
- package/dist/{Serializable-CK9upOU0.d.ts → Typeable-CitTP1ay.d.ts} +42 -42
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { O as Option, E as Either, L as List, T as Try, y as Reshapeable, d as DoProtocol } from '../Either-D4P39LPj.js';
|
|
2
|
+
export { D as DO_PROTOCOL, a as DoResult } from '../Either-D4P39LPj.js';
|
|
3
|
+
import '../Typeable-CitTP1ay.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generator-based Do-notation for monadic comprehensions
|
|
7
|
+
* Provides Scala-like for-comprehension syntax using JavaScript generators
|
|
8
|
+
*
|
|
9
|
+
* ## Scala Equivalents
|
|
10
|
+
*
|
|
11
|
+
* Functype: `const x = yield* $(Option(5))`
|
|
12
|
+
* Scala: `x <- Some(5)`
|
|
13
|
+
*
|
|
14
|
+
* Functype: `return x + y`
|
|
15
|
+
* Scala: `yield x + y`
|
|
16
|
+
*
|
|
17
|
+
* ## Core Concepts
|
|
18
|
+
*
|
|
19
|
+
* - **Generators**: Use `yield* $(monad)` to extract values from monads
|
|
20
|
+
* - **Short-circuiting**: None/Left/Failure automatically propagates
|
|
21
|
+
* - **Type inference**: The $ helper provides proper TypeScript types
|
|
22
|
+
* - **First monad wins**: Return type matches the first yielded monad
|
|
23
|
+
* - **Cartesian products**: Multiple List yields create all combinations
|
|
24
|
+
*
|
|
25
|
+
* ## Usage Rules
|
|
26
|
+
*
|
|
27
|
+
* 1. All yielded values MUST be monadic (Option, Either, List, Try)
|
|
28
|
+
* 2. Use the $ helper for type inference: `yield* $(Option(value))`
|
|
29
|
+
* 3. Raw values should be assigned directly without yielding
|
|
30
|
+
* 4. Mixed monad types are supported via Reshapeable interface
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // Basic Option chaining (Scala: for { x <- Some(5); y <- Some(10) } yield x + y)
|
|
34
|
+
* const result = Do(function* () {
|
|
35
|
+
* const x = yield* $(Option(5)) // Extract from Option
|
|
36
|
+
* const y = yield* $(Option(10)) // Extract from another Option
|
|
37
|
+
* return x + y // Return final value
|
|
38
|
+
* })
|
|
39
|
+
* // result: Option<number> with value 15
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* // List comprehension (Scala: for { x <- List(1,2); y <- List(10,20) } yield (x,y))
|
|
43
|
+
* const pairs = Do(function* () {
|
|
44
|
+
* const x = yield* $(List([1, 2])) // Iterates: 1, 2
|
|
45
|
+
* const y = yield* $(List([10, 20])) // Iterates: 10, 20
|
|
46
|
+
* return { x, y } // All combinations
|
|
47
|
+
* })
|
|
48
|
+
* // pairs: List([{x:1,y:10}, {x:1,y:20}, {x:2,y:10}, {x:2,y:20}])
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // Error propagation with Either
|
|
52
|
+
* const validate = Do(function* () {
|
|
53
|
+
* const email = yield* $(validateEmail(input)) // Either<string, Email>
|
|
54
|
+
* const user = yield* $(fetchUser(email)) // Either<string, User>
|
|
55
|
+
* const saved = yield* $(saveUser(user)) // Either<string, Result>
|
|
56
|
+
* return saved
|
|
57
|
+
* })
|
|
58
|
+
* // If any step returns Left, entire chain short-circuits with that error
|
|
59
|
+
*
|
|
60
|
+
* @see {@link https://github.com/jordanburke/functype/blob/main/docs/do-notation.md} Full documentation
|
|
61
|
+
* @module Do
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
type OptionLike = {
|
|
65
|
+
_tag: "Some" | "None";
|
|
66
|
+
isSome(): boolean;
|
|
67
|
+
get(): unknown;
|
|
68
|
+
};
|
|
69
|
+
type EitherLike = {
|
|
70
|
+
_tag: "Left" | "Right";
|
|
71
|
+
isLeft(): boolean;
|
|
72
|
+
isRight(): boolean;
|
|
73
|
+
value: unknown;
|
|
74
|
+
};
|
|
75
|
+
type ListLike = {
|
|
76
|
+
_tag: "List";
|
|
77
|
+
toArray(): unknown[];
|
|
78
|
+
};
|
|
79
|
+
type TryLike = {
|
|
80
|
+
_tag: "Success" | "Failure";
|
|
81
|
+
isSuccess(): boolean;
|
|
82
|
+
get(): unknown;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Executes a generator-based monadic comprehension
|
|
86
|
+
* Returns the same monad type as the first yielded monad (Scala semantics)
|
|
87
|
+
*
|
|
88
|
+
* - Option comprehensions return Option (None on short-circuit)
|
|
89
|
+
* - Either comprehensions return Either (Left with error on short-circuit)
|
|
90
|
+
* - List comprehensions return List (empty or cartesian product)
|
|
91
|
+
* - Try comprehensions return Try (Failure with error on short-circuit)
|
|
92
|
+
*
|
|
93
|
+
* Type Inference Notes:
|
|
94
|
+
* - TypeScript infers the correct return type for homogeneous comprehensions
|
|
95
|
+
* - For mixed monad types, TypeScript returns a union type
|
|
96
|
+
* - Use DoTyped<T> or type assertions for mixed scenarios
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* // Option comprehension returns Option:
|
|
101
|
+
* const result = Do(function* () {
|
|
102
|
+
* const x = yield* $(Option(5));
|
|
103
|
+
* const y = yield* $(Option(10));
|
|
104
|
+
* return x + y;
|
|
105
|
+
* });
|
|
106
|
+
* // result: Option(15)
|
|
107
|
+
*
|
|
108
|
+
* // Either comprehension returns Either:
|
|
109
|
+
* const result = Do(function* () {
|
|
110
|
+
* const x = yield* $(Right(5));
|
|
111
|
+
* const y = yield* $(Left("error"));
|
|
112
|
+
* return x + y;
|
|
113
|
+
* });
|
|
114
|
+
* // result: Left("error") - error is preserved
|
|
115
|
+
*
|
|
116
|
+
* // List comprehension returns List with cartesian product:
|
|
117
|
+
* const result = Do(function* () {
|
|
118
|
+
* const x = yield* $(List([1, 2]));
|
|
119
|
+
* const y = yield* $(List([3, 4]));
|
|
120
|
+
* return x + y;
|
|
121
|
+
* });
|
|
122
|
+
* // result: List([4, 5, 5, 6])
|
|
123
|
+
*
|
|
124
|
+
* // Mixed types - use type assertion or DoTyped:
|
|
125
|
+
* const result = Do(function* () {
|
|
126
|
+
* const x = yield* $(Option(5));
|
|
127
|
+
* const y = yield* $(Right<string, number>(10));
|
|
128
|
+
* return x + y;
|
|
129
|
+
* }) as Option<number>;
|
|
130
|
+
* // result: Option(15)
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @param gen - Generator function that yields monads and returns a result
|
|
134
|
+
* @returns The same monad type as the first yield
|
|
135
|
+
*/
|
|
136
|
+
declare function Do<T>(gen: () => Generator<OptionLike, T, unknown>): Option<T>;
|
|
137
|
+
declare function Do<L, R>(gen: () => Generator<EitherLike, R, unknown>): Either<L, R>;
|
|
138
|
+
declare function Do<T>(gen: () => Generator<ListLike, T, unknown>): List<T>;
|
|
139
|
+
declare function Do<T>(gen: () => Generator<TryLike, T, unknown>): Try<T>;
|
|
140
|
+
declare function Do<T>(gen: () => Generator<OptionLike | EitherLike | ListLike | TryLike, T, unknown>): Reshapeable<T>;
|
|
141
|
+
declare function Do<T>(gen: () => Generator<unknown, T, unknown>): unknown;
|
|
142
|
+
/**
|
|
143
|
+
* Executes an async generator-based monadic comprehension
|
|
144
|
+
* Returns the same monad type as the first yielded monad
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const result = await DoAsync(async function* () {
|
|
149
|
+
* const user = yield* $(await fetchUser(id)); // Promise<Option<User>> → User
|
|
150
|
+
* const profile = yield* $(await getProfile(user)); // Promise<Either<Error, Profile>> → Profile
|
|
151
|
+
* return { user, profile };
|
|
152
|
+
* });
|
|
153
|
+
* // result type matches first yield
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @param gen - Async generator function that yields monads/promises and returns a result
|
|
157
|
+
* @returns Promise of the same monad type as first yield
|
|
158
|
+
*/
|
|
159
|
+
declare function DoAsync<T>(gen: () => AsyncGenerator<OptionLike, T, unknown>): Promise<Option<T>>;
|
|
160
|
+
declare function DoAsync<L, R>(gen: () => AsyncGenerator<EitherLike, R, unknown>): Promise<Either<L, R>>;
|
|
161
|
+
declare function DoAsync<T>(gen: () => AsyncGenerator<ListLike, T, unknown>): Promise<List<T>>;
|
|
162
|
+
declare function DoAsync<T>(gen: () => AsyncGenerator<TryLike, T, unknown>): Promise<Try<T>>;
|
|
163
|
+
declare function DoAsync<T>(gen: () => AsyncGenerator<OptionLike | EitherLike | ListLike | TryLike, T, unknown>): Promise<Reshapeable<T>>;
|
|
164
|
+
declare function DoAsync<T>(gen: () => AsyncGenerator<unknown, T, unknown>): Promise<unknown>;
|
|
165
|
+
/**
|
|
166
|
+
* Helper function to check if a value implements the Do protocol
|
|
167
|
+
* @param value - Value to check
|
|
168
|
+
* @returns True if the value implements DoProtocol
|
|
169
|
+
*/
|
|
170
|
+
declare function isDoCapable<T>(value: unknown): value is DoProtocol<T>;
|
|
171
|
+
/**
|
|
172
|
+
* Manually unwrap a monad using the Do protocol
|
|
173
|
+
* Useful for testing or when you need to unwrap outside of a Do-comprehension
|
|
174
|
+
*
|
|
175
|
+
* @param monad - Monad to unwrap
|
|
176
|
+
* @returns The unwrapped value
|
|
177
|
+
* @throws Error if the monad cannot be unwrapped
|
|
178
|
+
*/
|
|
179
|
+
declare function unwrap<T>(monad: DoProtocol<T>): T;
|
|
180
|
+
/**
|
|
181
|
+
* Type helper for Do-notation generators.
|
|
182
|
+
* Provides better type hints in IDEs.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const result = Do(function* (): DoGenerator<number> {
|
|
187
|
+
* const x = yield* $(List([1, 2])) // x is still unknown but return type is clear
|
|
188
|
+
* const y = yield* $(List([3, 4]))
|
|
189
|
+
* return x + y
|
|
190
|
+
* })
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
type DoGenerator<T, TYield = unknown> = Generator<TYield, T, unknown>;
|
|
194
|
+
/**
|
|
195
|
+
* Extracts values from monads in Do-notation with type inference.
|
|
196
|
+
* The '$' symbol is the universal extraction operator in functional programming.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* const result = Do(function* () {
|
|
201
|
+
* const x = yield* $(Option(5)) // x: number
|
|
202
|
+
* const y = yield* $(List([1, 2, 3])) // y: number (for cartesian product)
|
|
203
|
+
* const name = yield* $(Right("Alice")) // name: string
|
|
204
|
+
* return `${name}: ${x + y}`
|
|
205
|
+
* })
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @param monad - Any monad that can be unwrapped (Option, Either, List, Try, etc.)
|
|
209
|
+
* @returns A generator that yields the monad and returns its extracted value
|
|
210
|
+
*/
|
|
211
|
+
declare function $<T>(monad: Option<T>): Generator<Option<T>, T, T>;
|
|
212
|
+
declare function $<L, R>(monad: Either<L, R>): Generator<Either<L, R>, R, R>;
|
|
213
|
+
declare function $<T>(monad: List<T>): Generator<List<T>, T, T>;
|
|
214
|
+
declare function $<T>(monad: Try<T>): Generator<Try<T>, T, T>;
|
|
215
|
+
declare function $<T>(monad: DoProtocol<T>): Generator<DoProtocol<T>, T, T>;
|
|
216
|
+
declare function $<M>(monad: M): Generator<M, InferYieldType<M>, InferYieldType<M>>;
|
|
217
|
+
type InferYieldType<M> = M extends {
|
|
218
|
+
isSome(): boolean;
|
|
219
|
+
get(): infer T;
|
|
220
|
+
} ? T : M extends {
|
|
221
|
+
isRight(): boolean;
|
|
222
|
+
value: infer R;
|
|
223
|
+
} ? R : M extends {
|
|
224
|
+
toArray(): (infer T)[];
|
|
225
|
+
} ? T : M extends {
|
|
226
|
+
isSuccess(): boolean;
|
|
227
|
+
get(): infer T;
|
|
228
|
+
} ? T : M extends DoProtocol<infer T> ? T : unknown;
|
|
229
|
+
declare const NoneError: (message?: string) => Error;
|
|
230
|
+
interface LeftErrorType<L> extends Error {
|
|
231
|
+
value: L;
|
|
232
|
+
}
|
|
233
|
+
declare const LeftError: <L>(value: L, message?: string) => LeftErrorType<L>;
|
|
234
|
+
declare const EmptyListError: (message?: string) => Error;
|
|
235
|
+
interface FailureErrorType extends Error {
|
|
236
|
+
cause: Error;
|
|
237
|
+
}
|
|
238
|
+
declare const FailureError: (cause: Error, message?: string) => FailureErrorType;
|
|
239
|
+
|
|
240
|
+
export { $, Do, DoAsync, type DoGenerator, DoProtocol, EmptyListError, FailureError, type FailureErrorType, LeftError, type LeftErrorType, NoneError, isDoCapable, unwrap };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export{P as $,a as DO_PROTOCOL,L as Do,M as DoAsync,S as EmptyListError,T as FailureError,R as LeftError,Q as NoneError,N as isDoCapable,O as unwrap}from'../chunk-7VZBQDNM.mjs';import'../chunk-BQJB6CCW.mjs';import'../chunk-YBBRJTHY.mjs';//# sourceMappingURL=index.mjs.map
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.mjs"}
|
package/dist/either/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { E as Either,
|
|
2
|
-
import '../
|
|
1
|
+
export { E as Either, f as Left, R as Right, e as TestEither, j as TypeCheckLeft, h as TypeCheckRight, g as isLeft, i as isRight, t as tryCatch, k as tryCatchAsync } from '../Either-D4P39LPj.js';
|
|
2
|
+
import '../Typeable-CitTP1ay.js';
|
package/dist/either/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export{
|
|
1
|
+
export{u as Either,n as Left,m as Right,s as TypeCheckLeft,r as TypeCheckRight,p as isLeft,o as isRight,q as tryCatch,t as tryCatchAsync}from'../chunk-7VZBQDNM.mjs';import'../chunk-BQJB6CCW.mjs';import'../chunk-YBBRJTHY.mjs';//# sourceMappingURL=index.mjs.map
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/fpromise/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { E as Either } from '../Either-
|
|
2
|
-
import { T as Type } from '../
|
|
1
|
+
import { E as Either } from '../Either-D4P39LPj.js';
|
|
2
|
+
import { T as Type } from '../Typeable-CitTP1ay.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Error context information that provides additional metadata about errors.
|
package/dist/fpromise/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export{
|
|
1
|
+
export{X as FPromise,W as FPromiseCompanion}from'../chunk-7VZBQDNM.mjs';import'../chunk-BQJB6CCW.mjs';import'../chunk-YBBRJTHY.mjs';//# sourceMappingURL=index.mjs.map
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|