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.cjs +119 -43
- package/dist/index.d.cts +254 -48
- package/dist/index.d.ts +254 -48
- package/dist/index.js +114 -41
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -12,13 +12,20 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
12
12
|
};
|
|
13
13
|
var _ResultAsync_inner;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.
|
|
15
|
+
exports.RetupleInvalidResultError = exports.RetupleThrownValueError = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
|
|
16
|
+
exports.Result = Result;
|
|
16
17
|
exports.Ok = Ok;
|
|
17
18
|
exports.Err = Err;
|
|
18
|
-
exports.
|
|
19
|
+
exports.nonNullable = nonNullable;
|
|
20
|
+
exports.truthy = truthy;
|
|
19
21
|
exports.safe = safe;
|
|
20
22
|
exports.safeAsync = safeAsync;
|
|
21
23
|
exports.safePromise = safePromise;
|
|
24
|
+
/**
|
|
25
|
+
* ## Retuple Unwrap Failed
|
|
26
|
+
*
|
|
27
|
+
* An error which occurs when calling `$unwrap` on `Err`.
|
|
28
|
+
*/
|
|
22
29
|
class RetupleUnwrapFailed extends Error {
|
|
23
30
|
constructor(value, msg = "Unwrap failed") {
|
|
24
31
|
super(msg, value instanceof Error ? { cause: value } : undefined);
|
|
@@ -26,6 +33,11 @@ class RetupleUnwrapFailed extends Error {
|
|
|
26
33
|
}
|
|
27
34
|
}
|
|
28
35
|
exports.RetupleUnwrapFailed = RetupleUnwrapFailed;
|
|
36
|
+
/**
|
|
37
|
+
* ## Retuple Unwrap Err Failed
|
|
38
|
+
*
|
|
39
|
+
* An error which occurs when calling `$unwrapErr` on `Ok`.
|
|
40
|
+
*/
|
|
29
41
|
class RetupleUnwrapErrFailed extends Error {
|
|
30
42
|
constructor(value, msg = "Unwrap error failed") {
|
|
31
43
|
super(msg);
|
|
@@ -33,6 +45,12 @@ class RetupleUnwrapErrFailed extends Error {
|
|
|
33
45
|
}
|
|
34
46
|
}
|
|
35
47
|
exports.RetupleUnwrapErrFailed = RetupleUnwrapErrFailed;
|
|
48
|
+
/**
|
|
49
|
+
* ## Retuple Expect Failed
|
|
50
|
+
*
|
|
51
|
+
* An error which occurs when calling `$expect` on `Err`, and when the value
|
|
52
|
+
* contained in the `Err` is not an instance of `Error`.
|
|
53
|
+
*/
|
|
36
54
|
class RetupleExpectFailed extends Error {
|
|
37
55
|
constructor(value) {
|
|
38
56
|
super("Expect failed");
|
|
@@ -40,6 +58,13 @@ class RetupleExpectFailed extends Error {
|
|
|
40
58
|
}
|
|
41
59
|
}
|
|
42
60
|
exports.RetupleExpectFailed = RetupleExpectFailed;
|
|
61
|
+
/**
|
|
62
|
+
* ## Retuple Thrown Value Error
|
|
63
|
+
*
|
|
64
|
+
* An error constructed when a safe function call throws or rejects, when the
|
|
65
|
+
* thrown error or rejected value is not an instance of `Error`, and when no
|
|
66
|
+
* map error function is provided.
|
|
67
|
+
*/
|
|
43
68
|
class RetupleThrownValueError extends Error {
|
|
44
69
|
constructor(value) {
|
|
45
70
|
super("Caught value was not an instance of Error");
|
|
@@ -47,50 +72,76 @@ class RetupleThrownValueError extends Error {
|
|
|
47
72
|
}
|
|
48
73
|
}
|
|
49
74
|
exports.RetupleThrownValueError = RetupleThrownValueError;
|
|
75
|
+
/**
|
|
76
|
+
* ## Retuple Invalid Result Error
|
|
77
|
+
*
|
|
78
|
+
* An error constructed when a safe function call throws or rejects, when the
|
|
79
|
+
* thrown error or rejected value is not an instance of `Error`, and when no
|
|
80
|
+
* map error function is provided.
|
|
81
|
+
*/
|
|
82
|
+
class RetupleInvalidResultError extends Error {
|
|
83
|
+
constructor(value) {
|
|
84
|
+
super("Constructing a Result from tuple failed, at least one of the values at index 0 or 1 should be undefined");
|
|
85
|
+
this.value = value;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.RetupleInvalidResultError = RetupleInvalidResultError;
|
|
50
89
|
/**
|
|
51
90
|
* ## Result
|
|
52
91
|
*
|
|
53
92
|
* @TODO
|
|
54
93
|
*/
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
94
|
+
function Result(resultLike) {
|
|
95
|
+
const [err, ok] = resultLike;
|
|
96
|
+
if (err === null || err === undefined) {
|
|
97
|
+
return new ResultOk(ok);
|
|
98
|
+
}
|
|
99
|
+
if (ok === null || ok === undefined) {
|
|
100
|
+
return new ResultErr(err);
|
|
101
|
+
}
|
|
102
|
+
throw new RetupleInvalidResultError(resultLike);
|
|
103
|
+
}
|
|
104
|
+
Result.Ok = Ok;
|
|
105
|
+
Result.Err = Err;
|
|
106
|
+
Result.nonNullable = nonNullable;
|
|
107
|
+
Result.truthy = truthy;
|
|
108
|
+
Result.safe = safe;
|
|
109
|
+
Result.safeAsync = safeAsync;
|
|
110
|
+
Result.safePromise = safePromise;
|
|
111
|
+
Object.freeze(Result);
|
|
64
112
|
function Ok(val) {
|
|
65
113
|
return new ResultOk(val);
|
|
66
114
|
}
|
|
67
115
|
function Err(err) {
|
|
68
116
|
return new ResultErr(err);
|
|
69
117
|
}
|
|
70
|
-
function
|
|
71
|
-
if (value) {
|
|
118
|
+
function nonNullable(value, error = mapTrue) {
|
|
119
|
+
if (value !== null && value !== undefined) {
|
|
72
120
|
return new ResultOk(value);
|
|
73
121
|
}
|
|
74
|
-
|
|
75
|
-
|
|
122
|
+
return new ResultErr(error());
|
|
123
|
+
}
|
|
124
|
+
function truthy(value, error = mapTrue) {
|
|
125
|
+
if (value) {
|
|
126
|
+
return new ResultOk(value);
|
|
76
127
|
}
|
|
77
|
-
return new ResultErr(
|
|
128
|
+
return new ResultErr(error());
|
|
78
129
|
}
|
|
79
130
|
function safe(f, mapError = ensureError) {
|
|
80
131
|
try {
|
|
81
|
-
return
|
|
132
|
+
return new ResultOk(f());
|
|
82
133
|
}
|
|
83
134
|
catch (err) {
|
|
84
|
-
return
|
|
135
|
+
return new ResultErr(mapError(err));
|
|
85
136
|
}
|
|
86
137
|
}
|
|
87
138
|
function safeAsync(f, mapError = ensureError) {
|
|
88
139
|
return new ResultAsync((async () => {
|
|
89
140
|
try {
|
|
90
|
-
return
|
|
141
|
+
return new ResultOk(await f());
|
|
91
142
|
}
|
|
92
143
|
catch (err) {
|
|
93
|
-
return
|
|
144
|
+
return new ResultErr(await mapError(err));
|
|
94
145
|
}
|
|
95
146
|
})());
|
|
96
147
|
}
|
|
@@ -108,6 +159,12 @@ class ResultOk extends Array {
|
|
|
108
159
|
this[0] = undefined;
|
|
109
160
|
this[1] = value;
|
|
110
161
|
}
|
|
162
|
+
toJSON() {
|
|
163
|
+
return this[1];
|
|
164
|
+
}
|
|
165
|
+
$toNativeTuple() {
|
|
166
|
+
return [undefined, this[1]];
|
|
167
|
+
}
|
|
111
168
|
$value() {
|
|
112
169
|
return this[1];
|
|
113
170
|
}
|
|
@@ -115,7 +172,7 @@ class ResultOk extends Array {
|
|
|
115
172
|
return true;
|
|
116
173
|
}
|
|
117
174
|
$isOkAnd(f) {
|
|
118
|
-
return f(this[1]);
|
|
175
|
+
return !!f(this[1]);
|
|
119
176
|
}
|
|
120
177
|
$isErr() {
|
|
121
178
|
return false;
|
|
@@ -150,6 +207,12 @@ class ResultOk extends Array {
|
|
|
150
207
|
$mapOrElse(_def, f) {
|
|
151
208
|
return new ResultOk(f(this[1]));
|
|
152
209
|
}
|
|
210
|
+
$assertOr(def, condition = isTruthy) {
|
|
211
|
+
return condition(this[1]) ? this : def;
|
|
212
|
+
}
|
|
213
|
+
$assertOrElse(def, condition = isTruthy) {
|
|
214
|
+
return condition(this[1]) ? this : def();
|
|
215
|
+
}
|
|
153
216
|
$or() {
|
|
154
217
|
return this;
|
|
155
218
|
}
|
|
@@ -209,6 +272,12 @@ class ResultErr extends Array {
|
|
|
209
272
|
this[0] = err;
|
|
210
273
|
this[1] = undefined;
|
|
211
274
|
}
|
|
275
|
+
toJSON() {
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
$toNativeTuple() {
|
|
279
|
+
return [this[0], undefined];
|
|
280
|
+
}
|
|
212
281
|
$value() {
|
|
213
282
|
return this[0];
|
|
214
283
|
}
|
|
@@ -222,7 +291,7 @@ class ResultErr extends Array {
|
|
|
222
291
|
return true;
|
|
223
292
|
}
|
|
224
293
|
$isErrAnd(f) {
|
|
225
|
-
return f(this[0]);
|
|
294
|
+
return !!f(this[0]);
|
|
226
295
|
}
|
|
227
296
|
$expect() {
|
|
228
297
|
if (this[0] instanceof Error) {
|
|
@@ -254,6 +323,12 @@ class ResultErr extends Array {
|
|
|
254
323
|
$mapOrElse(def) {
|
|
255
324
|
return new ResultOk(def(this[0]));
|
|
256
325
|
}
|
|
326
|
+
$assertOr() {
|
|
327
|
+
return this;
|
|
328
|
+
}
|
|
329
|
+
$assertOrElse() {
|
|
330
|
+
return this;
|
|
331
|
+
}
|
|
257
332
|
$or(or) {
|
|
258
333
|
return or;
|
|
259
334
|
}
|
|
@@ -391,6 +466,22 @@ class ResultAsync {
|
|
|
391
466
|
: new ResultOk(def(res[0]));
|
|
392
467
|
}));
|
|
393
468
|
}
|
|
469
|
+
$assertOr(def, condition = isTruthy) {
|
|
470
|
+
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
471
|
+
if (res instanceof ResultErr || condition(res[1])) {
|
|
472
|
+
return res;
|
|
473
|
+
}
|
|
474
|
+
return await def;
|
|
475
|
+
}));
|
|
476
|
+
}
|
|
477
|
+
$assertOrElse(def, condition = isTruthy) {
|
|
478
|
+
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
479
|
+
if (res instanceof ResultErr || condition(res[1])) {
|
|
480
|
+
return res;
|
|
481
|
+
}
|
|
482
|
+
return await def();
|
|
483
|
+
}));
|
|
484
|
+
}
|
|
394
485
|
/**
|
|
395
486
|
* @TODO
|
|
396
487
|
*/
|
|
@@ -407,9 +498,6 @@ class ResultAsync {
|
|
|
407
498
|
return res instanceof ResultErr ? await f(res[0]) : res;
|
|
408
499
|
}));
|
|
409
500
|
}
|
|
410
|
-
/**
|
|
411
|
-
* @TODO
|
|
412
|
-
*/
|
|
413
501
|
$orSafe(f, mapError = ensureError) {
|
|
414
502
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
415
503
|
if (res instanceof ResultOk) {
|
|
@@ -453,9 +541,6 @@ class ResultAsync {
|
|
|
453
541
|
return res;
|
|
454
542
|
}));
|
|
455
543
|
}
|
|
456
|
-
/**
|
|
457
|
-
* @TODO
|
|
458
|
-
*/
|
|
459
544
|
$andSafe(f, mapError = ensureError) {
|
|
460
545
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
461
546
|
if (res instanceof ResultErr) {
|
|
@@ -514,18 +599,9 @@ function ensureError(err) {
|
|
|
514
599
|
}
|
|
515
600
|
return new RetupleThrownValueError(err);
|
|
516
601
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
Object.freeze(RetupleExpectFailed);
|
|
524
|
-
Object.freeze(RetupleThrownValueError);
|
|
525
|
-
Object.freeze(ResultOk.prototype);
|
|
526
|
-
Object.freeze(ResultErr.prototype);
|
|
527
|
-
Object.freeze(ResultAsync.prototype);
|
|
528
|
-
Object.freeze(RetupleUnwrapFailed.prototype);
|
|
529
|
-
Object.freeze(RetupleUnwrapErrFailed.prototype);
|
|
530
|
-
Object.freeze(RetupleExpectFailed.prototype);
|
|
531
|
-
Object.freeze(RetupleThrownValueError.prototype);
|
|
602
|
+
function mapTrue() {
|
|
603
|
+
return true;
|
|
604
|
+
}
|
|
605
|
+
function isTruthy(val) {
|
|
606
|
+
return !!val;
|
|
607
|
+
}
|