retuple 1.0.0-next.11 → 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 +27 -1
- package/dist/index.d.cts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +25 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -12,7 +12,7 @@ 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;
|
|
@@ -95,6 +95,22 @@ class RetupleInvalidResultError extends Error {
|
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
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;
|
|
98
114
|
/**
|
|
99
115
|
* ## Retuple Array Method Unavailable Error
|
|
100
116
|
*
|
|
@@ -128,6 +144,7 @@ Result.Err = Err;
|
|
|
128
144
|
Result.$resolve = resolve;
|
|
129
145
|
Result.$nonNullable = nonNullable;
|
|
130
146
|
Result.$truthy = truthy;
|
|
147
|
+
Result.$union = union;
|
|
131
148
|
Result.$safe = safe;
|
|
132
149
|
Result.$safeAsync = safeAsync;
|
|
133
150
|
Result.$safePromise = safePromise;
|
|
@@ -161,6 +178,15 @@ function truthy(value, error = mapTrue) {
|
|
|
161
178
|
}
|
|
162
179
|
return Err(error());
|
|
163
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
|
+
}
|
|
164
190
|
function safe(f, mapError = ensureError) {
|
|
165
191
|
try {
|
|
166
192
|
return Ok(f());
|
package/dist/index.d.cts
CHANGED
|
@@ -62,6 +62,18 @@ export declare class RetupleInvalidResultError extends Error {
|
|
|
62
62
|
value: unknown[];
|
|
63
63
|
constructor(value: unknown[]);
|
|
64
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
|
+
}
|
|
65
77
|
/**
|
|
66
78
|
* ## Retuple Array Method Unavailable Error
|
|
67
79
|
*
|
|
@@ -83,6 +95,7 @@ export declare namespace Result {
|
|
|
83
95
|
var $resolve: typeof resolve;
|
|
84
96
|
var $nonNullable: typeof nonNullable;
|
|
85
97
|
var $truthy: typeof truthy;
|
|
98
|
+
var $union: typeof union;
|
|
86
99
|
var $safe: typeof safe;
|
|
87
100
|
var $safeAsync: typeof safeAsync;
|
|
88
101
|
var $safePromise: typeof safePromise;
|
|
@@ -224,6 +237,7 @@ declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNu
|
|
|
224
237
|
*/
|
|
225
238
|
declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
|
|
226
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>;
|
|
227
241
|
/**
|
|
228
242
|
* Construct a {@link Result} from a synchronous function call. If the function
|
|
229
243
|
* returns without throwing, the result is `Ok`.
|
|
@@ -939,6 +953,16 @@ type OkTuple<T> = [err: undefined, value: T];
|
|
|
939
953
|
type ErrTuple<E> = [err: E, value: undefined];
|
|
940
954
|
type ThisOk<T> = OkTuple<T> & Retuple<T, never>;
|
|
941
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
|
+
};
|
|
942
966
|
type RetupleAwaitable<T, E> = Retuple<T, E> | PromiseLike<Retuple<T, E>>;
|
|
943
967
|
interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
|
|
944
968
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -62,6 +62,18 @@ export declare class RetupleInvalidResultError extends Error {
|
|
|
62
62
|
value: unknown[];
|
|
63
63
|
constructor(value: unknown[]);
|
|
64
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
|
+
}
|
|
65
77
|
/**
|
|
66
78
|
* ## Retuple Array Method Unavailable Error
|
|
67
79
|
*
|
|
@@ -83,6 +95,7 @@ export declare namespace Result {
|
|
|
83
95
|
var $resolve: typeof resolve;
|
|
84
96
|
var $nonNullable: typeof nonNullable;
|
|
85
97
|
var $truthy: typeof truthy;
|
|
98
|
+
var $union: typeof union;
|
|
86
99
|
var $safe: typeof safe;
|
|
87
100
|
var $safeAsync: typeof safeAsync;
|
|
88
101
|
var $safePromise: typeof safePromise;
|
|
@@ -224,6 +237,7 @@ declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNu
|
|
|
224
237
|
*/
|
|
225
238
|
declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
|
|
226
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>;
|
|
227
241
|
/**
|
|
228
242
|
* Construct a {@link Result} from a synchronous function call. If the function
|
|
229
243
|
* returns without throwing, the result is `Ok`.
|
|
@@ -939,6 +953,16 @@ type OkTuple<T> = [err: undefined, value: T];
|
|
|
939
953
|
type ErrTuple<E> = [err: E, value: undefined];
|
|
940
954
|
type ThisOk<T> = OkTuple<T> & Retuple<T, never>;
|
|
941
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
|
+
};
|
|
942
966
|
type RetupleAwaitable<T, E> = Retuple<T, E> | PromiseLike<Retuple<T, E>>;
|
|
943
967
|
interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
|
|
944
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
|
*
|
|
@@ -115,6 +130,7 @@ Result.Err = Err;
|
|
|
115
130
|
Result.$resolve = resolve;
|
|
116
131
|
Result.$nonNullable = nonNullable;
|
|
117
132
|
Result.$truthy = truthy;
|
|
133
|
+
Result.$union = union;
|
|
118
134
|
Result.$safe = safe;
|
|
119
135
|
Result.$safeAsync = safeAsync;
|
|
120
136
|
Result.$safePromise = safePromise;
|
|
@@ -148,6 +164,15 @@ function truthy(value, error = mapTrue) {
|
|
|
148
164
|
}
|
|
149
165
|
return Err(error());
|
|
150
166
|
}
|
|
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
|
+
}
|
|
151
176
|
function safe(f, mapError = ensureError) {
|
|
152
177
|
try {
|
|
153
178
|
return Ok(f());
|