@pvorona/failable 0.7.0 → 0.9.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/dist/index.js CHANGED
@@ -1,41 +1,56 @@
1
- import { isFunction as h, isObject as u, hasOwnPropertyValue as S, hasOwnKey as g } from "@pvorona/assert";
2
- import { notImplemented as a } from "@pvorona/not-implemented";
3
- const F = /* @__PURE__ */ Symbol("Failable"), A = /* @__PURE__ */ Symbol("Success"), O = /* @__PURE__ */ Symbol("Failure"), o = Object.freeze({
1
+ import { isFunction as b, isObject as c, hasOwnPropertyValue as j, hasOwnKey as I } from "@pvorona/assert";
2
+ import { notImplemented as o } from "@pvorona/not-implemented";
3
+ const P = /* @__PURE__ */ Symbol("Failable"), L = /* @__PURE__ */ Symbol("Success"), _ = /* @__PURE__ */ Symbol("Failure"), n = Object.freeze({
4
4
  Success: "success",
5
5
  Failure: "failure"
6
- }), p = Object.freeze({
6
+ }), x = Object.freeze({
7
7
  mode: "normalized-errors"
8
8
  });
9
- function T(r) {
10
- return u(r) && Object.keys(r).length === 2 && S(r, "status", o.Success) && g(r, "data");
9
+ function D(r) {
10
+ return c(r) && Object.keys(r).length === 2 && j(r, "status", n.Success) && I(r, "data");
11
11
  }
12
- function N(r) {
13
- return u(r) && Object.keys(r).length === 2 && S(r, "status", o.Failure) && g(r, "error");
12
+ function V(r) {
13
+ return c(r) && Object.keys(r).length === 2 && j(r, "status", n.Failure) && I(r, "error");
14
14
  }
15
- function w(r) {
16
- return N(r) || T(r);
15
+ function y(r) {
16
+ return V(r) || D(r);
17
17
  }
18
- const m = {
19
- [F]: !0,
18
+ const G = {
19
+ [P]: !0,
20
20
  isSuccess: !1,
21
21
  isFailure: !1,
22
22
  data: null,
23
23
  error: null,
24
- or: a,
25
- orElse: a,
26
- getOr: a,
27
- getOrElse: a,
28
- getOrThrow: a,
29
- match: a,
24
+ or: o,
25
+ orElse: o,
26
+ getOr: o,
27
+ getOrElse: o,
28
+ getOrThrow: o,
29
+ match: o,
30
+ map: o,
31
+ flatMap: o,
30
32
  [Symbol.iterator]: function() {
31
- return R(this);
33
+ return Q(this);
32
34
  },
33
35
  [Symbol.asyncIterator]: function() {
34
- return I(this);
36
+ return W(this);
35
37
  }
36
- }, U = (() => {
37
- const r = Object.create(m);
38
- return r[A] = !0, r.status = o.Success, r.isSuccess = !0, r.or = function() {
38
+ };
39
+ function O(r, e) {
40
+ return r(e);
41
+ }
42
+ function K(r, e) {
43
+ if (e === void 0)
44
+ return m(r);
45
+ const t = C(e);
46
+ return t === null ? m(r) : r instanceof Error && A(e) ? r : t(r);
47
+ }
48
+ function M(r, e) {
49
+ throw K(r, e);
50
+ }
51
+ const H = (() => {
52
+ const r = Object.create(G);
53
+ return r[L] = !0, r.status = n.Success, r.isSuccess = !0, r.or = function() {
39
54
  return this;
40
55
  }, r.orElse = function() {
41
56
  return this;
@@ -43,248 +58,264 @@ const m = {
43
58
  return this.data;
44
59
  }, r.getOrElse = function() {
45
60
  return this.data;
46
- }, r.getOrThrow = function() {
61
+ }, r.getOrThrow = function(t) {
47
62
  return this.data;
48
63
  }, r.match = function(t) {
49
64
  return t(this.data);
65
+ }, r.map = function(t) {
66
+ return s(t(this.data));
67
+ }, r.flatMap = function(t) {
68
+ return t(this.data);
50
69
  }, Object.freeze(r);
51
- })(), C = (() => {
52
- const r = Object.create(m);
53
- return r[O] = !0, r.status = o.Failure, r.isFailure = !0, r.or = function(t) {
54
- return f(t);
70
+ })(), q = (() => {
71
+ const r = Object.create(G);
72
+ return r[_] = !0, r.status = n.Failure, r.isFailure = !0, r.or = function(t) {
73
+ return s(t);
55
74
  }, r.orElse = function(t) {
56
- return f(t());
75
+ return s(O(t, this.error));
57
76
  }, r.getOr = function(t) {
58
77
  return t;
59
78
  }, r.getOrElse = function(t) {
60
- return t();
61
- }, r.getOrThrow = function() {
62
- throw this.error === void 0 ? new Error("getOrThrow() called on Failure<void> with no error value") : this.error;
63
- }, r.match = function(t, n) {
64
- return n(this.error);
79
+ return O(t, this.error);
80
+ }, r.getOrThrow = function(t) {
81
+ M(this.error, t);
82
+ }, r.match = function(t, u) {
83
+ return u(this.error);
84
+ }, r.map = function() {
85
+ return this;
86
+ }, r.flatMap = function() {
87
+ return this;
65
88
  }, Object.freeze(r);
66
89
  })();
67
- function E(r, e) {
68
- return u(r) ? r[e] === !0 : !1;
90
+ function g(r, e) {
91
+ return c(r) ? r[e] === !0 : !1;
69
92
  }
70
- function c(r) {
71
- return E(r, F);
93
+ function a(r) {
94
+ return g(r, P);
72
95
  }
73
- function cr(r) {
74
- return E(r, A);
96
+ function hr(r) {
97
+ return g(r, L);
75
98
  }
76
- function sr(r) {
77
- return E(r, O);
99
+ function dr(r) {
100
+ return g(r, _);
78
101
  }
79
- function f(r) {
80
- const e = Object.create(U);
102
+ function s(r) {
103
+ const e = Object.create(H);
81
104
  return e.data = r, Object.freeze(e);
82
105
  }
83
- function d(r) {
84
- const e = Object.create(C);
106
+ function l(r) {
107
+ const e = Object.create(q);
85
108
  return e.error = r, Object.freeze(e);
86
109
  }
87
- function ar(r) {
88
- if (r.status === o.Failure) throw r.error;
110
+ function Sr(r, e) {
111
+ r.status === n.Failure && M(r.error, e);
89
112
  }
90
- function fr(r) {
91
- return r.status === o.Failure ? { status: o.Failure, error: r.error } : { status: o.Success, data: r.data };
113
+ function mr(r) {
114
+ return r.status === n.Failure ? { status: n.Failure, error: r.error } : { status: n.Success, data: r.data };
92
115
  }
93
- const M = "`failable(() => ...)` only accepts synchronous callbacks. This callback returned a Promise. Pass the promise directly instead: `await failable(promise)`.", j = /* @__PURE__ */ Symbol("FailablePromiseCallbackGuard"), z = /* @__PURE__ */ Symbol("RunGet");
94
- class s {
95
- [z] = !0;
116
+ const J = "`failable(() => ...)` only accepts synchronous callbacks. This callback returned a Promise. Pass the promise directly instead: `await failable(promise)`.", T = /* @__PURE__ */ Symbol(
117
+ "FailablePromiseCallbackGuard"
118
+ ), Y = /* @__PURE__ */ Symbol("RunGet");
119
+ class f {
120
+ [Y] = !0;
96
121
  source;
97
122
  constructor(e) {
98
123
  this.source = e;
99
124
  }
100
125
  static create(e) {
101
- return new s(e);
126
+ return new f(e);
102
127
  }
103
128
  }
104
- const l = "`run()` generators must use `yield*` only with hydrated sync `Failable` values or with `get(...)`. Use `yield* helper()` for sync hydrated `Failable` helpers and `yield* get(...)` for promised sources.", B = "`run()` generators must return a `Failable` or finish without returning a value.";
105
- function* R(r) {
106
- return yield s.create(r);
129
+ const h = "`run()` generators must use `yield*` only with hydrated `Failable` values. Use `yield* helper()` for sync helpers and `yield* await promisedHelper()` for promised sources.", v = "`run()` generators must return a `Failable` or finish without returning a value.";
130
+ function* Q(r) {
131
+ return yield f.create(r);
107
132
  }
108
- async function* I(r) {
109
- return yield s.create(r);
133
+ async function* W(r) {
134
+ return yield f.create(r);
110
135
  }
111
- function L(r) {
112
- return !u(r) && !h(r) ? !1 : h(r.then);
136
+ function k(r) {
137
+ return !c(r) && !b(r) ? !1 : b(r.then);
138
+ }
139
+ const X = Object.freeze({});
140
+ function E(r) {
141
+ return a(r) ? r : l(r);
142
+ }
143
+ function F(r) {
144
+ for (const e of r)
145
+ if (k(e)) return !0;
146
+ return !1;
147
+ }
148
+ async function N(r) {
149
+ return (await Promise.all(
150
+ r.map((t) => Promise.resolve(t))
151
+ )).map((t) => E(t));
152
+ }
153
+ function p(r) {
154
+ for (const t of r)
155
+ if (t.status === n.Failure)
156
+ return t;
157
+ const e = r.map(
158
+ (t) => t.data
159
+ );
160
+ return s(e);
113
161
  }
114
- function x(r) {
115
- return L(r) ? I(r) : R(r);
162
+ function yr(...r) {
163
+ return F(r) ? N(r).then(
164
+ (e) => p(e)
165
+ ) : p(
166
+ r.map((e) => E(e))
167
+ );
116
168
  }
117
- const D = Object.freeze({
118
- get: x
119
- });
120
- function P(r) {
121
- if (!(r instanceof s))
122
- throw new Error(l);
169
+ function gr(...r) {
170
+ return F(r) ? N(r).then(
171
+ (e) => e
172
+ ) : r.map((e) => E(e));
173
+ }
174
+ function Fr(...r) {
175
+ return r.length === 0 ? Promise.reject(
176
+ new Error("`race()` requires at least one `Failable` source.")
177
+ ) : F(r) ? Promise.race(r.map((e) => Promise.resolve(e))).then(
178
+ (e) => E(e)
179
+ ) : E(r[0]);
180
+ }
181
+ function U(r) {
182
+ if (!(r instanceof f))
183
+ throw new Error(h);
123
184
  const e = r.source;
124
- if (!c(e))
125
- throw new Error(l);
185
+ if (!a(e))
186
+ throw new Error(h);
126
187
  return e;
127
188
  }
128
- async function V(r) {
129
- if (!(r instanceof s))
130
- throw new Error(l);
189
+ async function z(r) {
190
+ if (!(r instanceof f))
191
+ throw new Error(h);
131
192
  const e = await r.source;
132
- if (!c(e))
133
- throw new Error(l);
193
+ if (!a(e))
194
+ throw new Error(h);
134
195
  return e;
135
196
  }
136
- async function _(r) {
137
- if (!(r instanceof s))
138
- throw new Error(l);
139
- let e;
140
- try {
141
- e = await r.source;
142
- } catch (t) {
143
- return { kind: "rejection", rejection: t };
144
- }
145
- if (!c(e))
146
- throw new Error(l);
147
- return { kind: "source", source: e };
148
- }
149
- function K(r, e) {
197
+ function Z(r, e) {
150
198
  let t = r.return(e);
151
199
  for (; !t.done; ) {
152
- const n = P(t.value);
153
- if (n.status === o.Failure) {
200
+ const u = U(t.value);
201
+ if (u.status === n.Failure) {
154
202
  t = r.return(e);
155
203
  continue;
156
204
  }
157
- t = r.next(n.data);
205
+ t = r.next(u.data);
158
206
  }
207
+ return d(t.value);
159
208
  }
160
- async function H(r, e) {
209
+ async function $(r, e) {
161
210
  let t = await r.return(e);
162
211
  for (; !t.done; ) {
163
- const n = await V(t.value);
164
- if (n.status === o.Failure) {
212
+ const u = await z(t.value);
213
+ if (u.status === n.Failure) {
165
214
  t = await r.return(e);
166
215
  continue;
167
216
  }
168
- t = await r.next(n.data);
217
+ t = await r.next(u.data);
169
218
  }
219
+ return d(t.value);
170
220
  }
171
- async function J(r) {
172
- let e = await r.return(void 0);
173
- for (; !e.done; ) {
174
- const t = await _(e.value);
175
- if (t.kind === "rejection") {
176
- e = await r.return(void 0);
177
- continue;
178
- }
179
- const n = t.source;
180
- if (n.status === o.Failure) {
181
- e = await r.return(void 0);
182
- continue;
183
- }
184
- e = await r.next(n.data);
185
- }
186
- }
187
- function k(r) {
188
- if (c(r))
221
+ function d(r) {
222
+ if (a(r))
189
223
  return r;
190
224
  if (r === void 0)
191
- return f();
192
- throw new Error(B);
225
+ return s();
226
+ throw new Error(v);
193
227
  }
194
- function W(r) {
228
+ function rr(r) {
195
229
  return Symbol.asyncIterator in r;
196
230
  }
197
- function Y(r) {
231
+ function er(r) {
198
232
  let e = r.next();
199
233
  for (; !e.done; ) {
200
- const t = P(e.value);
201
- if (t.status === o.Failure)
202
- return K(r, t), t;
234
+ const t = U(e.value);
235
+ if (t.status === n.Failure)
236
+ return Z(r, t);
203
237
  e = r.next(t.data);
204
238
  }
205
- return k(e.value);
239
+ return d(e.value);
206
240
  }
207
- async function q(r) {
241
+ async function tr(r) {
208
242
  let e = await r.next();
209
243
  for (; !e.done; ) {
210
- const t = await _(e.value);
211
- if (t.kind === "rejection")
212
- throw await J(r), t.rejection;
213
- const n = t.source;
214
- if (n.status === o.Failure)
215
- return await H(r, n), n;
216
- e = await r.next(n.data);
244
+ const t = await z(e.value);
245
+ if (t.status === n.Failure)
246
+ return $(r, t);
247
+ e = await r.next(t.data);
217
248
  }
218
- return k(e.value);
249
+ return d(e.value);
219
250
  }
220
- function lr(r) {
221
- const e = r(D);
222
- return W(e) ? q(e) : Y(e);
251
+ function wr(r) {
252
+ const e = r(X);
253
+ return rr(e) ? tr(e) : er(e);
223
254
  }
224
- function dr(r, e) {
225
- return c(r) ? i(r, e) : w(r) ? i(b(r), e) : h(r) ? $(r, e) : v(r, e);
255
+ function Ar(r, e) {
256
+ return a(r) ? i(r, e) : y(r) ? i(w(r), e) : b(r) ? or(r, e) : sr(r, e);
226
257
  }
227
- function b(r) {
228
- return r.status === o.Success ? f(r.data) : d(r.error);
258
+ function w(r) {
259
+ return r.status === n.Success ? s(r.data) : l(r.error);
229
260
  }
230
- function Q() {
261
+ function nr() {
231
262
  const r = new Error(
232
- M
263
+ J
233
264
  );
234
- return Object.defineProperty(r, j, {
265
+ return Object.defineProperty(r, T, {
235
266
  value: !0,
236
267
  enumerable: !1,
237
268
  configurable: !1,
238
269
  writable: !1
239
270
  }), r;
240
271
  }
241
- function X(r) {
272
+ function ur(r) {
242
273
  return r instanceof Error ? Object.getOwnPropertyDescriptor(
243
274
  r,
244
- j
275
+ T
245
276
  )?.value === !0 : !1;
246
277
  }
247
- function Z(r) {
278
+ function ir(r) {
248
279
  Promise.resolve(r).catch(() => {
249
280
  });
250
281
  }
251
- function $(r, e) {
282
+ function or(r, e) {
252
283
  try {
253
284
  const t = r();
254
- return L(t) ? (Z(t), i(
255
- d(Q()),
285
+ return k(t) ? (ir(t), i(
286
+ l(nr()),
256
287
  e
257
- )) : c(t) ? i(t, e) : w(t) ? i(b(t), e) : f(t);
288
+ )) : a(t) ? i(t, e) : y(t) ? i(w(t), e) : s(t);
258
289
  } catch (t) {
259
- return i(d(t), e);
290
+ return i(l(t), e);
260
291
  }
261
292
  }
262
- function v(r, e) {
293
+ function sr(r, e) {
263
294
  return Promise.resolve(r).then(
264
- (t) => c(t) ? i(t, e) : w(t) ? i(b(t), e) : f(t),
265
- (t) => i(d(t), e)
295
+ (t) => a(t) ? i(t, e) : y(t) ? i(w(t), e) : s(t),
296
+ (t) => i(l(t), e)
266
297
  );
267
298
  }
268
299
  function i(r, e) {
269
- if (r.status === o.Success || X(r.error)) return r;
270
- const t = rr(e);
271
- return t === null || r.error instanceof Error && e !== void 0 && G(e) ? r : d(t(r.error));
300
+ if (r.status === n.Success || ur(r.error)) return r;
301
+ const t = C(e);
302
+ return t === null || r.error instanceof Error && e !== void 0 && A(e) ? r : l(t(r.error));
272
303
  }
273
- function rr(r) {
274
- return r === void 0 ? null : G(r) ? or : er(r) ? r.normalizeError : null;
304
+ function C(r) {
305
+ return r === void 0 ? null : A(r) ? m : cr(r) ? r.normalizeError : null;
275
306
  }
276
- function G(r) {
277
- return !u(r) || Object.keys(r).length !== 1 ? !1 : Object.getOwnPropertyDescriptor(r, "mode")?.value === p.mode;
307
+ function A(r) {
308
+ return !c(r) || Object.keys(r).length !== 1 ? !1 : Object.getOwnPropertyDescriptor(r, "mode")?.value === x.mode;
278
309
  }
279
- function er(r) {
280
- return u(r) ? h(r.normalizeError) : !1;
310
+ function cr(r) {
311
+ return c(r) ? b(r.normalizeError) : !1;
281
312
  }
282
- function tr(r) {
283
- if (!u(r) || Array.isArray(r)) return !1;
313
+ function ar(r) {
314
+ if (!c(r) || Array.isArray(r)) return !1;
284
315
  const e = Object.getPrototypeOf(r);
285
316
  return e === Object.prototype || e === null;
286
317
  }
287
- function y(r) {
318
+ function R(r) {
288
319
  try {
289
320
  const e = JSON.stringify(r);
290
321
  if (typeof e == "string") return e;
@@ -293,30 +324,51 @@ function y(r) {
293
324
  }
294
325
  return null;
295
326
  }
296
- function nr(r) {
297
- if (Object.getPrototypeOf(r) === null) {
298
- const n = y(r);
299
- return n !== null ? n : Object.prototype.toString.call(r);
327
+ const S = "Unstringifiable error value";
328
+ function B(r) {
329
+ try {
330
+ return String(r);
331
+ } catch {
332
+ return null;
300
333
  }
301
- const e = String(r);
302
- if (e !== "[object Object]") return e;
303
- const t = y(r);
304
- return t === null ? e : t;
305
334
  }
306
- function or(r) {
307
- return r instanceof Error ? r : Array.isArray(r) ? new AggregateError(r, "Multiple errors", { cause: r }) : tr(r) ? new Error(nr(r), { cause: r }) : new Error(String(r), { cause: r });
335
+ function lr(r) {
336
+ try {
337
+ return Object.prototype.toString.call(r);
338
+ } catch {
339
+ return null;
340
+ }
341
+ }
342
+ function fr(r) {
343
+ if (Object.getPrototypeOf(r) === null) {
344
+ const u = R(r);
345
+ return u !== null ? u : lr(r) ?? S;
346
+ }
347
+ const e = B(r);
348
+ if (e !== null && e !== "[object Object]") return e;
349
+ const t = R(r);
350
+ return t !== null ? t : e ?? S;
351
+ }
352
+ function m(r) {
353
+ return r instanceof Error ? r : Array.isArray(r) ? new AggregateError(r, "Multiple errors", { cause: r }) : ar(r) ? new Error(fr(r), { cause: r }) : new Error(
354
+ B(r) ?? S,
355
+ { cause: r }
356
+ );
308
357
  }
309
358
  export {
310
- o as FailableStatus,
311
- p as NormalizedErrors,
312
- dr as failable,
313
- d as failure,
314
- c as isFailable,
315
- w as isFailableLike,
316
- sr as isFailure,
317
- cr as isSuccess,
318
- lr as run,
319
- f as success,
320
- ar as throwIfError,
321
- fr as toFailableLike
359
+ n as FailableStatus,
360
+ x as NormalizedErrors,
361
+ yr as all,
362
+ gr as allSettled,
363
+ Ar as failable,
364
+ l as failure,
365
+ a as isFailable,
366
+ y as isFailableLike,
367
+ dr as isFailure,
368
+ hr as isSuccess,
369
+ Fr as race,
370
+ wr as run,
371
+ s as success,
372
+ Sr as throwIfFailure,
373
+ mr as toFailableLike
322
374
  };