retuple 1.0.0-next.10 → 1.0.0-next.11
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 +12 -5
- package/dist/index.d.cts +12 -15
- package/dist/index.d.ts +12 -15
- package/dist/index.js +17 -5
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -16,11 +16,6 @@ exports.RetupleArrayMethodUnavailableError = exports.RetupleInvalidResultError =
|
|
|
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
|
*
|
|
@@ -130,6 +125,7 @@ function Result(resultLike) {
|
|
|
130
125
|
}
|
|
131
126
|
Result.Ok = Ok;
|
|
132
127
|
Result.Err = Err;
|
|
128
|
+
Result.$resolve = resolve;
|
|
133
129
|
Result.$nonNullable = nonNullable;
|
|
134
130
|
Result.$truthy = truthy;
|
|
135
131
|
Result.$safe = safe;
|
|
@@ -142,6 +138,17 @@ function Ok(val) {
|
|
|
142
138
|
function Err(err) {
|
|
143
139
|
return new ResultErr(err);
|
|
144
140
|
}
|
|
141
|
+
function resolve(result) {
|
|
142
|
+
if (result instanceof ResultAsync) {
|
|
143
|
+
return result;
|
|
144
|
+
}
|
|
145
|
+
else if (result instanceof ResultOk || result instanceof ResultErr) {
|
|
146
|
+
return result.$async();
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
return new ResultAsync(result);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
145
152
|
function nonNullable(value, error = mapTrue) {
|
|
146
153
|
if (value !== null && value !== undefined) {
|
|
147
154
|
return Ok(value);
|
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
|
/**
|
|
@@ -85,6 +80,7 @@ export declare function Result<R extends [err: null | undefined, value: unknown]
|
|
|
85
80
|
export declare namespace Result {
|
|
86
81
|
var Ok: typeof import(".").Ok;
|
|
87
82
|
var Err: typeof import(".").Err;
|
|
83
|
+
var $resolve: typeof resolve;
|
|
88
84
|
var $nonNullable: typeof nonNullable;
|
|
89
85
|
var $truthy: typeof truthy;
|
|
90
86
|
var $safe: typeof safe;
|
|
@@ -133,6 +129,7 @@ export declare function Ok<const T>(val: T): Result<T, never>;
|
|
|
133
129
|
*/
|
|
134
130
|
export declare function Err(): Result<never, void>;
|
|
135
131
|
export declare function Err<const E>(err: E): Result<never, E>;
|
|
132
|
+
declare function resolve<T, E>(result: Retuple<T, E> | PromiseLike<Retuple<T, E>>): ResultAsync<T, E>;
|
|
136
133
|
/**
|
|
137
134
|
* Construct a {@link Result} from a value. If the value is neither null or
|
|
138
135
|
* undefined, the result is `Ok`.
|
|
@@ -178,8 +175,8 @@ export declare function Err<const E>(err: E): Result<never, E>;
|
|
|
178
175
|
* assert.equal(value, undefined);
|
|
179
176
|
* ```
|
|
180
177
|
*/
|
|
181
|
-
|
|
182
|
-
|
|
178
|
+
declare function nonNullable<const T>(value: T): Result<NonNullable<T>, true>;
|
|
179
|
+
declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
|
|
183
180
|
/**
|
|
184
181
|
* Construct a {@link Result} from a value. If the value is truthy, the result
|
|
185
182
|
* is `Ok`.
|
|
@@ -225,8 +222,8 @@ export declare function nonNullable<const T, E>(value: T, error: () => E): Resul
|
|
|
225
222
|
* assert.equal(value, undefined);
|
|
226
223
|
* ```
|
|
227
224
|
*/
|
|
228
|
-
|
|
229
|
-
|
|
225
|
+
declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
|
|
226
|
+
declare function truthy<const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
|
|
230
227
|
/**
|
|
231
228
|
* Construct a {@link Result} from a synchronous function call. If the function
|
|
232
229
|
* returns without throwing, the result is `Ok`.
|
|
@@ -292,8 +289,8 @@ export declare function truthy<const T, E>(value: T, error: () => E): Result<Tru
|
|
|
292
289
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
293
290
|
* assert.equal(value, undefined);
|
|
294
291
|
*/
|
|
295
|
-
|
|
296
|
-
|
|
292
|
+
declare function safe<T>(f: () => Awaited<T>): Result<T, Error>;
|
|
293
|
+
declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
|
|
297
294
|
/**
|
|
298
295
|
* Construct a {@link ResultAsync} from a function call. If the function returns
|
|
299
296
|
* without throwing, and any promise returned resolves, the result is `Ok`.
|
|
@@ -359,8 +356,8 @@ export declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown)
|
|
|
359
356
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
360
357
|
* assert.equal(value, undefined);
|
|
361
358
|
*/
|
|
362
|
-
|
|
363
|
-
|
|
359
|
+
declare function safeAsync<T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
|
|
360
|
+
declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
|
|
364
361
|
/**
|
|
365
362
|
* Construct a {@link Result} from a promise. If the promise resolves, the
|
|
366
363
|
* result is `Ok`.
|
|
@@ -422,8 +419,8 @@ export declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (
|
|
|
422
419
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
423
420
|
* assert.equal(value, undefined);
|
|
424
421
|
*/
|
|
425
|
-
|
|
426
|
-
|
|
422
|
+
declare function safePromise<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
|
|
423
|
+
declare function safePromise<T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
|
|
427
424
|
/**
|
|
428
425
|
* ## RetupleArray
|
|
429
426
|
*
|
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
|
/**
|
|
@@ -85,6 +80,7 @@ export declare function Result<R extends [err: null | undefined, value: unknown]
|
|
|
85
80
|
export declare namespace Result {
|
|
86
81
|
var Ok: typeof import(".").Ok;
|
|
87
82
|
var Err: typeof import(".").Err;
|
|
83
|
+
var $resolve: typeof resolve;
|
|
88
84
|
var $nonNullable: typeof nonNullable;
|
|
89
85
|
var $truthy: typeof truthy;
|
|
90
86
|
var $safe: typeof safe;
|
|
@@ -133,6 +129,7 @@ export declare function Ok<const T>(val: T): Result<T, never>;
|
|
|
133
129
|
*/
|
|
134
130
|
export declare function Err(): Result<never, void>;
|
|
135
131
|
export declare function Err<const E>(err: E): Result<never, E>;
|
|
132
|
+
declare function resolve<T, E>(result: Retuple<T, E> | PromiseLike<Retuple<T, E>>): ResultAsync<T, E>;
|
|
136
133
|
/**
|
|
137
134
|
* Construct a {@link Result} from a value. If the value is neither null or
|
|
138
135
|
* undefined, the result is `Ok`.
|
|
@@ -178,8 +175,8 @@ export declare function Err<const E>(err: E): Result<never, E>;
|
|
|
178
175
|
* assert.equal(value, undefined);
|
|
179
176
|
* ```
|
|
180
177
|
*/
|
|
181
|
-
|
|
182
|
-
|
|
178
|
+
declare function nonNullable<const T>(value: T): Result<NonNullable<T>, true>;
|
|
179
|
+
declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
|
|
183
180
|
/**
|
|
184
181
|
* Construct a {@link Result} from a value. If the value is truthy, the result
|
|
185
182
|
* is `Ok`.
|
|
@@ -225,8 +222,8 @@ export declare function nonNullable<const T, E>(value: T, error: () => E): Resul
|
|
|
225
222
|
* assert.equal(value, undefined);
|
|
226
223
|
* ```
|
|
227
224
|
*/
|
|
228
|
-
|
|
229
|
-
|
|
225
|
+
declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
|
|
226
|
+
declare function truthy<const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
|
|
230
227
|
/**
|
|
231
228
|
* Construct a {@link Result} from a synchronous function call. If the function
|
|
232
229
|
* returns without throwing, the result is `Ok`.
|
|
@@ -292,8 +289,8 @@ export declare function truthy<const T, E>(value: T, error: () => E): Result<Tru
|
|
|
292
289
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
293
290
|
* assert.equal(value, undefined);
|
|
294
291
|
*/
|
|
295
|
-
|
|
296
|
-
|
|
292
|
+
declare function safe<T>(f: () => Awaited<T>): Result<T, Error>;
|
|
293
|
+
declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
|
|
297
294
|
/**
|
|
298
295
|
* Construct a {@link ResultAsync} from a function call. If the function returns
|
|
299
296
|
* without throwing, and any promise returned resolves, the result is `Ok`.
|
|
@@ -359,8 +356,8 @@ export declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown)
|
|
|
359
356
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
360
357
|
* assert.equal(value, undefined);
|
|
361
358
|
*/
|
|
362
|
-
|
|
363
|
-
|
|
359
|
+
declare function safeAsync<T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
|
|
360
|
+
declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
|
|
364
361
|
/**
|
|
365
362
|
* Construct a {@link Result} from a promise. If the promise resolves, the
|
|
366
363
|
* result is `Ok`.
|
|
@@ -422,8 +419,8 @@ export declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (
|
|
|
422
419
|
* assert(err instanceof RetupleThrownValueError && err.value === "non error");
|
|
423
420
|
* assert.equal(value, undefined);
|
|
424
421
|
*/
|
|
425
|
-
|
|
426
|
-
|
|
422
|
+
declare function safePromise<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
|
|
423
|
+
declare function safePromise<T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
|
|
427
424
|
/**
|
|
428
425
|
* ## RetupleArray
|
|
429
426
|
*
|
package/dist/index.js
CHANGED
|
@@ -112,6 +112,7 @@ export function Result(resultLike) {
|
|
|
112
112
|
}
|
|
113
113
|
Result.Ok = Ok;
|
|
114
114
|
Result.Err = Err;
|
|
115
|
+
Result.$resolve = resolve;
|
|
115
116
|
Result.$nonNullable = nonNullable;
|
|
116
117
|
Result.$truthy = truthy;
|
|
117
118
|
Result.$safe = safe;
|
|
@@ -124,19 +125,30 @@ export function Ok(val) {
|
|
|
124
125
|
export function Err(err) {
|
|
125
126
|
return new ResultErr(err);
|
|
126
127
|
}
|
|
127
|
-
|
|
128
|
+
function resolve(result) {
|
|
129
|
+
if (result instanceof ResultAsync) {
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
else if (result instanceof ResultOk || result instanceof ResultErr) {
|
|
133
|
+
return result.$async();
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
return new ResultAsync(result);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function nonNullable(value, error = mapTrue) {
|
|
128
140
|
if (value !== null && value !== undefined) {
|
|
129
141
|
return Ok(value);
|
|
130
142
|
}
|
|
131
143
|
return Err(error());
|
|
132
144
|
}
|
|
133
|
-
|
|
145
|
+
function truthy(value, error = mapTrue) {
|
|
134
146
|
if (value) {
|
|
135
147
|
return Ok(value);
|
|
136
148
|
}
|
|
137
149
|
return Err(error());
|
|
138
150
|
}
|
|
139
|
-
|
|
151
|
+
function safe(f, mapError = ensureError) {
|
|
140
152
|
try {
|
|
141
153
|
return Ok(f());
|
|
142
154
|
}
|
|
@@ -144,7 +156,7 @@ export function safe(f, mapError = ensureError) {
|
|
|
144
156
|
return Err(mapError(err));
|
|
145
157
|
}
|
|
146
158
|
}
|
|
147
|
-
|
|
159
|
+
function safeAsync(f, mapError = ensureError) {
|
|
148
160
|
return new ResultAsync((async () => {
|
|
149
161
|
try {
|
|
150
162
|
return Ok(await f());
|
|
@@ -154,7 +166,7 @@ export function safeAsync(f, mapError = ensureError) {
|
|
|
154
166
|
}
|
|
155
167
|
})());
|
|
156
168
|
}
|
|
157
|
-
|
|
169
|
+
function safePromise(promise, mapError = ensureError) {
|
|
158
170
|
return new ResultAsync(promise.then((Ok), async (err) => Err(await mapError(err))));
|
|
159
171
|
}
|
|
160
172
|
/**
|