retuple 1.0.0-next.10 → 1.0.0-next.12
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 +39 -6
- package/dist/index.d.cts +36 -15
- package/dist/index.d.ts +36 -15
- package/dist/index.js +42 -5
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -12,15 +12,10 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
12
12
|
};
|
|
13
13
|
var _ResultAsync_inner;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.RetupleArrayMethodUnavailableError = exports.RetupleInvalidResultError = exports.RetupleThrownValueError = exports.RetupleFlattenFailed = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
|
|
15
|
+
exports.RetupleArrayMethodUnavailableError = exports.RetupleInvalidUnionError = exports.RetupleInvalidResultError = exports.RetupleThrownValueError = exports.RetupleFlattenFailed = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
|
|
16
16
|
exports.Result = Result;
|
|
17
17
|
exports.Ok = Ok;
|
|
18
18
|
exports.Err = Err;
|
|
19
|
-
exports.nonNullable = nonNullable;
|
|
20
|
-
exports.truthy = truthy;
|
|
21
|
-
exports.safe = safe;
|
|
22
|
-
exports.safeAsync = safeAsync;
|
|
23
|
-
exports.safePromise = safePromise;
|
|
24
19
|
/**
|
|
25
20
|
* ## Retuple Unwrap Failed
|
|
26
21
|
*
|
|
@@ -100,6 +95,22 @@ class RetupleInvalidResultError extends Error {
|
|
|
100
95
|
}
|
|
101
96
|
}
|
|
102
97
|
exports.RetupleInvalidResultError = RetupleInvalidResultError;
|
|
98
|
+
/**
|
|
99
|
+
* ## Retuple Invalid Union Error
|
|
100
|
+
*
|
|
101
|
+
* This error is thrown when attempting to construct a `Result` from a
|
|
102
|
+
* discriminated union, when the 'success' property is not boolean. In this
|
|
103
|
+
* case, it is impossible to determine whether the result should be `Ok` or
|
|
104
|
+
* `Err`.
|
|
105
|
+
*/
|
|
106
|
+
class RetupleInvalidUnionError extends Error {
|
|
107
|
+
constructor(value) {
|
|
108
|
+
super("Constructing a Result from discriminated union failed, the success " +
|
|
109
|
+
"property must be boolean");
|
|
110
|
+
this.value = value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.RetupleInvalidUnionError = RetupleInvalidUnionError;
|
|
103
114
|
/**
|
|
104
115
|
* ## Retuple Array Method Unavailable Error
|
|
105
116
|
*
|
|
@@ -130,8 +141,10 @@ function Result(resultLike) {
|
|
|
130
141
|
}
|
|
131
142
|
Result.Ok = Ok;
|
|
132
143
|
Result.Err = Err;
|
|
144
|
+
Result.$resolve = resolve;
|
|
133
145
|
Result.$nonNullable = nonNullable;
|
|
134
146
|
Result.$truthy = truthy;
|
|
147
|
+
Result.$union = union;
|
|
135
148
|
Result.$safe = safe;
|
|
136
149
|
Result.$safeAsync = safeAsync;
|
|
137
150
|
Result.$safePromise = safePromise;
|
|
@@ -142,6 +155,17 @@ function Ok(val) {
|
|
|
142
155
|
function Err(err) {
|
|
143
156
|
return new ResultErr(err);
|
|
144
157
|
}
|
|
158
|
+
function resolve(result) {
|
|
159
|
+
if (result instanceof ResultAsync) {
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
else if (result instanceof ResultOk || result instanceof ResultErr) {
|
|
163
|
+
return result.$async();
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
return new ResultAsync(result);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
145
169
|
function nonNullable(value, error = mapTrue) {
|
|
146
170
|
if (value !== null && value !== undefined) {
|
|
147
171
|
return Ok(value);
|
|
@@ -154,6 +178,15 @@ function truthy(value, error = mapTrue) {
|
|
|
154
178
|
}
|
|
155
179
|
return Err(error());
|
|
156
180
|
}
|
|
181
|
+
function union(union) {
|
|
182
|
+
if (union.success === true) {
|
|
183
|
+
return Ok(union.data);
|
|
184
|
+
}
|
|
185
|
+
if (union.success === false) {
|
|
186
|
+
return Err(union.error);
|
|
187
|
+
}
|
|
188
|
+
throw new RetupleInvalidUnionError(union);
|
|
189
|
+
}
|
|
157
190
|
function safe(f, mapError = ensureError) {
|
|
158
191
|
try {
|
|
159
192
|
return Ok(f());
|
package/dist/index.d.cts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
export type Ok = typeof Ok;
|
|
2
2
|
export type Err = typeof Err;
|
|
3
|
-
export type nonNullable = typeof nonNullable;
|
|
4
|
-
export type truthy = typeof truthy;
|
|
5
|
-
export type safe = typeof safe;
|
|
6
|
-
export type safeAsync = typeof safeAsync;
|
|
7
|
-
export type safePromise = typeof safePromise;
|
|
8
3
|
export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
|
|
9
4
|
export { type ResultAsync };
|
|
10
5
|
/**
|
|
@@ -67,6 +62,18 @@ export declare class RetupleInvalidResultError extends Error {
|
|
|
67
62
|
value: unknown[];
|
|
68
63
|
constructor(value: unknown[]);
|
|
69
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* ## Retuple Invalid Union Error
|
|
67
|
+
*
|
|
68
|
+
* This error is thrown when attempting to construct a `Result` from a
|
|
69
|
+
* discriminated union, when the 'success' property is not boolean. In this
|
|
70
|
+
* case, it is impossible to determine whether the result should be `Ok` or
|
|
71
|
+
* `Err`.
|
|
72
|
+
*/
|
|
73
|
+
export declare class RetupleInvalidUnionError extends Error {
|
|
74
|
+
value: unknown;
|
|
75
|
+
constructor(value: unknown);
|
|
76
|
+
}
|
|
70
77
|
/**
|
|
71
78
|
* ## Retuple Array Method Unavailable Error
|
|
72
79
|
*
|
|
@@ -85,8 +92,10 @@ export declare function Result<R extends [err: null | undefined, value: unknown]
|
|
|
85
92
|
export declare namespace Result {
|
|
86
93
|
var Ok: typeof import(".").Ok;
|
|
87
94
|
var Err: typeof import(".").Err;
|
|
95
|
+
var $resolve: typeof resolve;
|
|
88
96
|
var $nonNullable: typeof nonNullable;
|
|
89
97
|
var $truthy: typeof truthy;
|
|
98
|
+
var $union: typeof union;
|
|
90
99
|
var $safe: typeof safe;
|
|
91
100
|
var $safeAsync: typeof safeAsync;
|
|
92
101
|
var $safePromise: typeof safePromise;
|
|
@@ -133,6 +142,7 @@ export declare function Ok<const T>(val: T): Result<T, never>;
|
|
|
133
142
|
*/
|
|
134
143
|
export declare function Err(): Result<never, void>;
|
|
135
144
|
export declare function Err<const E>(err: E): Result<never, E>;
|
|
145
|
+
declare function resolve<T, E>(result: Retuple<T, E> | PromiseLike<Retuple<T, E>>): ResultAsync<T, E>;
|
|
136
146
|
/**
|
|
137
147
|
* Construct a {@link Result} from a value. If the value is neither null or
|
|
138
148
|
* undefined, the result is `Ok`.
|
|
@@ -178,8 +188,8 @@ export declare function Err<const E>(err: E): Result<never, E>;
|
|
|
178
188
|
* assert.equal(value, undefined);
|
|
179
189
|
* ```
|
|
180
190
|
*/
|
|
181
|
-
|
|
182
|
-
|
|
191
|
+
declare function nonNullable<const T>(value: T): Result<NonNullable<T>, true>;
|
|
192
|
+
declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
|
|
183
193
|
/**
|
|
184
194
|
* Construct a {@link Result} from a value. If the value is truthy, the result
|
|
185
195
|
* is `Ok`.
|
|
@@ -225,8 +235,9 @@ export declare function nonNullable<const T, E>(value: T, error: () => E): Resul
|
|
|
225
235
|
* assert.equal(value, undefined);
|
|
226
236
|
* ```
|
|
227
237
|
*/
|
|
228
|
-
|
|
229
|
-
|
|
238
|
+
declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
|
|
239
|
+
declare function truthy<const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
|
|
240
|
+
declare function union<U extends ObjectUnionOk<any> | ObjectUnionErr<any>>(union: U): Result<U extends ObjectUnionOk<infer T> ? T : never, U extends ObjectUnionErr<infer E> ? E : never>;
|
|
230
241
|
/**
|
|
231
242
|
* Construct a {@link Result} from a synchronous function call. If the function
|
|
232
243
|
* returns without throwing, the result is `Ok`.
|
|
@@ -292,8 +303,8 @@ export declare function truthy<const T, E>(value: T, error: () => E): Result<Tru
|
|
|
292
303
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
293
304
|
* assert.equal(value, undefined);
|
|
294
305
|
*/
|
|
295
|
-
|
|
296
|
-
|
|
306
|
+
declare function safe<T>(f: () => Awaited<T>): Result<T, Error>;
|
|
307
|
+
declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
|
|
297
308
|
/**
|
|
298
309
|
* Construct a {@link ResultAsync} from a function call. If the function returns
|
|
299
310
|
* without throwing, and any promise returned resolves, the result is `Ok`.
|
|
@@ -359,8 +370,8 @@ export declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown)
|
|
|
359
370
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
360
371
|
* assert.equal(value, undefined);
|
|
361
372
|
*/
|
|
362
|
-
|
|
363
|
-
|
|
373
|
+
declare function safeAsync<T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
|
|
374
|
+
declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
|
|
364
375
|
/**
|
|
365
376
|
* Construct a {@link Result} from a promise. If the promise resolves, the
|
|
366
377
|
* result is `Ok`.
|
|
@@ -422,8 +433,8 @@ export declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (
|
|
|
422
433
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
423
434
|
* assert.equal(value, undefined);
|
|
424
435
|
*/
|
|
425
|
-
|
|
426
|
-
|
|
436
|
+
declare function safePromise<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
|
|
437
|
+
declare function safePromise<T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
|
|
427
438
|
/**
|
|
428
439
|
* ## RetupleArray
|
|
429
440
|
*
|
|
@@ -942,6 +953,16 @@ type OkTuple<T> = [err: undefined, value: T];
|
|
|
942
953
|
type ErrTuple<E> = [err: E, value: undefined];
|
|
943
954
|
type ThisOk<T> = OkTuple<T> & Retuple<T, never>;
|
|
944
955
|
type ThisErr<E> = ErrTuple<E> & Retuple<never, E>;
|
|
956
|
+
type ObjectUnionOk<T> = {
|
|
957
|
+
success: true;
|
|
958
|
+
data: T;
|
|
959
|
+
error?: never | undefined;
|
|
960
|
+
};
|
|
961
|
+
type ObjectUnionErr<E> = {
|
|
962
|
+
success: false;
|
|
963
|
+
data?: never | undefined;
|
|
964
|
+
error: E;
|
|
965
|
+
};
|
|
945
966
|
type RetupleAwaitable<T, E> = Retuple<T, E> | PromiseLike<Retuple<T, E>>;
|
|
946
967
|
interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
|
|
947
968
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
export type Ok = typeof Ok;
|
|
2
2
|
export type Err = typeof Err;
|
|
3
|
-
export type nonNullable = typeof nonNullable;
|
|
4
|
-
export type truthy = typeof truthy;
|
|
5
|
-
export type safe = typeof safe;
|
|
6
|
-
export type safeAsync = typeof safeAsync;
|
|
7
|
-
export type safePromise = typeof safePromise;
|
|
8
3
|
export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
|
|
9
4
|
export { type ResultAsync };
|
|
10
5
|
/**
|
|
@@ -67,6 +62,18 @@ export declare class RetupleInvalidResultError extends Error {
|
|
|
67
62
|
value: unknown[];
|
|
68
63
|
constructor(value: unknown[]);
|
|
69
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* ## Retuple Invalid Union Error
|
|
67
|
+
*
|
|
68
|
+
* This error is thrown when attempting to construct a `Result` from a
|
|
69
|
+
* discriminated union, when the 'success' property is not boolean. In this
|
|
70
|
+
* case, it is impossible to determine whether the result should be `Ok` or
|
|
71
|
+
* `Err`.
|
|
72
|
+
*/
|
|
73
|
+
export declare class RetupleInvalidUnionError extends Error {
|
|
74
|
+
value: unknown;
|
|
75
|
+
constructor(value: unknown);
|
|
76
|
+
}
|
|
70
77
|
/**
|
|
71
78
|
* ## Retuple Array Method Unavailable Error
|
|
72
79
|
*
|
|
@@ -85,8 +92,10 @@ export declare function Result<R extends [err: null | undefined, value: unknown]
|
|
|
85
92
|
export declare namespace Result {
|
|
86
93
|
var Ok: typeof import(".").Ok;
|
|
87
94
|
var Err: typeof import(".").Err;
|
|
95
|
+
var $resolve: typeof resolve;
|
|
88
96
|
var $nonNullable: typeof nonNullable;
|
|
89
97
|
var $truthy: typeof truthy;
|
|
98
|
+
var $union: typeof union;
|
|
90
99
|
var $safe: typeof safe;
|
|
91
100
|
var $safeAsync: typeof safeAsync;
|
|
92
101
|
var $safePromise: typeof safePromise;
|
|
@@ -133,6 +142,7 @@ export declare function Ok<const T>(val: T): Result<T, never>;
|
|
|
133
142
|
*/
|
|
134
143
|
export declare function Err(): Result<never, void>;
|
|
135
144
|
export declare function Err<const E>(err: E): Result<never, E>;
|
|
145
|
+
declare function resolve<T, E>(result: Retuple<T, E> | PromiseLike<Retuple<T, E>>): ResultAsync<T, E>;
|
|
136
146
|
/**
|
|
137
147
|
* Construct a {@link Result} from a value. If the value is neither null or
|
|
138
148
|
* undefined, the result is `Ok`.
|
|
@@ -178,8 +188,8 @@ export declare function Err<const E>(err: E): Result<never, E>;
|
|
|
178
188
|
* assert.equal(value, undefined);
|
|
179
189
|
* ```
|
|
180
190
|
*/
|
|
181
|
-
|
|
182
|
-
|
|
191
|
+
declare function nonNullable<const T>(value: T): Result<NonNullable<T>, true>;
|
|
192
|
+
declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
|
|
183
193
|
/**
|
|
184
194
|
* Construct a {@link Result} from a value. If the value is truthy, the result
|
|
185
195
|
* is `Ok`.
|
|
@@ -225,8 +235,9 @@ export declare function nonNullable<const T, E>(value: T, error: () => E): Resul
|
|
|
225
235
|
* assert.equal(value, undefined);
|
|
226
236
|
* ```
|
|
227
237
|
*/
|
|
228
|
-
|
|
229
|
-
|
|
238
|
+
declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
|
|
239
|
+
declare function truthy<const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
|
|
240
|
+
declare function union<U extends ObjectUnionOk<any> | ObjectUnionErr<any>>(union: U): Result<U extends ObjectUnionOk<infer T> ? T : never, U extends ObjectUnionErr<infer E> ? E : never>;
|
|
230
241
|
/**
|
|
231
242
|
* Construct a {@link Result} from a synchronous function call. If the function
|
|
232
243
|
* returns without throwing, the result is `Ok`.
|
|
@@ -292,8 +303,8 @@ export declare function truthy<const T, E>(value: T, error: () => E): Result<Tru
|
|
|
292
303
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
293
304
|
* assert.equal(value, undefined);
|
|
294
305
|
*/
|
|
295
|
-
|
|
296
|
-
|
|
306
|
+
declare function safe<T>(f: () => Awaited<T>): Result<T, Error>;
|
|
307
|
+
declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
|
|
297
308
|
/**
|
|
298
309
|
* Construct a {@link ResultAsync} from a function call. If the function returns
|
|
299
310
|
* without throwing, and any promise returned resolves, the result is `Ok`.
|
|
@@ -359,8 +370,8 @@ export declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown)
|
|
|
359
370
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
360
371
|
* assert.equal(value, undefined);
|
|
361
372
|
*/
|
|
362
|
-
|
|
363
|
-
|
|
373
|
+
declare function safeAsync<T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
|
|
374
|
+
declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
|
|
364
375
|
/**
|
|
365
376
|
* Construct a {@link Result} from a promise. If the promise resolves, the
|
|
366
377
|
* result is `Ok`.
|
|
@@ -422,8 +433,8 @@ export declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (
|
|
|
422
433
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
423
434
|
* assert.equal(value, undefined);
|
|
424
435
|
*/
|
|
425
|
-
|
|
426
|
-
|
|
436
|
+
declare function safePromise<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
|
|
437
|
+
declare function safePromise<T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
|
|
427
438
|
/**
|
|
428
439
|
* ## RetupleArray
|
|
429
440
|
*
|
|
@@ -942,6 +953,16 @@ type OkTuple<T> = [err: undefined, value: T];
|
|
|
942
953
|
type ErrTuple<E> = [err: E, value: undefined];
|
|
943
954
|
type ThisOk<T> = OkTuple<T> & Retuple<T, never>;
|
|
944
955
|
type ThisErr<E> = ErrTuple<E> & Retuple<never, E>;
|
|
956
|
+
type ObjectUnionOk<T> = {
|
|
957
|
+
success: true;
|
|
958
|
+
data: T;
|
|
959
|
+
error?: never | undefined;
|
|
960
|
+
};
|
|
961
|
+
type ObjectUnionErr<E> = {
|
|
962
|
+
success: false;
|
|
963
|
+
data?: never | undefined;
|
|
964
|
+
error: E;
|
|
965
|
+
};
|
|
945
966
|
type RetupleAwaitable<T, E> = Retuple<T, E> | PromiseLike<Retuple<T, E>>;
|
|
946
967
|
interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
|
|
947
968
|
/**
|
package/dist/index.js
CHANGED
|
@@ -83,6 +83,21 @@ export class RetupleInvalidResultError extends Error {
|
|
|
83
83
|
this.value = value;
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* ## Retuple Invalid Union Error
|
|
88
|
+
*
|
|
89
|
+
* This error is thrown when attempting to construct a `Result` from a
|
|
90
|
+
* discriminated union, when the 'success' property is not boolean. In this
|
|
91
|
+
* case, it is impossible to determine whether the result should be `Ok` or
|
|
92
|
+
* `Err`.
|
|
93
|
+
*/
|
|
94
|
+
export class RetupleInvalidUnionError extends Error {
|
|
95
|
+
constructor(value) {
|
|
96
|
+
super("Constructing a Result from discriminated union failed, the success " +
|
|
97
|
+
"property must be boolean");
|
|
98
|
+
this.value = value;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
86
101
|
/**
|
|
87
102
|
* ## Retuple Array Method Unavailable Error
|
|
88
103
|
*
|
|
@@ -112,8 +127,10 @@ export function Result(resultLike) {
|
|
|
112
127
|
}
|
|
113
128
|
Result.Ok = Ok;
|
|
114
129
|
Result.Err = Err;
|
|
130
|
+
Result.$resolve = resolve;
|
|
115
131
|
Result.$nonNullable = nonNullable;
|
|
116
132
|
Result.$truthy = truthy;
|
|
133
|
+
Result.$union = union;
|
|
117
134
|
Result.$safe = safe;
|
|
118
135
|
Result.$safeAsync = safeAsync;
|
|
119
136
|
Result.$safePromise = safePromise;
|
|
@@ -124,19 +141,39 @@ export function Ok(val) {
|
|
|
124
141
|
export function Err(err) {
|
|
125
142
|
return new ResultErr(err);
|
|
126
143
|
}
|
|
127
|
-
|
|
144
|
+
function resolve(result) {
|
|
145
|
+
if (result instanceof ResultAsync) {
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
else if (result instanceof ResultOk || result instanceof ResultErr) {
|
|
149
|
+
return result.$async();
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
return new ResultAsync(result);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function nonNullable(value, error = mapTrue) {
|
|
128
156
|
if (value !== null && value !== undefined) {
|
|
129
157
|
return Ok(value);
|
|
130
158
|
}
|
|
131
159
|
return Err(error());
|
|
132
160
|
}
|
|
133
|
-
|
|
161
|
+
function truthy(value, error = mapTrue) {
|
|
134
162
|
if (value) {
|
|
135
163
|
return Ok(value);
|
|
136
164
|
}
|
|
137
165
|
return Err(error());
|
|
138
166
|
}
|
|
139
|
-
|
|
167
|
+
function union(union) {
|
|
168
|
+
if (union.success === true) {
|
|
169
|
+
return Ok(union.data);
|
|
170
|
+
}
|
|
171
|
+
if (union.success === false) {
|
|
172
|
+
return Err(union.error);
|
|
173
|
+
}
|
|
174
|
+
throw new RetupleInvalidUnionError(union);
|
|
175
|
+
}
|
|
176
|
+
function safe(f, mapError = ensureError) {
|
|
140
177
|
try {
|
|
141
178
|
return Ok(f());
|
|
142
179
|
}
|
|
@@ -144,7 +181,7 @@ export function safe(f, mapError = ensureError) {
|
|
|
144
181
|
return Err(mapError(err));
|
|
145
182
|
}
|
|
146
183
|
}
|
|
147
|
-
|
|
184
|
+
function safeAsync(f, mapError = ensureError) {
|
|
148
185
|
return new ResultAsync((async () => {
|
|
149
186
|
try {
|
|
150
187
|
return Ok(await f());
|
|
@@ -154,7 +191,7 @@ export function safeAsync(f, mapError = ensureError) {
|
|
|
154
191
|
}
|
|
155
192
|
})());
|
|
156
193
|
}
|
|
157
|
-
|
|
194
|
+
function safePromise(promise, mapError = ensureError) {
|
|
158
195
|
return new ResultAsync(promise.then((Ok), async (err) => Err(await mapError(err))));
|
|
159
196
|
}
|
|
160
197
|
/**
|