retuple 1.0.0-next.1 → 1.0.0-next.3

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
@@ -10,74 +10,122 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _ResultAsync_inner;
13
+ /**
14
+ * ## Retuple Unwrap Failed
15
+ *
16
+ * An error which occurs when calling `$unwrap` on `Err`.
17
+ */
13
18
  export class RetupleUnwrapFailed extends Error {
14
19
  constructor(value, msg = "Unwrap failed") {
15
20
  super(msg, value instanceof Error ? { cause: value } : undefined);
16
21
  this.value = value;
17
22
  }
18
23
  }
24
+ /**
25
+ * ## Retuple Unwrap Err Failed
26
+ *
27
+ * An error which occurs when calling `$unwrapErr` on `Ok`.
28
+ */
19
29
  export class RetupleUnwrapErrFailed extends Error {
20
30
  constructor(value, msg = "Unwrap error failed") {
21
31
  super(msg);
22
32
  this.value = value;
23
33
  }
24
34
  }
35
+ /**
36
+ * ## Retuple Expect Failed
37
+ *
38
+ * An error which occurs when calling `$expect` on `Err`, and when the value
39
+ * contained in the `Err` is not an instance of `Error`.
40
+ */
25
41
  export class RetupleExpectFailed extends Error {
26
42
  constructor(value) {
27
43
  super("Expect failed");
28
44
  this.value = value;
29
45
  }
30
46
  }
47
+ /**
48
+ * ## Retuple Thrown Value Error
49
+ *
50
+ * An error constructed when a safe function call throws or rejects, when the
51
+ * thrown error or rejected value is not an instance of `Error`, and when no
52
+ * map error function is provided.
53
+ */
31
54
  export class RetupleThrownValueError extends Error {
32
55
  constructor(value) {
33
56
  super("Caught value was not an instance of Error");
34
57
  this.value = value;
35
58
  }
36
59
  }
60
+ /**
61
+ * ## Retuple Invalid Result Error
62
+ *
63
+ * An error constructed when a safe function call throws or rejects, when the
64
+ * thrown error or rejected value is not an instance of `Error`, and when no
65
+ * map error function is provided.
66
+ */
67
+ export class RetupleInvalidResultError extends Error {
68
+ constructor(value) {
69
+ super("Constructing a Result from tuple failed, at least one of the values at index 0 or 1 should be undefined");
70
+ this.value = value;
71
+ }
72
+ }
37
73
  /**
38
74
  * ## Result
39
75
  *
40
76
  * @TODO
41
77
  */
42
- export const Result = {
43
- Ok,
44
- Err,
45
- from,
46
- safe,
47
- safeAsync,
48
- safePromise,
49
- };
50
- export default Result;
78
+ export function Result(resultLike) {
79
+ const [err, ok] = resultLike;
80
+ if (err === null || err === undefined) {
81
+ return new ResultOk(ok);
82
+ }
83
+ if (ok === null || ok === undefined) {
84
+ return new ResultErr(err);
85
+ }
86
+ throw new RetupleInvalidResultError(resultLike);
87
+ }
88
+ Result.Ok = Ok;
89
+ Result.Err = Err;
90
+ Result.nonNullable = nonNullable;
91
+ Result.truthy = truthy;
92
+ Result.safe = safe;
93
+ Result.safeAsync = safeAsync;
94
+ Result.safePromise = safePromise;
95
+ Object.freeze(Result);
51
96
  export function Ok(val) {
52
97
  return new ResultOk(val);
53
98
  }
54
99
  export function Err(err) {
55
100
  return new ResultErr(err);
56
101
  }
57
- export function from(value, error) {
58
- if (value) {
102
+ export function nonNullable(value, error = mapTrue) {
103
+ if (value !== null && value !== undefined) {
59
104
  return new ResultOk(value);
60
105
  }
61
- if (error) {
62
- return new ResultErr(error());
106
+ return new ResultErr(error());
107
+ }
108
+ export function truthy(value, error = mapTrue) {
109
+ if (value) {
110
+ return new ResultOk(value);
63
111
  }
64
- return new ResultErr(true);
112
+ return new ResultErr(error());
65
113
  }
66
114
  export function safe(f, mapError = ensureError) {
67
115
  try {
68
- return Ok(f());
116
+ return new ResultOk(f());
69
117
  }
70
118
  catch (err) {
71
- return Err(mapError(err));
119
+ return new ResultErr(mapError(err));
72
120
  }
73
121
  }
74
122
  export function safeAsync(f, mapError = ensureError) {
75
123
  return new ResultAsync((async () => {
76
124
  try {
77
- return Ok(await f());
125
+ return new ResultOk(await f());
78
126
  }
79
127
  catch (err) {
80
- return Err(await mapError(err));
128
+ return new ResultErr(await mapError(err));
81
129
  }
82
130
  })());
83
131
  }
@@ -95,6 +143,12 @@ class ResultOk extends Array {
95
143
  this[0] = undefined;
96
144
  this[1] = value;
97
145
  }
146
+ toJSON() {
147
+ return this[1];
148
+ }
149
+ $toNativeTuple() {
150
+ return [undefined, this[1]];
151
+ }
98
152
  $value() {
99
153
  return this[1];
100
154
  }
@@ -102,7 +156,7 @@ class ResultOk extends Array {
102
156
  return true;
103
157
  }
104
158
  $isOkAnd(f) {
105
- return f(this[1]);
159
+ return !!f(this[1]);
106
160
  }
107
161
  $isErr() {
108
162
  return false;
@@ -137,6 +191,12 @@ class ResultOk extends Array {
137
191
  $mapOrElse(_def, f) {
138
192
  return new ResultOk(f(this[1]));
139
193
  }
194
+ $assertOr(def, condition = isTruthy) {
195
+ return condition(this[1]) ? this : def;
196
+ }
197
+ $assertOrElse(def, condition = isTruthy) {
198
+ return condition(this[1]) ? this : def();
199
+ }
140
200
  $or() {
141
201
  return this;
142
202
  }
@@ -196,6 +256,12 @@ class ResultErr extends Array {
196
256
  this[0] = err;
197
257
  this[1] = undefined;
198
258
  }
259
+ toJSON() {
260
+ return null;
261
+ }
262
+ $toNativeTuple() {
263
+ return [this[0], undefined];
264
+ }
199
265
  $value() {
200
266
  return this[0];
201
267
  }
@@ -209,7 +275,7 @@ class ResultErr extends Array {
209
275
  return true;
210
276
  }
211
277
  $isErrAnd(f) {
212
- return f(this[0]);
278
+ return !!f(this[0]);
213
279
  }
214
280
  $expect() {
215
281
  if (this[0] instanceof Error) {
@@ -241,6 +307,12 @@ class ResultErr extends Array {
241
307
  $mapOrElse(def) {
242
308
  return new ResultOk(def(this[0]));
243
309
  }
310
+ $assertOr() {
311
+ return this;
312
+ }
313
+ $assertOrElse() {
314
+ return this;
315
+ }
244
316
  $or(or) {
245
317
  return or;
246
318
  }
@@ -378,6 +450,22 @@ class ResultAsync {
378
450
  : new ResultOk(def(res[0]));
379
451
  }));
380
452
  }
453
+ $assertOr(def, condition = isTruthy) {
454
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
455
+ if (res instanceof ResultErr || condition(res[1])) {
456
+ return res;
457
+ }
458
+ return await def;
459
+ }));
460
+ }
461
+ $assertOrElse(def, condition = isTruthy) {
462
+ return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
463
+ if (res instanceof ResultErr || condition(res[1])) {
464
+ return res;
465
+ }
466
+ return await def();
467
+ }));
468
+ }
381
469
  /**
382
470
  * @TODO
383
471
  */
@@ -394,9 +482,6 @@ class ResultAsync {
394
482
  return res instanceof ResultErr ? await f(res[0]) : res;
395
483
  }));
396
484
  }
397
- /**
398
- * @TODO
399
- */
400
485
  $orSafe(f, mapError = ensureError) {
401
486
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
402
487
  if (res instanceof ResultOk) {
@@ -440,9 +525,6 @@ class ResultAsync {
440
525
  return res;
441
526
  }));
442
527
  }
443
- /**
444
- * @TODO
445
- */
446
528
  $andSafe(f, mapError = ensureError) {
447
529
  return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
448
530
  if (res instanceof ResultErr) {
@@ -501,18 +583,9 @@ function ensureError(err) {
501
583
  }
502
584
  return new RetupleThrownValueError(err);
503
585
  }
504
- Object.freeze(Result);
505
- Object.freeze(ResultOk);
506
- Object.freeze(ResultErr);
507
- Object.freeze(ResultAsync);
508
- Object.freeze(RetupleUnwrapFailed);
509
- Object.freeze(RetupleUnwrapErrFailed);
510
- Object.freeze(RetupleExpectFailed);
511
- Object.freeze(RetupleThrownValueError);
512
- Object.freeze(ResultOk.prototype);
513
- Object.freeze(ResultErr.prototype);
514
- Object.freeze(ResultAsync.prototype);
515
- Object.freeze(RetupleUnwrapFailed.prototype);
516
- Object.freeze(RetupleUnwrapErrFailed.prototype);
517
- Object.freeze(RetupleExpectFailed.prototype);
518
- Object.freeze(RetupleThrownValueError.prototype);
586
+ function mapTrue() {
587
+ return true;
588
+ }
589
+ function isTruthy(val) {
590
+ return !!val;
591
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "retuple",
3
- "version": "1.0.0-next.1",
3
+ "version": "1.0.0-next.3",
4
4
  "scripts": {
5
5
  "test": "vitest",
6
6
  "lint": "eslint . --ext .ts -c eslint.config.mjs --fix",