@nlozgachev/pipelined 0.6.4

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 (144) hide show
  1. package/README.md +85 -0
  2. package/esm/mod.js +3 -0
  3. package/esm/package.json +3 -0
  4. package/esm/src/Composition/compose.js +3 -0
  5. package/esm/src/Composition/converge.js +3 -0
  6. package/esm/src/Composition/curry.js +42 -0
  7. package/esm/src/Composition/flip.js +20 -0
  8. package/esm/src/Composition/flow.js +8 -0
  9. package/esm/src/Composition/fn.js +85 -0
  10. package/esm/src/Composition/index.js +13 -0
  11. package/esm/src/Composition/juxt.js +3 -0
  12. package/esm/src/Composition/memoize.js +66 -0
  13. package/esm/src/Composition/not.js +25 -0
  14. package/esm/src/Composition/on.js +12 -0
  15. package/esm/src/Composition/pipe.js +3 -0
  16. package/esm/src/Composition/tap.js +33 -0
  17. package/esm/src/Composition/uncurry.js +32 -0
  18. package/esm/src/Core/Arr.js +463 -0
  19. package/esm/src/Core/Deferred.js +26 -0
  20. package/esm/src/Core/InternalTypes.js +1 -0
  21. package/esm/src/Core/Lens.js +98 -0
  22. package/esm/src/Core/Option.js +186 -0
  23. package/esm/src/Core/Optional.js +160 -0
  24. package/esm/src/Core/Reader.js +134 -0
  25. package/esm/src/Core/Rec.js +167 -0
  26. package/esm/src/Core/RemoteData.js +206 -0
  27. package/esm/src/Core/Result.js +164 -0
  28. package/esm/src/Core/Task.js +187 -0
  29. package/esm/src/Core/TaskOption.js +105 -0
  30. package/esm/src/Core/TaskResult.js +125 -0
  31. package/esm/src/Core/TaskValidation.js +101 -0
  32. package/esm/src/Core/These.js +241 -0
  33. package/esm/src/Core/Validation.js +214 -0
  34. package/esm/src/Core/index.js +15 -0
  35. package/esm/src/Types/Brand.js +28 -0
  36. package/esm/src/Types/NonEmptyList.js +14 -0
  37. package/esm/src/Types/index.js +2 -0
  38. package/package.json +61 -0
  39. package/script/mod.js +19 -0
  40. package/script/package.json +3 -0
  41. package/script/src/Composition/compose.js +6 -0
  42. package/script/src/Composition/converge.js +6 -0
  43. package/script/src/Composition/curry.js +48 -0
  44. package/script/src/Composition/flip.js +24 -0
  45. package/script/src/Composition/flow.js +11 -0
  46. package/script/src/Composition/fn.js +98 -0
  47. package/script/src/Composition/index.js +29 -0
  48. package/script/src/Composition/juxt.js +6 -0
  49. package/script/src/Composition/memoize.js +71 -0
  50. package/script/src/Composition/not.js +29 -0
  51. package/script/src/Composition/on.js +16 -0
  52. package/script/src/Composition/pipe.js +6 -0
  53. package/script/src/Composition/tap.js +37 -0
  54. package/script/src/Composition/uncurry.js +38 -0
  55. package/script/src/Core/Arr.js +466 -0
  56. package/script/src/Core/Deferred.js +29 -0
  57. package/script/src/Core/InternalTypes.js +2 -0
  58. package/script/src/Core/Lens.js +101 -0
  59. package/script/src/Core/Option.js +189 -0
  60. package/script/src/Core/Optional.js +163 -0
  61. package/script/src/Core/Reader.js +137 -0
  62. package/script/src/Core/Rec.js +170 -0
  63. package/script/src/Core/RemoteData.js +209 -0
  64. package/script/src/Core/Result.js +167 -0
  65. package/script/src/Core/Task.js +190 -0
  66. package/script/src/Core/TaskOption.js +108 -0
  67. package/script/src/Core/TaskResult.js +128 -0
  68. package/script/src/Core/TaskValidation.js +104 -0
  69. package/script/src/Core/These.js +244 -0
  70. package/script/src/Core/Validation.js +217 -0
  71. package/script/src/Core/index.js +31 -0
  72. package/script/src/Types/Brand.js +31 -0
  73. package/script/src/Types/NonEmptyList.js +18 -0
  74. package/script/src/Types/index.js +18 -0
  75. package/types/mod.d.ts +4 -0
  76. package/types/mod.d.ts.map +1 -0
  77. package/types/src/Composition/compose.d.ts +33 -0
  78. package/types/src/Composition/compose.d.ts.map +1 -0
  79. package/types/src/Composition/converge.d.ts +21 -0
  80. package/types/src/Composition/converge.d.ts.map +1 -0
  81. package/types/src/Composition/curry.d.ts +43 -0
  82. package/types/src/Composition/curry.d.ts.map +1 -0
  83. package/types/src/Composition/flip.d.ts +21 -0
  84. package/types/src/Composition/flip.d.ts.map +1 -0
  85. package/types/src/Composition/flow.d.ts +56 -0
  86. package/types/src/Composition/flow.d.ts.map +1 -0
  87. package/types/src/Composition/fn.d.ts +76 -0
  88. package/types/src/Composition/fn.d.ts.map +1 -0
  89. package/types/src/Composition/index.d.ts +14 -0
  90. package/types/src/Composition/index.d.ts.map +1 -0
  91. package/types/src/Composition/juxt.d.ts +18 -0
  92. package/types/src/Composition/juxt.d.ts.map +1 -0
  93. package/types/src/Composition/memoize.d.ts +46 -0
  94. package/types/src/Composition/memoize.d.ts.map +1 -0
  95. package/types/src/Composition/not.d.ts +26 -0
  96. package/types/src/Composition/not.d.ts.map +1 -0
  97. package/types/src/Composition/on.d.ts +13 -0
  98. package/types/src/Composition/on.d.ts.map +1 -0
  99. package/types/src/Composition/pipe.d.ts +56 -0
  100. package/types/src/Composition/pipe.d.ts.map +1 -0
  101. package/types/src/Composition/tap.d.ts +31 -0
  102. package/types/src/Composition/tap.d.ts.map +1 -0
  103. package/types/src/Composition/uncurry.d.ts +54 -0
  104. package/types/src/Composition/uncurry.d.ts.map +1 -0
  105. package/types/src/Core/Arr.d.ts +355 -0
  106. package/types/src/Core/Arr.d.ts.map +1 -0
  107. package/types/src/Core/Deferred.d.ts +49 -0
  108. package/types/src/Core/Deferred.d.ts.map +1 -0
  109. package/types/src/Core/InternalTypes.d.ts +20 -0
  110. package/types/src/Core/InternalTypes.d.ts.map +1 -0
  111. package/types/src/Core/Lens.d.ts +118 -0
  112. package/types/src/Core/Lens.d.ts.map +1 -0
  113. package/types/src/Core/Option.d.ts +205 -0
  114. package/types/src/Core/Option.d.ts.map +1 -0
  115. package/types/src/Core/Optional.d.ts +158 -0
  116. package/types/src/Core/Optional.d.ts.map +1 -0
  117. package/types/src/Core/Reader.d.ts +156 -0
  118. package/types/src/Core/Reader.d.ts.map +1 -0
  119. package/types/src/Core/Rec.d.ts +121 -0
  120. package/types/src/Core/Rec.d.ts.map +1 -0
  121. package/types/src/Core/RemoteData.d.ts +192 -0
  122. package/types/src/Core/RemoteData.d.ts.map +1 -0
  123. package/types/src/Core/Result.d.ts +176 -0
  124. package/types/src/Core/Result.d.ts.map +1 -0
  125. package/types/src/Core/Task.d.ts +189 -0
  126. package/types/src/Core/Task.d.ts.map +1 -0
  127. package/types/src/Core/TaskOption.d.ts +120 -0
  128. package/types/src/Core/TaskOption.d.ts.map +1 -0
  129. package/types/src/Core/TaskResult.d.ts +117 -0
  130. package/types/src/Core/TaskResult.d.ts.map +1 -0
  131. package/types/src/Core/TaskValidation.d.ts +119 -0
  132. package/types/src/Core/TaskValidation.d.ts.map +1 -0
  133. package/types/src/Core/These.d.ts +221 -0
  134. package/types/src/Core/These.d.ts.map +1 -0
  135. package/types/src/Core/Validation.d.ts +213 -0
  136. package/types/src/Core/Validation.d.ts.map +1 -0
  137. package/types/src/Core/index.d.ts +16 -0
  138. package/types/src/Core/index.d.ts.map +1 -0
  139. package/types/src/Types/Brand.d.ts +52 -0
  140. package/types/src/Types/Brand.d.ts.map +1 -0
  141. package/types/src/Types/NonEmptyList.d.ts +29 -0
  142. package/types/src/Types/NonEmptyList.d.ts.map +1 -0
  143. package/types/src/Types/index.d.ts +3 -0
  144. package/types/src/Types/index.d.ts.map +1 -0
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RemoteData = void 0;
4
+ var RemoteData;
5
+ (function (RemoteData) {
6
+ /**
7
+ * Creates a NotAsked RemoteData.
8
+ */
9
+ RemoteData.notAsked = () => ({ kind: "NotAsked" });
10
+ /**
11
+ * Creates a Loading RemoteData.
12
+ */
13
+ RemoteData.loading = () => ({ kind: "Loading" });
14
+ /**
15
+ * Creates a Failure RemoteData with the given error.
16
+ */
17
+ RemoteData.failure = (error) => ({
18
+ kind: "Failure",
19
+ error,
20
+ });
21
+ /**
22
+ * Creates a Success RemoteData with the given value.
23
+ */
24
+ RemoteData.success = (value) => ({
25
+ kind: "Success",
26
+ value,
27
+ });
28
+ /**
29
+ * Type guard that checks if a RemoteData is NotAsked.
30
+ */
31
+ RemoteData.isNotAsked = (data) => data.kind === "NotAsked";
32
+ /**
33
+ * Type guard that checks if a RemoteData is Loading.
34
+ */
35
+ RemoteData.isLoading = (data) => data.kind === "Loading";
36
+ /**
37
+ * Type guard that checks if a RemoteData is Failure.
38
+ */
39
+ RemoteData.isFailure = (data) => data.kind === "Failure";
40
+ /**
41
+ * Type guard that checks if a RemoteData is Success.
42
+ */
43
+ RemoteData.isSuccess = (data) => data.kind === "Success";
44
+ /**
45
+ * Transforms the success value inside a RemoteData.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * pipe(RemoteData.success(5), RemoteData.map(n => n * 2)); // Success(10)
50
+ * pipe(RemoteData.loading(), RemoteData.map(n => n * 2)); // Loading
51
+ * ```
52
+ */
53
+ RemoteData.map = (f) => (data) => RemoteData.isSuccess(data) ? RemoteData.success(f(data.value)) : data;
54
+ /**
55
+ * Transforms the error value inside a RemoteData.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * pipe(RemoteData.failure("oops"), RemoteData.mapError(e => e.toUpperCase())); // Failure("OOPS")
60
+ * ```
61
+ */
62
+ RemoteData.mapError = (f) => (data) => RemoteData.isFailure(data) ? RemoteData.failure(f(data.error)) : data;
63
+ /**
64
+ * Chains RemoteData computations. If the input is Success, passes the value to f.
65
+ * Otherwise, propagates the current state.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * pipe(
70
+ * RemoteData.success(5),
71
+ * RemoteData.chain(n => n > 0 ? RemoteData.success(n) : RemoteData.failure("negative"))
72
+ * );
73
+ * ```
74
+ */
75
+ RemoteData.chain = (f) => (data) => RemoteData.isSuccess(data) ? f(data.value) : data;
76
+ /**
77
+ * Applies a function wrapped in a RemoteData to a value wrapped in a RemoteData.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const add = (a: number) => (b: number) => a + b;
82
+ * pipe(
83
+ * RemoteData.success(add),
84
+ * RemoteData.ap(RemoteData.success(5)),
85
+ * RemoteData.ap(RemoteData.success(3))
86
+ * ); // Success(8)
87
+ * ```
88
+ */
89
+ RemoteData.ap = (arg) => (data) => {
90
+ if (RemoteData.isSuccess(data) && RemoteData.isSuccess(arg)) {
91
+ return RemoteData.success(data.value(arg.value));
92
+ }
93
+ if (RemoteData.isFailure(data))
94
+ return data;
95
+ if (RemoteData.isFailure(arg))
96
+ return arg;
97
+ if (RemoteData.isLoading(data) || RemoteData.isLoading(arg))
98
+ return RemoteData.loading();
99
+ return RemoteData.notAsked();
100
+ };
101
+ /**
102
+ * Extracts the value from a RemoteData by providing handlers for all four cases.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * pipe(
107
+ * userData,
108
+ * RemoteData.fold(
109
+ * () => "Not asked",
110
+ * () => "Loading...",
111
+ * e => `Error: ${e}`,
112
+ * value => `Got: ${value}`
113
+ * )
114
+ * );
115
+ * ```
116
+ */
117
+ RemoteData.fold = (onNotAsked, onLoading, onFailure, onSuccess) => (data) => {
118
+ switch (data.kind) {
119
+ case "NotAsked":
120
+ return onNotAsked();
121
+ case "Loading":
122
+ return onLoading();
123
+ case "Failure":
124
+ return onFailure(data.error);
125
+ case "Success":
126
+ return onSuccess(data.value);
127
+ }
128
+ };
129
+ /**
130
+ * Pattern matches on a RemoteData, returning the result of the matching case.
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * pipe(
135
+ * userData,
136
+ * RemoteData.match({
137
+ * notAsked: () => "Click to load",
138
+ * loading: () => "Loading...",
139
+ * failure: e => `Error: ${e}`,
140
+ * success: user => `Hello, ${user.name}!`
141
+ * })
142
+ * );
143
+ * ```
144
+ */
145
+ RemoteData.match = (cases) => (data) => {
146
+ switch (data.kind) {
147
+ case "NotAsked":
148
+ return cases.notAsked();
149
+ case "Loading":
150
+ return cases.loading();
151
+ case "Failure":
152
+ return cases.failure(data.error);
153
+ case "Success":
154
+ return cases.success(data.value);
155
+ }
156
+ };
157
+ /**
158
+ * Returns the success value or a default value if the RemoteData is not Success.
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * pipe(RemoteData.success(5), RemoteData.getOrElse(0)); // 5
163
+ * pipe(RemoteData.loading(), RemoteData.getOrElse(0)); // 0
164
+ * ```
165
+ */
166
+ RemoteData.getOrElse = (defaultValue) => (data) => RemoteData.isSuccess(data) ? data.value : defaultValue;
167
+ /**
168
+ * Executes a side effect on the success value without changing the RemoteData.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * pipe(
173
+ * RemoteData.success(5),
174
+ * RemoteData.tap(n => console.log("Value:", n)),
175
+ * RemoteData.map(n => n * 2)
176
+ * );
177
+ * ```
178
+ */
179
+ RemoteData.tap = (f) => (data) => {
180
+ if (RemoteData.isSuccess(data))
181
+ f(data.value);
182
+ return data;
183
+ };
184
+ /**
185
+ * Recovers from a Failure state by providing a fallback RemoteData.
186
+ */
187
+ RemoteData.recover = (fallback) => (data) => RemoteData.isFailure(data) ? fallback(data.error) : data;
188
+ /**
189
+ * Converts a RemoteData to an Option.
190
+ * Success becomes Some, all other states become None.
191
+ */
192
+ RemoteData.toOption = (data) => RemoteData.isSuccess(data) ? { kind: "Some", value: data.value } : { kind: "None" };
193
+ /**
194
+ * Converts a RemoteData to a Result.
195
+ * Success becomes Ok, Failure becomes Err.
196
+ * NotAsked and Loading become Err with the provided fallback error.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * pipe(
201
+ * RemoteData.success(42),
202
+ * RemoteData.toResult(() => "not loaded")
203
+ * ); // Ok(42)
204
+ * ```
205
+ */
206
+ RemoteData.toResult = (onNotReady) => (data) => RemoteData.isSuccess(data)
207
+ ? { kind: "Ok", value: data.value }
208
+ : { kind: "Error", error: RemoteData.isFailure(data) ? data.error : onNotReady() };
209
+ })(RemoteData || (exports.RemoteData = RemoteData = {}));
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Result = void 0;
4
+ var Result;
5
+ (function (Result) {
6
+ /**
7
+ * Creates a successful Result with the given value.
8
+ */
9
+ Result.ok = (value) => ({ kind: "Ok", value });
10
+ /**
11
+ * Creates a failed Result with the given error.
12
+ */
13
+ Result.err = (error) => ({ kind: "Error", error });
14
+ /**
15
+ * Type guard that checks if an Result is Ok.
16
+ */
17
+ Result.isOk = (data) => data.kind === "Ok";
18
+ /**
19
+ * Type guard that checks if an Result is Err.
20
+ */
21
+ Result.isErr = (data) => data.kind === "Error";
22
+ /**
23
+ * Creates an Result from a function that may throw.
24
+ * Catches any errors and transforms them using the onError function.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const parseJson = (s: string): Result<string, unknown> =>
29
+ * Result.tryCatch(
30
+ * () => JSON.parse(s),
31
+ * (e) => `Parse error: ${e}`
32
+ * );
33
+ * ```
34
+ */
35
+ Result.tryCatch = (f, onError) => {
36
+ try {
37
+ return Result.ok(f());
38
+ }
39
+ catch (e) {
40
+ return Result.err(onError(e));
41
+ }
42
+ };
43
+ /**
44
+ * Transforms the success value inside an Result.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * pipe(Result.ok(5), Result.map(n => n * 2)); // Ok(10)
49
+ * pipe(Result.err("error"), Result.map(n => n * 2)); // Err("error")
50
+ * ```
51
+ */
52
+ Result.map = (f) => (data) => Result.isOk(data) ? Result.ok(f(data.value)) : data;
53
+ /**
54
+ * Transforms the error value inside an Result.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * pipe(Result.err("oops"), Result.mapError(e => e.toUpperCase())); // Err("OOPS")
59
+ * ```
60
+ */
61
+ Result.mapError = (f) => (data) => Result.isErr(data) ? Result.err(f(data.error)) : data;
62
+ /**
63
+ * Chains Result computations. If the first is Ok, passes the value to f.
64
+ * If the first is Err, propagates the error.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * const validatePositive = (n: number): Result<string, number> =>
69
+ * n > 0 ? Result.ok(n) : Result.err("Must be positive");
70
+ *
71
+ * pipe(Result.ok(5), Result.chain(validatePositive)); // Ok(5)
72
+ * pipe(Result.ok(-1), Result.chain(validatePositive)); // Err("Must be positive")
73
+ * ```
74
+ */
75
+ Result.chain = (f) => (data) => Result.isOk(data) ? f(data.value) : data;
76
+ /**
77
+ * Extracts the value from an Result by providing handlers for both cases.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * pipe(
82
+ * Result.ok(5),
83
+ * Result.fold(
84
+ * e => `Error: ${e}`,
85
+ * n => `Value: ${n}`
86
+ * )
87
+ * ); // "Value: 5"
88
+ * ```
89
+ */
90
+ Result.fold = (onErr, onOk) => (data) => Result.isOk(data) ? onOk(data.value) : onErr(data.error);
91
+ /**
92
+ * Pattern matches on a Result, returning the result of the matching case.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * pipe(
97
+ * result,
98
+ * Result.match({
99
+ * ok: value => `Got ${value}`,
100
+ * err: error => `Failed: ${error}`
101
+ * })
102
+ * );
103
+ * ```
104
+ */
105
+ Result.match = (cases) => (data) => Result.isOk(data) ? cases.ok(data.value) : cases.err(data.error);
106
+ /**
107
+ * Returns the success value or a default value if the Result is an error.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * pipe(Result.ok(5), Result.getOrElse(0)); // 5
112
+ * pipe(Result.err("error"), Result.getOrElse(0)); // 0
113
+ * ```
114
+ */
115
+ Result.getOrElse = (defaultValue) => (data) => Result.isOk(data) ? data.value : defaultValue;
116
+ /**
117
+ * Executes a side effect on the success value without changing the Result.
118
+ * Useful for logging or debugging.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * pipe(
123
+ * Result.ok(5),
124
+ * Result.tap(n => console.log("Value:", n)),
125
+ * Result.map(n => n * 2)
126
+ * );
127
+ * ```
128
+ */
129
+ Result.tap = (f) => (data) => {
130
+ if (Result.isOk(data))
131
+ f(data.value);
132
+ return data;
133
+ };
134
+ /**
135
+ * Recovers from an error by providing a fallback Result.
136
+ */
137
+ Result.recover = (fallback) => (data) => Result.isOk(data) ? data : fallback();
138
+ /**
139
+ * Recovers from an error unless it matches the blocked error.
140
+ */
141
+ Result.recoverUnless = (blockedErr, fallback) => (data) => Result.isErr(data) && data.error !== blockedErr ? fallback() : data;
142
+ /**
143
+ * Converts a Result to an Option.
144
+ * Ok becomes Some, Err becomes None (the error is discarded).
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * Result.toOption(Result.ok(42)); // Some(42)
149
+ * Result.toOption(Result.err("oops")); // None
150
+ * ```
151
+ */
152
+ Result.toOption = (data) => Result.isOk(data) ? { kind: "Some", value: data.value } : { kind: "None" };
153
+ /**
154
+ * Applies a function wrapped in an Result to a value wrapped in an Result.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * const add = (a: number) => (b: number) => a + b;
159
+ * pipe(
160
+ * Result.ok(add),
161
+ * Result.ap(Result.ok(5)),
162
+ * Result.ap(Result.ok(3))
163
+ * ); // Ok(8)
164
+ * ```
165
+ */
166
+ Result.ap = (arg) => (data) => Result.isOk(data) && Result.isOk(arg) ? Result.ok(data.value(arg.value)) : Result.isErr(data) ? data : arg;
167
+ })(Result || (exports.Result = Result = {}));
@@ -0,0 +1,190 @@
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
+ * Converts a `Task<A>` into a `Task<Result<E, A>>`, resolving to `Err` if the
167
+ * Task does not complete within the given time.
168
+ *
169
+ * @example
170
+ * ```ts
171
+ * pipe(
172
+ * heavyComputation,
173
+ * Task.timeout(5000, () => "timed out"),
174
+ * TaskResult.chain(processResult)
175
+ * );
176
+ * ```
177
+ */
178
+ Task.timeout = (ms, onTimeout) => (task) => Task.from(() => {
179
+ let timerId;
180
+ return Promise.race([
181
+ toPromise(task).then((a) => {
182
+ clearTimeout(timerId);
183
+ return Result_js_1.Result.ok(a);
184
+ }),
185
+ new Promise((resolve) => {
186
+ timerId = setTimeout(() => resolve(Result_js_1.Result.err(onTimeout())), ms);
187
+ }),
188
+ ]);
189
+ });
190
+ })(Task || (exports.Task = Task = {}));
@@ -0,0 +1,108 @@
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
+ */
86
+ TaskOption.getOrElse = (defaultValue) => (data) => Task_js_1.Task.map(Option_js_1.Option.getOrElse(defaultValue))(data);
87
+ /**
88
+ * Executes a side effect on the value without changing the TaskOption.
89
+ * Useful for logging or debugging.
90
+ */
91
+ TaskOption.tap = (f) => (data) => Task_js_1.Task.map(Option_js_1.Option.tap(f))(data);
92
+ /**
93
+ * Filters the value inside a TaskOption. Returns None if the predicate fails.
94
+ */
95
+ TaskOption.filter = (predicate) => (data) => Task_js_1.Task.map(Option_js_1.Option.filter(predicate))(data);
96
+ /**
97
+ * Converts a TaskOption to a TaskResult, using onNone to produce the error value.
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * pipe(
102
+ * findUser("123"),
103
+ * TaskOption.toTaskResult(() => "User not found")
104
+ * );
105
+ * ```
106
+ */
107
+ TaskOption.toTaskResult = (onNone) => (data) => Task_js_1.Task.map(Option_js_1.Option.toResult(onNone))(data);
108
+ })(TaskOption || (exports.TaskOption = TaskOption = {}));