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