@zokugun/xtry 0.3.0 → 0.4.1
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/README.md +17 -14
- package/lib/partial.d.ts +10 -7
- package/lib/partial.js +20 -5
- package/lib/result.d.ts +3 -2
- package/lib/result.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -119,21 +119,21 @@ API reference
|
|
|
119
119
|
### Result helpers
|
|
120
120
|
|
|
121
121
|
```typescript
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
type Success<T> = { fails: false; value: T; error: undefined };
|
|
123
|
+
type Failure<E> = { fails: true; value: undefined; error: E };
|
|
124
|
+
type Result<T, E> = Success<T> | Failure<E>;
|
|
125
125
|
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
function ok<T>(value: T): Success<T>;
|
|
127
|
+
function err<E>(error: E): Failure<E>;
|
|
128
128
|
```
|
|
129
129
|
|
|
130
130
|
### Try helpers
|
|
131
131
|
|
|
132
132
|
```typescript
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
function xtry<T, E>(func: () => Exclude<T, Promise<unknown>>, handler?: (error: unknown) => void | E): Result<T, E>;
|
|
134
|
+
function xatry<T, E>(func: (() => Exclude<T, Promise<unknown>>) | Promise<Exclude<T, Promise<unknown>>>, handler?: (error: unknown) => void | E): Promise<Result<T, E>>;
|
|
135
135
|
|
|
136
|
-
|
|
136
|
+
function stringifyError(error: unknown): string;
|
|
137
137
|
```
|
|
138
138
|
|
|
139
139
|
Both helpers:
|
|
@@ -145,12 +145,15 @@ Both helpers:
|
|
|
145
145
|
### Partial helpers
|
|
146
146
|
|
|
147
147
|
```typescript
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
148
|
+
type YSuccess<T> = Success<T> & { success: true };
|
|
149
|
+
type YFailure<M> = { fails: false; success: false; miscue: M; value: undefined; error: undefined };
|
|
150
|
+
type YResult<T, E, M> = Failure<E> | YSuccess<T> | YFailure<M>;
|
|
151
|
+
|
|
152
|
+
function yok<T>(value: T): YSuccess<T>;
|
|
153
|
+
function yerr<M>(type: M): YFailure<M>;
|
|
154
|
+
function yress<T, E>(result: Result<T, E>): Failure<E> | YSuccess<T>;
|
|
155
|
+
function yresa<T, E>(promise: Promise<Result<T, E>>): Promise<Failure<E> | YSuccess<T>>;
|
|
156
|
+
function yep<T>(result: Success<T>): YSuccess<T>;
|
|
154
157
|
```
|
|
155
158
|
|
|
156
159
|
These helpers are useful when you need to separate soft rejections (`success: false`) from hard failures (`fails: true`).
|
package/lib/partial.d.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import { type Failure, type Success } from './result.js';
|
|
2
|
-
export type YResult<T, E,
|
|
1
|
+
import { type Result, type Failure, type Success } from './result.js';
|
|
2
|
+
export type YResult<T, E, M> = Failure<E> | YSuccess<T> | YFailure<M>;
|
|
3
3
|
export type YSuccess<T> = Success<T> & {
|
|
4
4
|
success: true;
|
|
5
5
|
};
|
|
6
|
-
export type YFailure<
|
|
6
|
+
export type YFailure<M> = {
|
|
7
7
|
fails: false;
|
|
8
8
|
success: false;
|
|
9
|
-
|
|
10
|
-
value:
|
|
11
|
-
error:
|
|
9
|
+
miscue: M;
|
|
10
|
+
value: undefined;
|
|
11
|
+
error: undefined;
|
|
12
12
|
};
|
|
13
13
|
export declare function yok<T>(value: T): YSuccess<T>;
|
|
14
|
-
export declare function yerr<
|
|
14
|
+
export declare function yerr<M>(miscue: M): YFailure<M>;
|
|
15
|
+
export declare function yress<T, E>(result: Result<T, E>): Failure<E> | YSuccess<T>;
|
|
16
|
+
export declare function yresa<T, E>(promise: Promise<Result<T, E>>): Promise<Failure<E> | YSuccess<T>>;
|
|
17
|
+
export declare function yep<T>(result: Success<T>): YSuccess<T>;
|
package/lib/partial.js
CHANGED
|
@@ -3,15 +3,30 @@ export function yok(value) {
|
|
|
3
3
|
fails: false,
|
|
4
4
|
success: true,
|
|
5
5
|
value,
|
|
6
|
-
error:
|
|
6
|
+
error: undefined,
|
|
7
7
|
};
|
|
8
8
|
}
|
|
9
|
-
export function yerr(
|
|
9
|
+
export function yerr(miscue) {
|
|
10
10
|
return {
|
|
11
11
|
fails: false,
|
|
12
12
|
success: false,
|
|
13
|
-
|
|
14
|
-
value:
|
|
15
|
-
error:
|
|
13
|
+
miscue,
|
|
14
|
+
value: undefined,
|
|
15
|
+
error: undefined,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function yress(result) {
|
|
19
|
+
if (result.fails) {
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
return yep(result);
|
|
23
|
+
}
|
|
24
|
+
export async function yresa(promise) {
|
|
25
|
+
return promise.then(yress);
|
|
26
|
+
}
|
|
27
|
+
export function yep(result) {
|
|
28
|
+
return {
|
|
29
|
+
...result,
|
|
30
|
+
success: true,
|
|
16
31
|
};
|
|
17
32
|
}
|
package/lib/result.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
export type Success<T> = {
|
|
2
2
|
fails: false;
|
|
3
3
|
value: T;
|
|
4
|
-
error:
|
|
4
|
+
error: undefined;
|
|
5
5
|
};
|
|
6
6
|
export type Failure<E> = {
|
|
7
7
|
fails: true;
|
|
8
|
-
value:
|
|
8
|
+
value: undefined;
|
|
9
9
|
error: E;
|
|
10
10
|
};
|
|
11
11
|
export type Result<T, E> = Success<T> | Failure<E>;
|
|
12
|
+
export declare function ok(): Success<void>;
|
|
12
13
|
export declare function ok<T>(value: T): Success<T>;
|
|
13
14
|
export declare function err<E>(error: E): Failure<E>;
|
package/lib/result.js
CHANGED