@shirudo/result 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/dist/collections.cjs +181 -9
- package/dist/collections.cjs.map +1 -0
- package/dist/collections.d.cts +101 -3
- package/dist/collections.d.cts.map +1 -0
- package/dist/collections.d.mts +101 -3
- package/dist/collections.d.mts.map +1 -0
- package/dist/collections.mjs +175 -3
- package/dist/collections.mjs.map +1 -0
- package/dist/errors-CVgC7NII.cjs +278 -0
- package/dist/errors-CVgC7NII.cjs.map +1 -0
- package/dist/errors-MuakEcnM.mjs +146 -0
- package/dist/errors-MuakEcnM.mjs.map +1 -0
- package/dist/errors.cjs +22 -130
- package/dist/errors.d.cts +9 -9
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.mts +9 -9
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +2 -109
- package/dist/index.cjs +99 -69
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +87 -74
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +87 -74
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +64 -34
- package/dist/index.mjs.map +1 -1
- package/dist/operators.cjs +412 -31
- package/dist/operators.cjs.map +1 -0
- package/dist/operators.d.cts +211 -3
- package/dist/operators.d.cts.map +1 -0
- package/dist/operators.d.mts +211 -3
- package/dist/operators.d.mts.map +1 -0
- package/dist/operators.mjs +384 -3
- package/dist/operators.mjs.map +1 -0
- package/dist/{result-q9Na2bGa.mjs → result-DQCpkuOJ.mjs} +36 -17
- package/dist/result-DQCpkuOJ.mjs.map +1 -0
- package/dist/{result-BBOGxvSH.cjs → result-DoqQufvR.cjs} +36 -17
- package/dist/result-DoqQufvR.cjs.map +1 -0
- package/dist/{sequence-DDwePRLd.d.cts → sequence-CmugKPbu.d.cts} +105 -84
- package/dist/sequence-CmugKPbu.d.cts.map +1 -0
- package/dist/{sequence-DDwePRLd.d.mts → sequence-CmugKPbu.d.mts} +105 -84
- package/dist/sequence-CmugKPbu.d.mts.map +1 -0
- package/package.json +20 -12
- package/dist/errors.cjs.map +0 -1
- package/dist/errors.mjs.map +0 -1
- package/dist/flatten-B_XIkaOK.cjs +0 -208
- package/dist/flatten-B_XIkaOK.cjs.map +0 -1
- package/dist/flatten-C7D6Kttf.d.mts +0 -81
- package/dist/flatten-C7D6Kttf.d.mts.map +0 -1
- package/dist/flatten-ChTL5BfA.mjs +0 -167
- package/dist/flatten-ChTL5BfA.mjs.map +0 -1
- package/dist/flatten-D0K8UcQH.d.cts +0 -81
- package/dist/flatten-D0K8UcQH.d.cts.map +0 -1
- package/dist/result-BBOGxvSH.cjs.map +0 -1
- package/dist/result-q9Na2bGa.mjs.map +0 -1
- package/dist/sequence-DDwePRLd.d.cts.map +0 -1
- package/dist/sequence-DDwePRLd.d.mts.map +0 -1
- package/dist/swap-BnyMBdD_.cjs +0 -552
- package/dist/swap-BnyMBdD_.cjs.map +0 -1
- package/dist/swap-CFD2g1cB.mjs +0 -385
- package/dist/swap-CFD2g1cB.mjs.map +0 -1
- package/dist/swap-CrHQZIax.d.cts +0 -212
- package/dist/swap-CrHQZIax.d.cts.map +0 -1
- package/dist/swap-DT4LdRFV.d.mts +0 -212
- package/dist/swap-DT4LdRFV.d.mts.map +0 -1
package/dist/operators.mjs
CHANGED
|
@@ -1,4 +1,385 @@
|
|
|
1
|
-
import { m as zip, p as combine } from "./result-
|
|
2
|
-
import {
|
|
1
|
+
import { c as ok, i as err, m as zip, p as combine } from "./result-DQCpkuOJ.mjs";
|
|
2
|
+
import { p as InvalidResultStateError } from "./errors-MuakEcnM.mjs";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
//#region src/core/map.ts
|
|
5
|
+
/**
|
|
6
|
+
* Transforms the value (Ok case).
|
|
7
|
+
* Corresponds to Rust `map`.
|
|
8
|
+
*/
|
|
9
|
+
function map(project) {
|
|
10
|
+
return (source) => {
|
|
11
|
+
if (source.isOk()) return ok(project(source.value));
|
|
12
|
+
if (source.isErr()) return source;
|
|
13
|
+
throw new InvalidResultStateError("map");
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/core/mapErr.ts
|
|
19
|
+
/**
|
|
20
|
+
* Transforms the error (Err case).
|
|
21
|
+
* Corresponds to Rust `map_err`.
|
|
22
|
+
*/
|
|
23
|
+
function mapErr(project) {
|
|
24
|
+
return (source) => {
|
|
25
|
+
if (source.isErr()) return err(project(source.error));
|
|
26
|
+
if (source.isOk()) return source;
|
|
27
|
+
throw new InvalidResultStateError("mapErr");
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/core/mapBoth.ts
|
|
33
|
+
/**
|
|
34
|
+
* Transforms both the Ok value and the Err error.
|
|
35
|
+
* Corresponds to FP `bimap` / `mapBoth`.
|
|
36
|
+
*/
|
|
37
|
+
function mapBoth(mapOk, mapErr) {
|
|
38
|
+
return (source) => {
|
|
39
|
+
if (source.isOk()) return ok(mapOk(source.value));
|
|
40
|
+
if (source.isErr()) return err(mapErr(source.error));
|
|
41
|
+
throw new InvalidResultStateError("mapBoth");
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Alias for `mapBoth`.
|
|
46
|
+
*/
|
|
47
|
+
const bimap = mapBoth;
|
|
48
|
+
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/core/flatMap.ts
|
|
51
|
+
/**
|
|
52
|
+
* Chains another operation that returns a Result.
|
|
53
|
+
* Corresponds to Rust `and_then` or JS `flatMap`.
|
|
54
|
+
*/
|
|
55
|
+
function flatMap(project) {
|
|
56
|
+
return (source) => {
|
|
57
|
+
if (source.isOk()) return project(source.value);
|
|
58
|
+
if (source.isErr()) return source;
|
|
59
|
+
throw new InvalidResultStateError("flatMap");
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/core/tap.ts
|
|
65
|
+
/**
|
|
66
|
+
* Executes a side effect (logging, debugging) without changing the Result.
|
|
67
|
+
* Corresponds to Rust `inspect` / `inspect_err`.
|
|
68
|
+
*/
|
|
69
|
+
function tap(observer) {
|
|
70
|
+
return (source) => {
|
|
71
|
+
if (source.isOk()) {
|
|
72
|
+
if (observer.ok) observer.ok(source.value);
|
|
73
|
+
return source;
|
|
74
|
+
}
|
|
75
|
+
if (source.isErr()) {
|
|
76
|
+
if (observer.err) observer.err(source.error);
|
|
77
|
+
return source;
|
|
78
|
+
}
|
|
79
|
+
throw new InvalidResultStateError("tap");
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/core/filter.ts
|
|
85
|
+
/**
|
|
86
|
+
* Checks a condition. If false, the Result becomes Err.
|
|
87
|
+
* Corresponds to Rust `filter` (partially).
|
|
88
|
+
*/
|
|
89
|
+
function filter(predicate, errorFn) {
|
|
90
|
+
return (source) => {
|
|
91
|
+
if (source.isOk()) {
|
|
92
|
+
if (predicate(source.value)) return source;
|
|
93
|
+
return err(errorFn());
|
|
94
|
+
}
|
|
95
|
+
if (source.isErr()) return source;
|
|
96
|
+
throw new InvalidResultStateError("filter");
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/core/fold.ts
|
|
102
|
+
/**
|
|
103
|
+
* Folds the Result into a single value by applying one of two functions.
|
|
104
|
+
* The end of the pipe.
|
|
105
|
+
*
|
|
106
|
+
* This is the pipe operator equivalent of the `.fold()` instance method.
|
|
107
|
+
*/
|
|
108
|
+
function fold(handlers) {
|
|
109
|
+
return (source) => {
|
|
110
|
+
if (source.isOk()) return handlers.ok(source.value);
|
|
111
|
+
if (source.isErr()) return handlers.err(source.error);
|
|
112
|
+
throw new InvalidResultStateError("fold");
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/core/tryCatch.ts
|
|
118
|
+
function tryCatch(fn, errorMapper) {
|
|
119
|
+
return (source) => {
|
|
120
|
+
if (source.isErr()) return source;
|
|
121
|
+
try {
|
|
122
|
+
return ok(fn());
|
|
123
|
+
} catch (error) {
|
|
124
|
+
return err(errorMapper ? errorMapper(error) : error);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/core/tryMap.ts
|
|
131
|
+
function tryMap(project, errorMapper) {
|
|
132
|
+
return (source) => {
|
|
133
|
+
if (source.isErr()) return source;
|
|
134
|
+
if (!source.isOk()) throw new InvalidResultStateError("tryMap");
|
|
135
|
+
try {
|
|
136
|
+
return ok(project(source.value));
|
|
137
|
+
} catch (error) {
|
|
138
|
+
return err(errorMapper ? errorMapper(error) : error);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/core/recover.ts
|
|
145
|
+
/**
|
|
146
|
+
* Recover: converts Err to Ok(defaultValue).
|
|
147
|
+
* Result is guaranteed to be Ok → Error type becomes `never`.
|
|
148
|
+
*/
|
|
149
|
+
function recover(defaultValue) {
|
|
150
|
+
return (source) => {
|
|
151
|
+
if (source.isOk()) return source;
|
|
152
|
+
if (source.isErr()) return ok(defaultValue);
|
|
153
|
+
throw new InvalidResultStateError("recover");
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Like `recover`, but calculates the default value based on the error.
|
|
158
|
+
*/
|
|
159
|
+
function recoverWith(fn) {
|
|
160
|
+
return (source) => {
|
|
161
|
+
if (source.isOk()) return source;
|
|
162
|
+
if (source.isErr()) return ok(fn(source.error));
|
|
163
|
+
throw new InvalidResultStateError("recoverWith");
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/core/match.ts
|
|
169
|
+
/**
|
|
170
|
+
* Resolves the Result. The end of the pipe.
|
|
171
|
+
* Corresponds to Rust `match`.
|
|
172
|
+
*/
|
|
173
|
+
function match(handlers) {
|
|
174
|
+
return (source) => {
|
|
175
|
+
if (source.isOk()) return handlers.ok(source.value);
|
|
176
|
+
if (source.isErr()) return handlers.err(source.error);
|
|
177
|
+
throw new InvalidResultStateError("match");
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region src/core/mapAsync.ts
|
|
183
|
+
/**
|
|
184
|
+
* Async version of map.
|
|
185
|
+
*/
|
|
186
|
+
function mapAsync(project) {
|
|
187
|
+
return async (source) => {
|
|
188
|
+
if (source.isOk()) return ok(await project(source.value));
|
|
189
|
+
if (source.isErr()) return source;
|
|
190
|
+
throw new InvalidResultStateError("mapAsync");
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/core/mapErrAsync.ts
|
|
196
|
+
/**
|
|
197
|
+
* Async version of mapErr.
|
|
198
|
+
*/
|
|
199
|
+
function mapErrAsync(project) {
|
|
200
|
+
return async (source) => {
|
|
201
|
+
if (source.isErr()) return err(await project(source.error));
|
|
202
|
+
if (source.isOk()) return source;
|
|
203
|
+
throw new InvalidResultStateError("mapErrAsync");
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/core/flatMapAsync.ts
|
|
209
|
+
/**
|
|
210
|
+
* Async version of flatMap.
|
|
211
|
+
*/
|
|
212
|
+
function flatMapAsync(project) {
|
|
213
|
+
return async (source) => {
|
|
214
|
+
if (source.isOk()) return await project(source.value);
|
|
215
|
+
if (source.isErr()) return source;
|
|
216
|
+
throw new InvalidResultStateError("flatMapAsync");
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/core/tapAsync.ts
|
|
222
|
+
/**
|
|
223
|
+
* Async version of tap.
|
|
224
|
+
*/
|
|
225
|
+
function tapAsync(observer) {
|
|
226
|
+
return async (source) => {
|
|
227
|
+
if (source.isOk()) {
|
|
228
|
+
if (observer.ok) await observer.ok(source.value);
|
|
229
|
+
return source;
|
|
230
|
+
}
|
|
231
|
+
if (source.isErr()) {
|
|
232
|
+
if (observer.err) await observer.err(source.error);
|
|
233
|
+
return source;
|
|
234
|
+
}
|
|
235
|
+
throw new InvalidResultStateError("tapAsync");
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/core/filterAsync.ts
|
|
241
|
+
/**
|
|
242
|
+
* Async version of filter.
|
|
243
|
+
*/
|
|
244
|
+
function filterAsync(predicate, errorFn) {
|
|
245
|
+
return async (source) => {
|
|
246
|
+
if (source.isOk()) {
|
|
247
|
+
if (await predicate(source.value)) return source;
|
|
248
|
+
return err(await errorFn());
|
|
249
|
+
}
|
|
250
|
+
if (source.isErr()) return source;
|
|
251
|
+
throw new InvalidResultStateError("filterAsync");
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
//#endregion
|
|
256
|
+
//#region src/core/foldAsync.ts
|
|
257
|
+
/**
|
|
258
|
+
* Async version of `fold`. Folds the Result into a single value asynchronously.
|
|
259
|
+
* The end of the pipe.
|
|
260
|
+
*
|
|
261
|
+
* This is the async pipe operator equivalent of the `.fold()` instance method.
|
|
262
|
+
*/
|
|
263
|
+
function foldAsync(handlers) {
|
|
264
|
+
return async (source) => {
|
|
265
|
+
if (source.isOk()) return await handlers.ok(source.value);
|
|
266
|
+
if (source.isErr()) return await handlers.err(source.error);
|
|
267
|
+
throw new InvalidResultStateError("foldAsync");
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
//#endregion
|
|
272
|
+
//#region src/core/tryCatchAsync.ts
|
|
273
|
+
function tryCatchAsync(fn, errorMapper) {
|
|
274
|
+
return async (source) => {
|
|
275
|
+
if (source.isErr()) return source;
|
|
276
|
+
try {
|
|
277
|
+
return ok(await fn());
|
|
278
|
+
} catch (error) {
|
|
279
|
+
return err(errorMapper ? errorMapper(error) : error);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
//#endregion
|
|
285
|
+
//#region src/core/tryMapAsync.ts
|
|
286
|
+
function tryMapAsync(project, errorMapper) {
|
|
287
|
+
return async (source) => {
|
|
288
|
+
if (source.isErr()) return source;
|
|
289
|
+
if (!source.isOk()) throw new InvalidResultStateError("tryMapAsync");
|
|
290
|
+
try {
|
|
291
|
+
return ok(await project(source.value));
|
|
292
|
+
} catch (error) {
|
|
293
|
+
return err(errorMapper ? errorMapper(error) : error);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
//#endregion
|
|
299
|
+
//#region src/core/matchAsync.ts
|
|
300
|
+
/**
|
|
301
|
+
* Async version of match.
|
|
302
|
+
*/
|
|
303
|
+
function matchAsync(handlers) {
|
|
304
|
+
return async (source) => {
|
|
305
|
+
if (source.isOk()) return await handlers.ok(source.value);
|
|
306
|
+
if (source.isErr()) return await handlers.err(source.error);
|
|
307
|
+
throw new InvalidResultStateError("matchAsync");
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
//#endregion
|
|
312
|
+
//#region src/core/and.ts
|
|
313
|
+
/**
|
|
314
|
+
* Combines two Results. Returns the second one only if the first is Ok.
|
|
315
|
+
* Corresponds to Rust `and`.
|
|
316
|
+
*/
|
|
317
|
+
function and(result, other) {
|
|
318
|
+
if (result.isOk()) return other;
|
|
319
|
+
if (result.isErr()) return result;
|
|
320
|
+
throw new InvalidResultStateError("and");
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
//#endregion
|
|
324
|
+
//#region src/core/or.ts
|
|
325
|
+
/**
|
|
326
|
+
* Fallback to another Result if the first is Err.
|
|
327
|
+
* Corresponds to Rust `or`.
|
|
328
|
+
*/
|
|
329
|
+
function or(result, other) {
|
|
330
|
+
if (result.isOk()) return result;
|
|
331
|
+
if (result.isErr()) return other;
|
|
332
|
+
throw new InvalidResultStateError("or");
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
//#endregion
|
|
336
|
+
//#region src/core/orElse.ts
|
|
337
|
+
/**
|
|
338
|
+
* Fallback with a function that returns a Result.
|
|
339
|
+
* Corresponds to Rust `or_else`.
|
|
340
|
+
*/
|
|
341
|
+
function orElse(result, fn) {
|
|
342
|
+
if (result.isOk()) return result;
|
|
343
|
+
if (result.isErr()) return fn(result.error);
|
|
344
|
+
throw new InvalidResultStateError("orElse");
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
//#endregion
|
|
348
|
+
//#region src/core/mapOr.ts
|
|
349
|
+
/**
|
|
350
|
+
* Transforms the value or returns a default value.
|
|
351
|
+
* Corresponds to Rust `map_or`.
|
|
352
|
+
*/
|
|
353
|
+
function mapOr(result, defaultValue, fn) {
|
|
354
|
+
if (result.isOk()) return fn(result.value);
|
|
355
|
+
if (result.isErr()) return defaultValue;
|
|
356
|
+
throw new InvalidResultStateError("mapOr");
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
//#endregion
|
|
360
|
+
//#region src/core/mapOrElse.ts
|
|
361
|
+
/**
|
|
362
|
+
* Transforms the value or calculates a default value using a function.
|
|
363
|
+
* Corresponds to Rust `map_or_else`.
|
|
364
|
+
*/
|
|
365
|
+
function mapOrElse(result, defaultFn, fn) {
|
|
366
|
+
if (result.isOk()) return fn(result.value);
|
|
367
|
+
if (result.isErr()) return defaultFn(result.error);
|
|
368
|
+
throw new InvalidResultStateError("mapOrElse");
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/core/swap.ts
|
|
373
|
+
/**
|
|
374
|
+
* Swaps Ok and Err.
|
|
375
|
+
* Result<T, E> → Result<E, T>
|
|
376
|
+
*/
|
|
377
|
+
function swap(result) {
|
|
378
|
+
if (result.isOk()) return err(result.value);
|
|
379
|
+
if (result.isErr()) return ok(result.error);
|
|
380
|
+
throw new InvalidResultStateError("swap");
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
//#endregion
|
|
384
|
+
export { and, bimap, combine, filter, filterAsync, flatMap, flatMapAsync, fold, foldAsync, map, mapAsync, mapBoth, mapErr, mapErrAsync, mapOr, mapOrElse, match, matchAsync, or, orElse, recover, recoverWith, swap, tap, tapAsync, tryCatch, tryCatchAsync, tryMap, tryMapAsync, zip };
|
|
385
|
+
//# sourceMappingURL=operators.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operators.mjs","names":[],"sources":["../src/core/map.ts","../src/core/mapErr.ts","../src/core/mapBoth.ts","../src/core/flatMap.ts","../src/core/tap.ts","../src/core/filter.ts","../src/core/fold.ts","../src/core/tryCatch.ts","../src/core/tryMap.ts","../src/core/recover.ts","../src/core/match.ts","../src/core/mapAsync.ts","../src/core/mapErrAsync.ts","../src/core/flatMapAsync.ts","../src/core/tapAsync.ts","../src/core/filterAsync.ts","../src/core/foldAsync.ts","../src/core/tryCatchAsync.ts","../src/core/tryMapAsync.ts","../src/core/matchAsync.ts","../src/core/and.ts","../src/core/or.ts","../src/core/orElse.ts","../src/core/mapOr.ts","../src/core/mapOrElse.ts","../src/core/swap.ts"],"sourcesContent":["import type { Result } from './result';\nimport { ok } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Transforms the value (Ok case).\n * Corresponds to Rust `map`.\n */\nexport function map<T, E, U>(project: (value: T) => U) {\n return (source: Result<T, E>): Result<U, E> => {\n if (source.isOk()) {\n return ok(project(source.value));\n }\n if (source.isErr()) return source as unknown as Result<U, E>;\n throw new InvalidResultStateError('map');\n };\n}\n","import type { Result } from './result';\nimport { err } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Transforms the error (Err case).\n * Corresponds to Rust `map_err`.\n */\nexport function mapErr<T, E, F>(project: (error: E) => F) {\n return (source: Result<T, E>): Result<T, F> => {\n if (source.isErr()) {\n return err(project(source.error));\n }\n if (source.isOk()) return source as unknown as Result<T, F>;\n throw new InvalidResultStateError('mapErr');\n };\n}\n","import type { Result } from './result';\nimport { err, ok } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Transforms both the Ok value and the Err error.\n * Corresponds to FP `bimap` / `mapBoth`.\n */\nexport function mapBoth<T, E, U, F>(mapOk: (value: T) => U, mapErr: (error: E) => F) {\n return (source: Result<T, E>): Result<U, F> => {\n if (source.isOk()) {\n return ok<U, F>(mapOk(source.value));\n }\n if (source.isErr()) {\n return err<F, U>(mapErr(source.error));\n }\n throw new InvalidResultStateError('mapBoth');\n };\n}\n\n/**\n * Alias for `mapBoth`.\n */\nexport const bimap: typeof mapBoth = mapBoth;\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Chains another operation that returns a Result.\n * Corresponds to Rust `and_then` or JS `flatMap`.\n */\nexport function flatMap<T, E, U, F>(project: (value: T) => Result<U, F>) {\n return (source: Result<T, E>): Result<U, E | F> => {\n if (source.isOk()) {\n return project(source.value);\n }\n if (source.isErr()) return source as unknown as Result<U, E | F>;\n throw new InvalidResultStateError('flatMap');\n };\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Executes a side effect (logging, debugging) without changing the Result.\n * Corresponds to Rust `inspect` / `inspect_err`.\n */\nexport function tap<T, E>(observer: { ok?: (val: T) => void; err?: (e: E) => void }) {\n return (source: Result<T, E>): Result<T, E> => {\n if (source.isOk()) {\n if (observer.ok) observer.ok(source.value);\n return source;\n }\n if (source.isErr()) {\n if (observer.err) observer.err(source.error);\n return source;\n }\n throw new InvalidResultStateError('tap');\n };\n}\n","import type { Result } from './result';\nimport { err } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Checks a condition. If false, the Result becomes Err.\n * Corresponds to Rust `filter` (partially).\n */\nexport function filter<T, E>(predicate: (val: T) => boolean, errorFn: () => E) {\n return (source: Result<T, E>): Result<T, E> => {\n if (source.isOk()) {\n if (predicate(source.value)) {\n return source;\n }\n return err(errorFn());\n }\n if (source.isErr()) return source;\n throw new InvalidResultStateError('filter');\n };\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Folds the Result into a single value by applying one of two functions.\n * The end of the pipe.\n *\n * This is the pipe operator equivalent of the `.fold()` instance method.\n */\nexport function fold<T, E, R>(handlers: { ok: (val: T) => R; err: (e: E) => R }) {\n return (source: Result<T, E>): R => {\n if (source.isOk()) {\n return handlers.ok(source.value);\n }\n if (source.isErr()) return handlers.err(source.error);\n throw new InvalidResultStateError('fold');\n };\n}\n","import type { Result } from './result';\nimport { ok, err } from './result';\n\n/**\n * Executes a function and catches exceptions.\n * Converts exceptions into Result<E>.\n * Corresponds to Rust `Result::from` for fallible operations.\n */\nexport function tryCatch<T>(fn: () => T): <SE>(source: Result<unknown, SE>) => Result<T, unknown>;\nexport function tryCatch<T, E>(\n fn: () => T,\n errorMapper: (error: unknown) => E\n): <SE>(source: Result<unknown, SE>) => Result<T, SE | E>;\nexport function tryCatch<T, E>(\n fn: () => T,\n errorMapper?: (error: unknown) => E\n): <SE>(source: Result<unknown, SE>) => Result<T, SE | E> {\n return <SE>(source: Result<unknown, SE>): Result<T, SE | E> => {\n if (source.isErr()) return source as unknown as Result<T, SE | E>;\n\n try {\n return ok<T, SE | E>(fn());\n } catch (error) {\n return err<SE | E, T>(errorMapper ? errorMapper(error) : error as E);\n }\n };\n}\n","import type { Result } from './result';\nimport { ok, err } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Like `map`, but catches exceptions and converts them to Err.\n */\nexport function tryMap<T, E, U>(project: (value: T) => U): (source: Result<T, E>) => Result<U, unknown>;\nexport function tryMap<T, E, U, F>(\n project: (value: T) => U,\n errorMapper: (error: unknown) => F\n): (source: Result<T, E>) => Result<U, E | F>;\nexport function tryMap<T, E, U, F>(\n project: (value: T) => U,\n errorMapper?: (error: unknown) => F\n): (source: Result<T, E>) => Result<U, E | F> {\n return (source: Result<T, E>): Result<U, E | F> => {\n if (source.isErr()) {\n return source as unknown as Result<U, E | F>;\n }\n if (!source.isOk()) throw new InvalidResultStateError('tryMap');\n\n try {\n return ok<U, E | F>(project(source.value));\n } catch (error) {\n return err<E | F, U>(errorMapper ? errorMapper(error) : error as F);\n }\n };\n}\n","import type { Result } from './result';\nimport { ok } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Recover: converts Err to Ok(defaultValue).\n * Result is guaranteed to be Ok → Error type becomes `never`.\n */\nexport function recover<T, E, F>(defaultValue: F) {\n return (source: Result<T, E>): Result<T | F, never> => {\n if (source.isOk()) return source as unknown as Result<T | F, never>;\n if (source.isErr()) return ok(defaultValue);\n throw new InvalidResultStateError('recover');\n };\n}\n\n/**\n * Like `recover`, but calculates the default value based on the error.\n */\nexport function recoverWith<T, E, F>(fn: (error: E) => F) {\n return (source: Result<T, E>): Result<T | F, never> => {\n if (source.isOk()) return source as unknown as Result<T | F, never>;\n if (source.isErr()) return ok(fn(source.error));\n throw new InvalidResultStateError('recoverWith');\n };\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Resolves the Result. The end of the pipe.\n * Corresponds to Rust `match`.\n */\nexport function match<T, E, R>(handlers: { ok: (val: T) => R; err: (e: E) => R }) {\n return (source: Result<T, E>): R => {\n if (source.isOk()) {\n return handlers.ok(source.value);\n }\n if (source.isErr()) return handlers.err(source.error);\n throw new InvalidResultStateError('match');\n };\n}\n","import type { Result } from './result';\nimport { ok } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Async version of map.\n */\nexport function mapAsync<T, E, U>(project: (value: T) => Promise<U>) {\n return async (source: Result<T, E>): Promise<Result<U, E>> => {\n if (source.isOk()) {\n return ok(await project(source.value));\n }\n if (source.isErr()) return source as unknown as Result<U, E>;\n throw new InvalidResultStateError('mapAsync');\n };\n}\n","import type { Result } from './result';\nimport { err } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Async version of mapErr.\n */\nexport function mapErrAsync<T, E, F>(project: (error: E) => Promise<F>) {\n return async (source: Result<T, E>): Promise<Result<T, F>> => {\n if (source.isErr()) {\n return err(await project(source.error));\n }\n if (source.isOk()) return source as unknown as Result<T, F>;\n throw new InvalidResultStateError('mapErrAsync');\n };\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Async version of flatMap.\n */\nexport function flatMapAsync<T, E, U, F>(project: (value: T) => Promise<Result<U, F>>) {\n return async (source: Result<T, E>): Promise<Result<U, E | F>> => {\n if (source.isOk()) {\n return await project(source.value);\n }\n if (source.isErr()) return source as unknown as Result<U, E | F>;\n throw new InvalidResultStateError('flatMapAsync');\n };\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Async version of tap.\n */\nexport function tapAsync<T, E>(observer: { ok?: (val: T) => Promise<void>; err?: (e: E) => Promise<void> }) {\n return async (source: Result<T, E>): Promise<Result<T, E>> => {\n if (source.isOk()) {\n if (observer.ok) await observer.ok(source.value);\n return source;\n }\n if (source.isErr()) {\n if (observer.err) await observer.err(source.error);\n return source;\n }\n throw new InvalidResultStateError('tapAsync');\n };\n}\n","import type { Result } from './result';\nimport { err } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Async version of filter.\n */\nexport function filterAsync<T, E>(predicate: (val: T) => Promise<boolean>, errorFn: () => Promise<E>) {\n return async (source: Result<T, E>): Promise<Result<T, E>> => {\n if (source.isOk()) {\n if (await predicate(source.value)) {\n return source;\n }\n return err(await errorFn());\n }\n if (source.isErr()) return source;\n throw new InvalidResultStateError('filterAsync');\n };\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Async version of `fold`. Folds the Result into a single value asynchronously.\n * The end of the pipe.\n *\n * This is the async pipe operator equivalent of the `.fold()` instance method.\n */\nexport function foldAsync<T, E, R>(handlers: { ok: (val: T) => Promise<R>; err: (e: E) => Promise<R> }) {\n return async (source: Result<T, E>): Promise<R> => {\n if (source.isOk()) {\n return await handlers.ok(source.value);\n }\n if (source.isErr()) return await handlers.err(source.error);\n throw new InvalidResultStateError('foldAsync');\n };\n}\n","import type { Result } from './result';\nimport { ok, err } from './result';\n\n/**\n * Async version of tryCatch.\n */\nexport function tryCatchAsync<T>(fn: () => Promise<T>): <SE>(source: Result<unknown, SE>) => Promise<Result<T, unknown>>;\nexport function tryCatchAsync<T, E>(\n fn: () => Promise<T>,\n errorMapper: (error: unknown) => E\n): <SE>(source: Result<unknown, SE>) => Promise<Result<T, SE | E>>;\nexport function tryCatchAsync<T, E>(\n fn: () => Promise<T>,\n errorMapper?: (error: unknown) => E\n): <SE>(source: Result<unknown, SE>) => Promise<Result<T, SE | E>> {\n return async <SE>(source: Result<unknown, SE>): Promise<Result<T, SE | E>> => {\n if (source.isErr()) return source as unknown as Result<T, SE | E>;\n\n try {\n return ok<T, SE | E>(await fn());\n } catch (error) {\n return err<SE | E, T>(errorMapper ? errorMapper(error) : error as E);\n }\n };\n}\n","import type { Result } from './result';\nimport { ok, err } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Async version of tryMap.\n */\nexport function tryMapAsync<T, E, U>(project: (value: T) => Promise<U>): (source: Result<T, E>) => Promise<Result<U, unknown>>;\nexport function tryMapAsync<T, E, U, F>(\n project: (value: T) => Promise<U>,\n errorMapper: (error: unknown) => F\n): (source: Result<T, E>) => Promise<Result<U, E | F>>;\nexport function tryMapAsync<T, E, U, F>(\n project: (value: T) => Promise<U>,\n errorMapper?: (error: unknown) => F\n): (source: Result<T, E>) => Promise<Result<U, E | F>> {\n return async (source: Result<T, E>): Promise<Result<U, E | F>> => {\n if (source.isErr()) {\n return source as unknown as Result<U, E | F>;\n }\n if (!source.isOk()) throw new InvalidResultStateError('tryMapAsync');\n\n try {\n return ok<U, E | F>(await project(source.value));\n } catch (error) {\n return err<E | F, U>(errorMapper ? errorMapper(error) : error as F);\n }\n };\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Async version of match.\n */\nexport function matchAsync<T, E, R>(handlers: { ok: (val: T) => Promise<R>; err: (e: E) => Promise<R> }) {\n return async (source: Result<T, E>): Promise<R> => {\n if (source.isOk()) {\n return await handlers.ok(source.value);\n }\n if (source.isErr()) return await handlers.err(source.error);\n throw new InvalidResultStateError('matchAsync');\n };\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Combines two Results. Returns the second one only if the first is Ok.\n * Corresponds to Rust `and`.\n */\nexport function and<T, E, U>(result: Result<T, E>, other: Result<U, E>): Result<U, E> {\n if (result.isOk()) return other;\n if (result.isErr()) return result as unknown as Result<U, E>;\n throw new InvalidResultStateError('and');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Fallback to another Result if the first is Err.\n * Corresponds to Rust `or`.\n */\nexport function or<T, E, F>(result: Result<T, E>, other: Result<T, F>): Result<T, F> {\n if (result.isOk()) return result as unknown as Result<T, F>;\n if (result.isErr()) return other;\n throw new InvalidResultStateError('or');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Fallback with a function that returns a Result.\n * Corresponds to Rust `or_else`.\n */\nexport function orElse<T, E, F>(result: Result<T, E>, fn: (error: E) => Result<T, F>): Result<T, F> {\n if (result.isOk()) return result as unknown as Result<T, F>;\n if (result.isErr()) return fn(result.error);\n throw new InvalidResultStateError('orElse');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Transforms the value or returns a default value.\n * Corresponds to Rust `map_or`.\n */\nexport function mapOr<T, E, U>(result: Result<T, E>, defaultValue: U, fn: (value: T) => U): U {\n if (result.isOk()) return fn(result.value);\n if (result.isErr()) return defaultValue;\n throw new InvalidResultStateError('mapOr');\n}\n","import type { Result } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Transforms the value or calculates a default value using a function.\n * Corresponds to Rust `map_or_else`.\n */\nexport function mapOrElse<T, E, U>(result: Result<T, E>, defaultFn: (error: E) => U, fn: (value: T) => U): U {\n if (result.isOk()) return fn(result.value);\n if (result.isErr()) return defaultFn(result.error);\n throw new InvalidResultStateError('mapOrElse');\n}\n","import type { Result } from './result';\nimport { err, ok } from './result';\nimport { InvalidResultStateError } from '../errors';\n\n/**\n * Swaps Ok and Err.\n * Result<T, E> → Result<E, T>\n */\nexport function swap<T, E>(result: Result<T, E>): Result<E, T> {\n if (result.isOk()) {\n return err<T, E>(result.value);\n }\n if (result.isErr()) {\n return ok<E, T>(result.error);\n }\n throw new InvalidResultStateError('swap');\n}\n"],"mappings":";;;;;;;;AAQA,SAAgB,IAAa,SAA0B;CACnD,QAAQ,WAAuC;EAC3C,IAAI,OAAO,KAAK,GACZ,OAAO,GAAG,QAAQ,OAAO,KAAK,CAAC;EAEnC,IAAI,OAAO,MAAM,GAAG,OAAO;EAC3B,MAAM,IAAI,wBAAwB,KAAK;CAC3C;AACJ;;;;;;;;ACRA,SAAgB,OAAgB,SAA0B;CACtD,QAAQ,WAAuC;EAC3C,IAAI,OAAO,MAAM,GACb,OAAO,IAAI,QAAQ,OAAO,KAAK,CAAC;EAEpC,IAAI,OAAO,KAAK,GAAG,OAAO;EAC1B,MAAM,IAAI,wBAAwB,QAAQ;CAC9C;AACJ;;;;;;;;ACRA,SAAgB,QAAoB,OAAwB,QAAyB;CACjF,QAAQ,WAAuC;EAC3C,IAAI,OAAO,KAAK,GACZ,OAAO,GAAS,MAAM,OAAO,KAAK,CAAC;EAEvC,IAAI,OAAO,MAAM,GACb,OAAO,IAAU,OAAO,OAAO,KAAK,CAAC;EAEzC,MAAM,IAAI,wBAAwB,SAAS;CAC/C;AACJ;;;;AAKA,MAAa,QAAwB;;;;;;;;AChBrC,SAAgB,Q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AM,GAAG,OAAO;CAC3B,MAAM,IAAI,wBAAwB,OAAO;AAC7C;;;;;;;;ACJA,SAAgB,UAAmB,QAAsB,WAA4B,IAAwB;CACzG,IAAI,OAAO,KAAK,GAAG,OAAO,GAAG,OAAO,KAAK;CACzC,IAAI,OAAO,MAAM,GAAG,OAAO,UAAU,OAAO,KAAK;CACjD,MAAM,IAAI,wBAAwB,WAAW;AACjD;;;;;;;;ACHA,SAAgB,KAAW,QAAoC;CAC3D,IAAI,OAAO,KAAK,GACZ,OAAO,IAAU,OAAO,KAAK;CAEjC,IAAI,OAAO,MAAM,GACb,OAAO,GAAS,OAAO,KAAK;CAEhC,MAAM,IAAI,wBAAwB,MAAM;AAC5C"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ExpectErrError, ExpectOkError,
|
|
1
|
+
import { S as describeErrorMessage, b as UnwrapErrOnOkError, d as ExpectErrError, f as ExpectOkError, g as MatchTagMissingHandlerError, h as MatchOnOkError, m as MatchErrHandlerNotResultError, p as InvalidResultStateError, x as UnwrapOnErrError } from "./errors-MuakEcnM.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/core/pipeable.ts
|
|
4
4
|
var Pipeable = class {
|
|
@@ -60,7 +60,9 @@ function matchTag(result, key, handlers) {
|
|
|
60
60
|
*
|
|
61
61
|
* - `.when(Ctor, handler)` matches via `instanceof`
|
|
62
62
|
* - `.whenGuard(guard, handler)` matches via Type-Guard
|
|
63
|
-
* - `.run()` is only allowed if all error cases have been handled (`E` has been reduced to `never`)
|
|
63
|
+
* - `.run()` is only allowed if all error cases have been handled (`E` has been reduced to `never`).
|
|
64
|
+
* The reduction is structural: two error classes of the same shape count as one case, so each
|
|
65
|
+
* class needs a distinguishing member, for example a literal `readonly code`.
|
|
64
66
|
*/
|
|
65
67
|
var ErrorMatchBuilder = class ErrorMatchBuilder {
|
|
66
68
|
#error;
|
|
@@ -172,7 +174,8 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
172
174
|
when(ctor, handler) {
|
|
173
175
|
if (this.#resolved) return this;
|
|
174
176
|
if (this.#error instanceof ctor) {
|
|
175
|
-
const
|
|
177
|
+
const out = handler(this.#error);
|
|
178
|
+
const resolved = expectResultReturn(out, "when");
|
|
176
179
|
return new ErrMatchBuilder(this.#error, resolved);
|
|
177
180
|
}
|
|
178
181
|
return this;
|
|
@@ -181,7 +184,8 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
181
184
|
if (this.#resolved) return this;
|
|
182
185
|
const error = this.#error;
|
|
183
186
|
if (guard(error)) {
|
|
184
|
-
const
|
|
187
|
+
const out = handler(error);
|
|
188
|
+
const resolved = expectResultReturn(out, "whenGuard");
|
|
185
189
|
return new ErrMatchBuilder(this.#error, resolved);
|
|
186
190
|
}
|
|
187
191
|
return this;
|
|
@@ -189,14 +193,16 @@ var ErrMatchBuilder = class ErrMatchBuilder {
|
|
|
189
193
|
whenTag(key, tag, handler) {
|
|
190
194
|
if (this.#resolved) return this;
|
|
191
195
|
if (matchesTag(this.#error, key, tag)) {
|
|
192
|
-
const
|
|
196
|
+
const out = handler(this.#error);
|
|
197
|
+
const resolved = expectResultReturn(out, "whenTag");
|
|
193
198
|
return new ErrMatchBuilder(this.#error, resolved);
|
|
194
199
|
}
|
|
195
200
|
return this;
|
|
196
201
|
}
|
|
197
202
|
otherwise(handler) {
|
|
198
203
|
if (this.#resolved) return this.#resolved;
|
|
199
|
-
|
|
204
|
+
const out = handler(this.#error);
|
|
205
|
+
return expectResultReturn(out, "otherwise");
|
|
200
206
|
}
|
|
201
207
|
run() {
|
|
202
208
|
if (this.#resolved) return this.#resolved;
|
|
@@ -244,7 +250,8 @@ var AsyncErrMatchBuilder = class AsyncErrMatchBuilder {
|
|
|
244
250
|
}
|
|
245
251
|
async otherwise(handler) {
|
|
246
252
|
if (this.#resolve) return await this.#invoke();
|
|
247
|
-
|
|
253
|
+
const out = await handler(this.#error);
|
|
254
|
+
return expectResultReturn(out, "otherwise");
|
|
248
255
|
}
|
|
249
256
|
async run() {
|
|
250
257
|
if (this.#resolve) return await this.#invoke();
|
|
@@ -432,12 +439,24 @@ var ResultBase = class extends Pipeable {
|
|
|
432
439
|
return AsyncErrMatchBuilder.fromResult(this);
|
|
433
440
|
}
|
|
434
441
|
/**
|
|
435
|
-
*
|
|
436
|
-
* `{ _tag: 'Ok', value }` / `{ _tag: 'Err', error }
|
|
442
|
+
* Converts the Result into its plain discriminated shape
|
|
443
|
+
* `{ _tag: 'Ok', value }` / `{ _tag: 'Err', error }` (`ResultType<T, E>`),
|
|
444
|
+
* with no prototype, methods, or brand attached.
|
|
437
445
|
*
|
|
438
|
-
* Unlike {@link serialize}, `Ok(undefined)` stays unambiguous,
|
|
439
|
-
*
|
|
440
|
-
* `JSON.stringify
|
|
446
|
+
* Unlike {@link serialize}, `Ok(undefined)` stays unambiguous, because the
|
|
447
|
+
* `_tag` discriminant carries the state. The returned `Ok` object holds
|
|
448
|
+
* the `value` key, and `JSON.stringify` encodes it exactly as it encodes
|
|
449
|
+
* the Result instance, so the shape is safe for JSON, `structuredClone`,
|
|
450
|
+
* and `postMessage`. JSON is the lossy step: `JSON.stringify` drops a key
|
|
451
|
+
* whose value is `undefined`, so `Ok(undefined)` crosses the wire as
|
|
452
|
+
* `{"_tag":"Ok"}`, and `ok(parsed.value)` rebuilds it.
|
|
453
|
+
*
|
|
454
|
+
* To rebuild a Result on the other side, validate the payload with your
|
|
455
|
+
* schema tool and then call `ok`/`err` on the discriminant:
|
|
456
|
+
*
|
|
457
|
+
* ```ts
|
|
458
|
+
* const restored = parsed._tag === 'Ok' ? ok(parsed.value) : err(parsed.error);
|
|
459
|
+
* ```
|
|
441
460
|
*/
|
|
442
461
|
toSerialized() {
|
|
443
462
|
if (this._tag === "Ok") return {
|
|
@@ -456,8 +475,9 @@ var ResultBase = class extends Pipeable {
|
|
|
456
475
|
*
|
|
457
476
|
* @deprecated Use {@link toSerialized} instead: with this format,
|
|
458
477
|
* `Ok(undefined)` is indistinguishable from a missing `data` field, and
|
|
459
|
-
*
|
|
460
|
-
*
|
|
478
|
+
* the shape does not match what `JSON.stringify(result)` produces.
|
|
479
|
+
* `toSerialized()` returns the discriminated `ResultType<T, E>`, which
|
|
480
|
+
* rebuilds unambiguously via `ok`/`err`.
|
|
461
481
|
*/
|
|
462
482
|
serialize() {
|
|
463
483
|
if (this._tag === "Ok") return {
|
|
@@ -480,10 +500,9 @@ var ResultBase = class extends Pipeable {
|
|
|
480
500
|
data: this.value
|
|
481
501
|
};
|
|
482
502
|
if (this._tag !== "Err") throw new InvalidResultStateError("Result.toUserFriendly");
|
|
483
|
-
const error = this.error;
|
|
484
503
|
return {
|
|
485
504
|
isSuccess: false,
|
|
486
|
-
error:
|
|
505
|
+
error: describeErrorMessage(this.error)
|
|
487
506
|
};
|
|
488
507
|
}
|
|
489
508
|
};
|
|
@@ -573,4 +592,4 @@ const Result = {
|
|
|
573
592
|
|
|
574
593
|
//#endregion
|
|
575
594
|
export { matchTag as _, fromNullable as a, ok as c, tryAsync as d, tryFn as f, sequence as g, all as h, err as i, okIf as l, zip as m, Ok as n, fromPromise as o, combine as p, Result as r, fromThrowable as s, Err as t, okIfLazy as u, isResult as v };
|
|
576
|
-
//# sourceMappingURL=result-
|
|
595
|
+
//# sourceMappingURL=result-DQCpkuOJ.mjs.map
|