@nlozgachev/pipelined 0.12.0 → 0.13.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 (205) hide show
  1. package/LICENCE +28 -0
  2. package/README.md +1 -1
  3. package/{types/src/Types/NonEmptyList.d.ts → dist/NonEmptyList-BlGFjor5.d.mts} +4 -3
  4. package/dist/NonEmptyList-BlGFjor5.d.ts +30 -0
  5. package/dist/Task-BB8Wmc1J.d.ts +677 -0
  6. package/dist/Task-ChKyH0pF.d.mts +677 -0
  7. package/dist/chunk-DBIC62UV.mjs +6 -0
  8. package/dist/chunk-QPTGO5AS.mjs +150 -0
  9. package/dist/composition.d.mts +495 -0
  10. package/dist/composition.d.ts +495 -0
  11. package/dist/composition.js +188 -0
  12. package/dist/composition.mjs +135 -0
  13. package/dist/core.d.mts +2170 -0
  14. package/dist/core.d.ts +2170 -0
  15. package/dist/core.js +698 -0
  16. package/dist/core.mjs +517 -0
  17. package/dist/types.d.mts +54 -0
  18. package/{types/src/Types/Brand.d.ts → dist/types.d.ts} +6 -4
  19. package/dist/types.js +41 -0
  20. package/dist/types.mjs +14 -0
  21. package/dist/utils.d.mts +1285 -0
  22. package/dist/utils.d.ts +1285 -0
  23. package/dist/utils.js +722 -0
  24. package/dist/utils.mjs +553 -0
  25. package/package.json +63 -69
  26. package/esm/mod.js +0 -3
  27. package/esm/package.json +0 -3
  28. package/esm/src/Composition/compose.js +0 -3
  29. package/esm/src/Composition/converge.js +0 -3
  30. package/esm/src/Composition/curry.js +0 -42
  31. package/esm/src/Composition/flip.js +0 -20
  32. package/esm/src/Composition/flow.js +0 -8
  33. package/esm/src/Composition/fn.js +0 -85
  34. package/esm/src/Composition/index.js +0 -13
  35. package/esm/src/Composition/juxt.js +0 -3
  36. package/esm/src/Composition/memoize.js +0 -66
  37. package/esm/src/Composition/not.js +0 -25
  38. package/esm/src/Composition/on.js +0 -12
  39. package/esm/src/Composition/pipe.js +0 -3
  40. package/esm/src/Composition/tap.js +0 -33
  41. package/esm/src/Composition/uncurry.js +0 -32
  42. package/esm/src/Core/Deferred.js +0 -30
  43. package/esm/src/Core/InternalTypes.js +0 -1
  44. package/esm/src/Core/Lens.js +0 -98
  45. package/esm/src/Core/Logged.js +0 -111
  46. package/esm/src/Core/Option.js +0 -191
  47. package/esm/src/Core/Optional.js +0 -160
  48. package/esm/src/Core/Predicate.js +0 -133
  49. package/esm/src/Core/Reader.js +0 -134
  50. package/esm/src/Core/Refinement.js +0 -115
  51. package/esm/src/Core/RemoteData.js +0 -211
  52. package/esm/src/Core/Result.js +0 -170
  53. package/esm/src/Core/State.js +0 -181
  54. package/esm/src/Core/Task.js +0 -223
  55. package/esm/src/Core/TaskOption.js +0 -106
  56. package/esm/src/Core/TaskResult.js +0 -127
  57. package/esm/src/Core/TaskValidation.js +0 -128
  58. package/esm/src/Core/These.js +0 -245
  59. package/esm/src/Core/Tuple.js +0 -112
  60. package/esm/src/Core/Validation.js +0 -212
  61. package/esm/src/Core/index.js +0 -18
  62. package/esm/src/Types/Brand.js +0 -28
  63. package/esm/src/Types/NonEmptyList.js +0 -14
  64. package/esm/src/Types/index.js +0 -2
  65. package/esm/src/Utils/Arr.js +0 -570
  66. package/esm/src/Utils/Dict.js +0 -421
  67. package/esm/src/Utils/Num.js +0 -124
  68. package/esm/src/Utils/Rec.js +0 -241
  69. package/esm/src/Utils/Str.js +0 -134
  70. package/esm/src/Utils/Uniq.js +0 -265
  71. package/esm/src/Utils/index.js +0 -6
  72. package/script/mod.js +0 -19
  73. package/script/package.json +0 -3
  74. package/script/src/Composition/compose.js +0 -6
  75. package/script/src/Composition/converge.js +0 -6
  76. package/script/src/Composition/curry.js +0 -48
  77. package/script/src/Composition/flip.js +0 -24
  78. package/script/src/Composition/flow.js +0 -11
  79. package/script/src/Composition/fn.js +0 -98
  80. package/script/src/Composition/index.js +0 -29
  81. package/script/src/Composition/juxt.js +0 -6
  82. package/script/src/Composition/memoize.js +0 -71
  83. package/script/src/Composition/not.js +0 -29
  84. package/script/src/Composition/on.js +0 -16
  85. package/script/src/Composition/pipe.js +0 -6
  86. package/script/src/Composition/tap.js +0 -37
  87. package/script/src/Composition/uncurry.js +0 -38
  88. package/script/src/Core/Deferred.js +0 -33
  89. package/script/src/Core/InternalTypes.js +0 -2
  90. package/script/src/Core/Lens.js +0 -101
  91. package/script/src/Core/Logged.js +0 -114
  92. package/script/src/Core/Option.js +0 -194
  93. package/script/src/Core/Optional.js +0 -163
  94. package/script/src/Core/Predicate.js +0 -136
  95. package/script/src/Core/Reader.js +0 -137
  96. package/script/src/Core/Refinement.js +0 -118
  97. package/script/src/Core/RemoteData.js +0 -214
  98. package/script/src/Core/Result.js +0 -173
  99. package/script/src/Core/State.js +0 -184
  100. package/script/src/Core/Task.js +0 -226
  101. package/script/src/Core/TaskOption.js +0 -109
  102. package/script/src/Core/TaskResult.js +0 -130
  103. package/script/src/Core/TaskValidation.js +0 -131
  104. package/script/src/Core/These.js +0 -248
  105. package/script/src/Core/Tuple.js +0 -115
  106. package/script/src/Core/Validation.js +0 -215
  107. package/script/src/Core/index.js +0 -34
  108. package/script/src/Types/Brand.js +0 -31
  109. package/script/src/Types/NonEmptyList.js +0 -18
  110. package/script/src/Types/index.js +0 -18
  111. package/script/src/Utils/Arr.js +0 -573
  112. package/script/src/Utils/Dict.js +0 -424
  113. package/script/src/Utils/Num.js +0 -127
  114. package/script/src/Utils/Rec.js +0 -244
  115. package/script/src/Utils/Str.js +0 -137
  116. package/script/src/Utils/Uniq.js +0 -268
  117. package/script/src/Utils/index.js +0 -22
  118. package/types/mod.d.ts +0 -4
  119. package/types/mod.d.ts.map +0 -1
  120. package/types/src/Composition/compose.d.ts +0 -33
  121. package/types/src/Composition/compose.d.ts.map +0 -1
  122. package/types/src/Composition/converge.d.ts +0 -21
  123. package/types/src/Composition/converge.d.ts.map +0 -1
  124. package/types/src/Composition/curry.d.ts +0 -43
  125. package/types/src/Composition/curry.d.ts.map +0 -1
  126. package/types/src/Composition/flip.d.ts +0 -21
  127. package/types/src/Composition/flip.d.ts.map +0 -1
  128. package/types/src/Composition/flow.d.ts +0 -56
  129. package/types/src/Composition/flow.d.ts.map +0 -1
  130. package/types/src/Composition/fn.d.ts +0 -76
  131. package/types/src/Composition/fn.d.ts.map +0 -1
  132. package/types/src/Composition/index.d.ts +0 -14
  133. package/types/src/Composition/index.d.ts.map +0 -1
  134. package/types/src/Composition/juxt.d.ts +0 -18
  135. package/types/src/Composition/juxt.d.ts.map +0 -1
  136. package/types/src/Composition/memoize.d.ts +0 -46
  137. package/types/src/Composition/memoize.d.ts.map +0 -1
  138. package/types/src/Composition/not.d.ts +0 -26
  139. package/types/src/Composition/not.d.ts.map +0 -1
  140. package/types/src/Composition/on.d.ts +0 -13
  141. package/types/src/Composition/on.d.ts.map +0 -1
  142. package/types/src/Composition/pipe.d.ts +0 -56
  143. package/types/src/Composition/pipe.d.ts.map +0 -1
  144. package/types/src/Composition/tap.d.ts +0 -31
  145. package/types/src/Composition/tap.d.ts.map +0 -1
  146. package/types/src/Composition/uncurry.d.ts +0 -54
  147. package/types/src/Composition/uncurry.d.ts.map +0 -1
  148. package/types/src/Core/Deferred.d.ts +0 -49
  149. package/types/src/Core/Deferred.d.ts.map +0 -1
  150. package/types/src/Core/InternalTypes.d.ts +0 -23
  151. package/types/src/Core/InternalTypes.d.ts.map +0 -1
  152. package/types/src/Core/Lens.d.ts +0 -118
  153. package/types/src/Core/Lens.d.ts.map +0 -1
  154. package/types/src/Core/Logged.d.ts +0 -126
  155. package/types/src/Core/Logged.d.ts.map +0 -1
  156. package/types/src/Core/Option.d.ts +0 -209
  157. package/types/src/Core/Option.d.ts.map +0 -1
  158. package/types/src/Core/Optional.d.ts +0 -158
  159. package/types/src/Core/Optional.d.ts.map +0 -1
  160. package/types/src/Core/Predicate.d.ts +0 -161
  161. package/types/src/Core/Predicate.d.ts.map +0 -1
  162. package/types/src/Core/Reader.d.ts +0 -156
  163. package/types/src/Core/Reader.d.ts.map +0 -1
  164. package/types/src/Core/Refinement.d.ts +0 -138
  165. package/types/src/Core/Refinement.d.ts.map +0 -1
  166. package/types/src/Core/RemoteData.d.ts +0 -197
  167. package/types/src/Core/RemoteData.d.ts.map +0 -1
  168. package/types/src/Core/Result.d.ts +0 -182
  169. package/types/src/Core/Result.d.ts.map +0 -1
  170. package/types/src/Core/State.d.ts +0 -192
  171. package/types/src/Core/State.d.ts.map +0 -1
  172. package/types/src/Core/Task.d.ts +0 -219
  173. package/types/src/Core/Task.d.ts.map +0 -1
  174. package/types/src/Core/TaskOption.d.ts +0 -121
  175. package/types/src/Core/TaskOption.d.ts.map +0 -1
  176. package/types/src/Core/TaskResult.d.ts +0 -119
  177. package/types/src/Core/TaskResult.d.ts.map +0 -1
  178. package/types/src/Core/TaskValidation.d.ts +0 -144
  179. package/types/src/Core/TaskValidation.d.ts.map +0 -1
  180. package/types/src/Core/These.d.ts +0 -225
  181. package/types/src/Core/These.d.ts.map +0 -1
  182. package/types/src/Core/Tuple.d.ts +0 -129
  183. package/types/src/Core/Tuple.d.ts.map +0 -1
  184. package/types/src/Core/Validation.d.ts +0 -203
  185. package/types/src/Core/Validation.d.ts.map +0 -1
  186. package/types/src/Core/index.d.ts +0 -19
  187. package/types/src/Core/index.d.ts.map +0 -1
  188. package/types/src/Types/Brand.d.ts.map +0 -1
  189. package/types/src/Types/NonEmptyList.d.ts.map +0 -1
  190. package/types/src/Types/index.d.ts +0 -3
  191. package/types/src/Types/index.d.ts.map +0 -1
  192. package/types/src/Utils/Arr.d.ts +0 -403
  193. package/types/src/Utils/Arr.d.ts.map +0 -1
  194. package/types/src/Utils/Dict.d.ts +0 -310
  195. package/types/src/Utils/Dict.d.ts.map +0 -1
  196. package/types/src/Utils/Num.d.ts +0 -110
  197. package/types/src/Utils/Num.d.ts.map +0 -1
  198. package/types/src/Utils/Rec.d.ts +0 -159
  199. package/types/src/Utils/Rec.d.ts.map +0 -1
  200. package/types/src/Utils/Str.d.ts +0 -128
  201. package/types/src/Utils/Str.d.ts.map +0 -1
  202. package/types/src/Utils/Uniq.d.ts +0 -179
  203. package/types/src/Utils/Uniq.d.ts.map +0 -1
  204. package/types/src/Utils/index.d.ts +0 -7
  205. package/types/src/Utils/index.d.ts.map +0 -1
@@ -1,223 +0,0 @@
1
- import { Deferred } from "./Deferred.js";
2
- import { Result } from "./Result.js";
3
- // Internal helper — not exported. Runs a Task and converts the result to a Promise
4
- // so that combinators can use Promise chaining (.then, Promise.all, Promise.race, etc.)
5
- // internally without leaking that primitive through the public API.
6
- const toPromise = (task) => Deferred.toPromise(task());
7
- export var Task;
8
- (function (Task) {
9
- /**
10
- * Creates a Task that immediately resolves to the given value.
11
- *
12
- * @example
13
- * ```ts
14
- * const task = Task.resolve(42);
15
- * const value = await task(); // 42
16
- * ```
17
- */
18
- Task.resolve = (value) => () => Deferred.fromPromise(Promise.resolve(value));
19
- /**
20
- * Creates a Task from a function that returns a Promise.
21
- *
22
- * @example
23
- * ```ts
24
- * const getTimestamp = Task.from(() => Promise.resolve(Date.now()));
25
- * ```
26
- */
27
- Task.from = (f) => () => Deferred.fromPromise(f());
28
- /**
29
- * Transforms the value inside a Task.
30
- *
31
- * @example
32
- * ```ts
33
- * pipe(
34
- * Task.resolve(5),
35
- * Task.map(n => n * 2)
36
- * )(); // Deferred<10>
37
- * ```
38
- */
39
- Task.map = (f) => (data) => Task.from(() => toPromise(data).then(f));
40
- /**
41
- * Chains Task computations. Passes the resolved value of the first Task to f.
42
- *
43
- * @example
44
- * ```ts
45
- * const readUserId: Task<string> = Task.resolve(session.userId);
46
- * const loadPrefs = (id: string): Task<Preferences> =>
47
- * Task.resolve(prefsCache.get(id));
48
- *
49
- * pipe(
50
- * readUserId,
51
- * Task.chain(loadPrefs)
52
- * )(); // Deferred<Preferences>
53
- * ```
54
- */
55
- Task.chain = (f) => (data) => Task.from(() => toPromise(data).then((a) => toPromise(f(a))));
56
- /**
57
- * Applies a function wrapped in a Task to a value wrapped in a Task.
58
- * Both Tasks run in parallel.
59
- *
60
- * @example
61
- * ```ts
62
- * const add = (a: number) => (b: number) => a + b;
63
- * pipe(
64
- * Task.resolve(add),
65
- * Task.ap(Task.resolve(5)),
66
- * Task.ap(Task.resolve(3))
67
- * )(); // Deferred<8>
68
- * ```
69
- */
70
- Task.ap = (arg) => (data) => Task.from(() => Promise.all([
71
- toPromise(data),
72
- toPromise(arg),
73
- ]).then(([f, a]) => f(a)));
74
- /**
75
- * Executes a side effect on the value without changing the Task.
76
- * Useful for logging or debugging.
77
- *
78
- * @example
79
- * ```ts
80
- * pipe(
81
- * loadConfig,
82
- * Task.tap(cfg => console.log("Config:", cfg)),
83
- * Task.map(buildReport)
84
- * );
85
- * ```
86
- */
87
- Task.tap = (f) => (data) => Task.from(() => toPromise(data).then((a) => {
88
- f(a);
89
- return a;
90
- }));
91
- /**
92
- * Runs multiple Tasks in parallel and collects their results.
93
- *
94
- * @example
95
- * ```ts
96
- * Task.all([loadConfig, detectLocale, loadTheme])();
97
- * // Deferred<[Config, string, Theme]>
98
- * ```
99
- */
100
- Task.all = (tasks) => Task.from(() => Promise.all(tasks.map((t) => toPromise(t))));
101
- /**
102
- * Delays the execution of a Task by the specified milliseconds.
103
- * Useful for debouncing or rate limiting.
104
- *
105
- * @example
106
- * ```ts
107
- * pipe(
108
- * Task.resolve(42),
109
- * Task.delay(1000)
110
- * )(); // Resolves after 1 second
111
- * ```
112
- */
113
- Task.delay = (ms) => (data) => Task.from(() => new Promise((resolve) => setTimeout(() => toPromise(data).then(resolve), ms)));
114
- /**
115
- * Runs a Task a fixed number of times sequentially, collecting all results into an array.
116
- * An optional delay (ms) can be inserted between runs.
117
- *
118
- * @example
119
- * ```ts
120
- * pipe(
121
- * pollSensor,
122
- * Task.repeat({ times: 5, delay: 1000 })
123
- * )(); // Task<Reading[]> — 5 readings, one per second
124
- * ```
125
- */
126
- Task.repeat = (options) => (task) => Task.from(() => {
127
- const { times, delay: ms } = options;
128
- if (times <= 0)
129
- return Promise.resolve([]);
130
- const results = [];
131
- const wait = () => ms !== undefined && ms > 0 ? new Promise((r) => setTimeout(r, ms)) : Promise.resolve();
132
- const run = (left) => toPromise(task).then((a) => {
133
- results.push(a);
134
- if (left <= 1)
135
- return results;
136
- return wait().then(() => run(left - 1));
137
- });
138
- return run(times);
139
- });
140
- /**
141
- * Runs a Task repeatedly until the result satisfies a predicate, returning that result.
142
- * An optional delay (ms) can be inserted between runs.
143
- *
144
- * @example
145
- * ```ts
146
- * pipe(
147
- * checkStatus,
148
- * Task.repeatUntil({ when: (s) => s === "ready", delay: 500 })
149
- * )(); // polls every 500ms until status is "ready"
150
- * ```
151
- */
152
- Task.repeatUntil = (options) => (task) => Task.from(() => {
153
- const { when: predicate, delay: ms } = options;
154
- const wait = () => ms !== undefined && ms > 0 ? new Promise((r) => setTimeout(r, ms)) : Promise.resolve();
155
- const run = () => toPromise(task).then((a) => {
156
- if (predicate(a))
157
- return a;
158
- return wait().then(run);
159
- });
160
- return run();
161
- });
162
- /**
163
- * Resolves with the value of the first Task to complete. All Tasks start
164
- * immediately; the rest are abandoned once one resolves.
165
- *
166
- * @example
167
- * ```ts
168
- * const fast = Task.from(() => new Promise<string>(r => setTimeout(() => r("fast"), 10)));
169
- * const slow = Task.from(() => new Promise<string>(r => setTimeout(() => r("slow"), 200)));
170
- *
171
- * await Task.race([fast, slow])(); // "fast"
172
- * ```
173
- */
174
- Task.race = (tasks) => Task.from(() => Promise.race(tasks.map(toPromise)));
175
- /**
176
- * Runs an array of Tasks one at a time in order, collecting all results.
177
- * Each Task starts only after the previous one resolves.
178
- *
179
- * @example
180
- * ```ts
181
- * let log: number[] = [];
182
- * const makeTask = (n: number) => Task.from(() => {
183
- * log.push(n);
184
- * return Promise.resolve(n);
185
- * });
186
- *
187
- * await Task.sequential([makeTask(1), makeTask(2), makeTask(3)])();
188
- * // log = [1, 2, 3] — tasks ran in order
189
- * ```
190
- */
191
- Task.sequential = (tasks) => Task.from(async () => {
192
- const results = [];
193
- for (const task of tasks) {
194
- results.push(await toPromise(task));
195
- }
196
- return results;
197
- });
198
- /**
199
- * Converts a `Task<A>` into a `Task<Result<E, A>>`, resolving to `Err` if the
200
- * Task does not complete within the given time.
201
- *
202
- * @example
203
- * ```ts
204
- * pipe(
205
- * heavyComputation,
206
- * Task.timeout(5000, () => "timed out"),
207
- * TaskResult.chain(processResult)
208
- * );
209
- * ```
210
- */
211
- Task.timeout = (ms, onTimeout) => (task) => Task.from(() => {
212
- let timerId;
213
- return Promise.race([
214
- toPromise(task).then((a) => {
215
- clearTimeout(timerId);
216
- return Result.ok(a);
217
- }),
218
- new Promise((resolve) => {
219
- timerId = setTimeout(() => resolve(Result.err(onTimeout())), ms);
220
- }),
221
- ]);
222
- });
223
- })(Task || (Task = {}));
@@ -1,106 +0,0 @@
1
- import { Deferred } from "./Deferred.js";
2
- import { Option } from "./Option.js";
3
- import { Task } from "./Task.js";
4
- export var TaskOption;
5
- (function (TaskOption) {
6
- /**
7
- * Wraps a value in a Some inside a Task.
8
- */
9
- TaskOption.some = (value) => Task.resolve(Option.some(value));
10
- /**
11
- * Creates a TaskOption that resolves to None.
12
- */
13
- TaskOption.none = () => Task.resolve(Option.none());
14
- /**
15
- * Lifts an Option into a TaskOption.
16
- */
17
- TaskOption.fromOption = (option) => Task.resolve(option);
18
- /**
19
- * Lifts a Task into a TaskOption by wrapping its result in Some.
20
- */
21
- TaskOption.fromTask = (task) => Task.map(Option.some)(task);
22
- /**
23
- * Creates a TaskOption from a Promise-returning function.
24
- * Returns Some if the promise resolves, None if it rejects.
25
- *
26
- * @example
27
- * ```ts
28
- * const fetchUser = TaskOption.tryCatch(() =>
29
- * fetch("/user/1").then(r => r.json())
30
- * );
31
- * ```
32
- */
33
- TaskOption.tryCatch = (f) => Task.from(() => f()
34
- .then(Option.some)
35
- .catch(() => Option.none()));
36
- /**
37
- * Transforms the value inside a TaskOption.
38
- */
39
- TaskOption.map = (f) => (data) => Task.map(Option.map(f))(data);
40
- /**
41
- * Chains TaskOption computations. If the first resolves to Some, passes the
42
- * value to f. If the first resolves to None, propagates None.
43
- *
44
- * @example
45
- * ```ts
46
- * pipe(
47
- * findUser("123"),
48
- * TaskOption.chain(user => findOrg(user.orgId))
49
- * )();
50
- * ```
51
- */
52
- TaskOption.chain = (f) => (data) => Task.chain((option) => Option.isSome(option) ? f(option.value) : Task.resolve(Option.none()))(data);
53
- /**
54
- * Applies a function wrapped in a TaskOption to a value wrapped in a TaskOption.
55
- * Both Tasks run in parallel.
56
- */
57
- TaskOption.ap = (arg) => (data) => Task.from(() => Promise.all([
58
- Deferred.toPromise(data()),
59
- Deferred.toPromise(arg()),
60
- ]).then(([of_, oa]) => Option.ap(oa)(of_)));
61
- /**
62
- * Extracts a value from a TaskOption by providing handlers for both cases.
63
- */
64
- TaskOption.fold = (onNone, onSome) => (data) => Task.map(Option.fold(onNone, onSome))(data);
65
- /**
66
- * Pattern matches on a TaskOption, returning a Task of the result.
67
- *
68
- * @example
69
- * ```ts
70
- * pipe(
71
- * findUser("123"),
72
- * TaskOption.match({
73
- * some: user => `Hello, ${user.name}`,
74
- * none: () => "User not found"
75
- * })
76
- * )();
77
- * ```
78
- */
79
- TaskOption.match = (cases) => (data) => Task.map(Option.match(cases))(data);
80
- /**
81
- * Returns the value or a default if the TaskOption resolves to None.
82
- * The default can be a different type, widening the result to `Task<A | B>`.
83
- */
84
- TaskOption.getOrElse = (defaultValue) => (data) => Task.map(Option.getOrElse(defaultValue))(data);
85
- /**
86
- * Executes a side effect on the value without changing the TaskOption.
87
- * Useful for logging or debugging.
88
- */
89
- TaskOption.tap = (f) => (data) => Task.map(Option.tap(f))(data);
90
- /**
91
- * Filters the value inside a TaskOption. Returns None if the predicate fails.
92
- */
93
- TaskOption.filter = (predicate) => (data) => Task.map(Option.filter(predicate))(data);
94
- /**
95
- * Converts a TaskOption to a TaskResult, using onNone to produce the error value.
96
- *
97
- * @example
98
- * ```ts
99
- * pipe(
100
- * findUser("123"),
101
- * TaskOption.toTaskResult(() => "User not found")
102
- * );
103
- * ```
104
- */
105
- TaskOption.toTaskResult = (onNone) => (data) => Task.map(Option.toResult(onNone))(data);
106
- })(TaskOption || (TaskOption = {}));
@@ -1,127 +0,0 @@
1
- import { Deferred } from "./Deferred.js";
2
- import { Result } from "./Result.js";
3
- import { Task } from "./Task.js";
4
- export var TaskResult;
5
- (function (TaskResult) {
6
- /**
7
- * Wraps a value in a successful TaskResult.
8
- */
9
- TaskResult.ok = (value) => Task.resolve(Result.ok(value));
10
- /**
11
- * Creates a failed TaskResult with the given error.
12
- */
13
- TaskResult.err = (error) => Task.resolve(Result.err(error));
14
- /**
15
- * Creates a TaskResult from a function that may throw.
16
- * Catches any errors and transforms them using the onError function.
17
- *
18
- * @example
19
- * ```ts
20
- * const parseJson = (s: string): TaskResult<string, unknown> =>
21
- * TaskResult.tryCatch(
22
- * async () => JSON.parse(s),
23
- * (e) => `Parse error: ${e}`
24
- * );
25
- * ```
26
- */
27
- TaskResult.tryCatch = (f, onError) => Task.from(() => f()
28
- .then(Result.ok)
29
- .catch((e) => Result.err(onError(e))));
30
- /**
31
- * Transforms the success value inside a TaskResult.
32
- */
33
- TaskResult.map = (f) => (data) => Task.map(Result.map(f))(data);
34
- /**
35
- * Transforms the error value inside a TaskResult.
36
- */
37
- TaskResult.mapError = (f) => (data) => Task.map(Result.mapError(f))(data);
38
- /**
39
- * Chains TaskResult computations. If the first succeeds, passes the value to f.
40
- * If the first fails, propagates the error.
41
- */
42
- TaskResult.chain = (f) => (data) => Task.chain((result) => Result.isOk(result) ? f(result.value) : Task.resolve(Result.err(result.error)))(data);
43
- /**
44
- * Extracts the value from a TaskResult by providing handlers for both cases.
45
- */
46
- TaskResult.fold = (onErr, onOk) => (data) => Task.map(Result.fold(onErr, onOk))(data);
47
- /**
48
- * Pattern matches on a TaskResult, returning a Task of the result.
49
- */
50
- TaskResult.match = (cases) => (data) => Task.map(Result.match(cases))(data);
51
- /**
52
- * Recovers from an error by providing a fallback TaskResult.
53
- * The fallback can produce a different success type, widening the result to `TaskResult<E, A | B>`.
54
- */
55
- TaskResult.recover = (fallback) => (data) => Task.chain((result) => Result.isErr(result) ? fallback(result.error) : Task.resolve(result))(data);
56
- /**
57
- * Returns the success value or a default value if the TaskResult is an error.
58
- * The default can be a different type, widening the result to `Task<A | B>`.
59
- */
60
- TaskResult.getOrElse = (defaultValue) => (data) => Task.map(Result.getOrElse(defaultValue))(data);
61
- /**
62
- * Executes a side effect on the success value without changing the TaskResult.
63
- * Useful for logging or debugging.
64
- */
65
- TaskResult.tap = (f) => (data) => Task.map(Result.tap(f))(data);
66
- /**
67
- * Re-runs a TaskResult on `Err` with configurable attempts, backoff, and retry condition.
68
- *
69
- * @param options.attempts - Total number of attempts (1 = no retry, 3 = up to 3 tries)
70
- * @param options.backoff - Fixed delay in ms, or a function `(attempt) => ms` for computed delay
71
- * @param options.when - Only retry when this returns true; defaults to always retry on Err
72
- *
73
- * @example
74
- * ```ts
75
- * // Retry up to 3 times with exponential backoff
76
- * pipe(
77
- * fetchUser,
78
- * TaskResult.retry({ attempts: 3, backoff: n => n * 1000 })
79
- * );
80
- *
81
- * // Only retry on network errors, not auth errors
82
- * pipe(
83
- * fetchUser,
84
- * TaskResult.retry({ attempts: 3, when: e => e instanceof NetworkError })
85
- * );
86
- * ```
87
- */
88
- TaskResult.retry = (options) => (data) => Task.from(() => {
89
- const { attempts, backoff, when: shouldRetry } = options;
90
- const getDelay = (n) => backoff === undefined ? 0 : typeof backoff === "function" ? backoff(n) : backoff;
91
- const run = (left) => Deferred.toPromise(data()).then((result) => {
92
- if (Result.isOk(result))
93
- return result;
94
- if (left <= 1)
95
- return result;
96
- if (shouldRetry !== undefined && !shouldRetry(result.error)) {
97
- return result;
98
- }
99
- const ms = getDelay(attempts - left + 1);
100
- return (ms > 0 ? new Promise((r) => setTimeout(r, ms)) : Promise.resolve()).then(() => run(left - 1));
101
- });
102
- return run(attempts);
103
- });
104
- /**
105
- * Fails a TaskResult with a typed error if it does not resolve within the given time.
106
- *
107
- * @example
108
- * ```ts
109
- * pipe(
110
- * fetchUser,
111
- * TaskResult.timeout(5000, () => new TimeoutError("fetch user timed out"))
112
- * );
113
- * ```
114
- */
115
- TaskResult.timeout = (ms, onTimeout) => (data) => Task.from(() => {
116
- let timerId;
117
- return Promise.race([
118
- Deferred.toPromise(data()).then((result) => {
119
- clearTimeout(timerId);
120
- return result;
121
- }),
122
- new Promise((resolve) => {
123
- timerId = setTimeout(() => resolve(Result.err(onTimeout())), ms);
124
- }),
125
- ]);
126
- });
127
- })(TaskResult || (TaskResult = {}));
@@ -1,128 +0,0 @@
1
- import { Deferred } from "./Deferred.js";
2
- import { Task } from "./Task.js";
3
- import { Validation } from "./Validation.js";
4
- export var TaskValidation;
5
- (function (TaskValidation) {
6
- /**
7
- * Wraps a value in a valid TaskValidation.
8
- */
9
- TaskValidation.valid = (value) => Task.resolve(Validation.valid(value));
10
- /**
11
- * Creates a failed TaskValidation with a single error.
12
- */
13
- TaskValidation.invalid = (error) => Task.resolve(Validation.invalid(error));
14
- /**
15
- * Creates an invalid TaskValidation from multiple errors.
16
- */
17
- TaskValidation.invalidAll = (errors) => Task.resolve(Validation.invalidAll(errors));
18
- /**
19
- * Lifts a Validation into a TaskValidation.
20
- */
21
- TaskValidation.fromValidation = (validation) => Task.resolve(validation);
22
- /**
23
- * Creates a TaskValidation from a Promise-returning function.
24
- * Catches any errors and transforms them using the onError function.
25
- *
26
- * @example
27
- * ```ts
28
- * const fetchUser = (id: string): TaskValidation<string, User> =>
29
- * TaskValidation.tryCatch(
30
- * () => fetch(`/users/${id}`).then(r => r.json()),
31
- * e => `Failed to fetch user: ${e}`
32
- * );
33
- * ```
34
- */
35
- TaskValidation.tryCatch = (f, onError) => Task.from(() => f()
36
- .then((Validation.valid))
37
- .catch((e) => Validation.invalid(onError(e))));
38
- /**
39
- * Transforms the success value inside a TaskValidation.
40
- */
41
- TaskValidation.map = (f) => (data) => Task.map(Validation.map(f))(data);
42
- /**
43
- * Applies a function wrapped in a TaskValidation to a value wrapped in a
44
- * TaskValidation. Both Tasks run in parallel and errors from both sides
45
- * are accumulated.
46
- *
47
- * @example
48
- * ```ts
49
- * pipe(
50
- * TaskValidation.valid((name: string) => (age: number) => ({ name, age })),
51
- * TaskValidation.ap(validateName(name)),
52
- * TaskValidation.ap(validateAge(age))
53
- * )();
54
- * ```
55
- */
56
- TaskValidation.ap = (arg) => (data) => Task.from(() => Promise.all([
57
- Deferred.toPromise(data()),
58
- Deferred.toPromise(arg()),
59
- ]).then(([vf, va]) => Validation.ap(va)(vf)));
60
- /**
61
- * Extracts a value from a TaskValidation by providing handlers for both cases.
62
- */
63
- TaskValidation.fold = (onInvalid, onValid) => (data) => Task.map(Validation.fold(onInvalid, onValid))(data);
64
- /**
65
- * Pattern matches on a TaskValidation, returning a Task of the result.
66
- *
67
- * @example
68
- * ```ts
69
- * pipe(
70
- * validateForm(input),
71
- * TaskValidation.match({
72
- * valid: data => save(data),
73
- * invalid: errors => showErrors(errors)
74
- * })
75
- * )();
76
- * ```
77
- */
78
- TaskValidation.match = (cases) => (data) => Task.map(Validation.match(cases))(data);
79
- /**
80
- * Returns the success value or a default value if the TaskValidation is invalid.
81
- * The default can be a different type, widening the result to `Task<A | B>`.
82
- */
83
- TaskValidation.getOrElse = (defaultValue) => (data) => Task.map(Validation.getOrElse(defaultValue))(data);
84
- /**
85
- * Executes a side effect on the success value without changing the TaskValidation.
86
- * Useful for logging or debugging.
87
- */
88
- TaskValidation.tap = (f) => (data) => Task.map(Validation.tap(f))(data);
89
- /**
90
- * Recovers from an Invalid state by providing a fallback TaskValidation.
91
- * The fallback receives the accumulated error list so callers can inspect which errors occurred.
92
- * The fallback can produce a different success type, widening the result to `TaskValidation<E, A | B>`.
93
- */
94
- TaskValidation.recover = (fallback) => (data) => Task.chain((validation) => Validation.isValid(validation) ? Task.resolve(validation) : fallback(validation.errors))(data);
95
- /**
96
- * Runs two TaskValidations concurrently and combines their results into a tuple.
97
- * If both are Valid, returns Valid with both values. If either fails, accumulates
98
- * errors from both sides.
99
- *
100
- * @example
101
- * ```ts
102
- * await TaskValidation.product(
103
- * validateName(form.name),
104
- * validateAge(form.age),
105
- * )(); // Valid(["Alice", 30]) or Invalid([...errors])
106
- * ```
107
- */
108
- TaskValidation.product = (first, second) => Task.from(() => Promise.all([
109
- Deferred.toPromise(first()),
110
- Deferred.toPromise(second()),
111
- ]).then(([va, vb]) => Validation.product(va, vb)));
112
- /**
113
- * Runs all TaskValidations concurrently and collects results.
114
- * If all are Valid, returns Valid with all values as an array.
115
- * If any fail, returns Invalid with all accumulated errors.
116
- *
117
- * @example
118
- * ```ts
119
- * await TaskValidation.productAll([
120
- * validateName(form.name),
121
- * validateEmail(form.email),
122
- * validateAge(form.age),
123
- * ])(); // Valid([name, email, age]) or Invalid([...all errors])
124
- * ```
125
- */
126
- TaskValidation.productAll = (data) => Task.from(() => Promise.all(data.map((t) => Deferred.toPromise(t())))
127
- .then((results) => Validation.productAll(results)));
128
- })(TaskValidation || (TaskValidation = {}));