@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 CHANGED
@@ -119,21 +119,21 @@ API reference
119
119
  ### Result helpers
120
120
 
121
121
  ```typescript
122
- export type Success<T> = { fails: false; value: T; error: null };
123
- export type Failure<E> = { fails: true; value: null; error: E };
124
- export type Result<T, E> = Success<T> | Failure<E>;
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
- export function ok<T>(value: T): Success<T>;
127
- export function err<E>(error: E): Failure<E>;
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
- export function xtry<T, E>(func: () => Exclude<T, Promise<unknown>>, handler?: (error: unknown) => void | E): Result<T, E>;
134
- export function xatry<T, E>(func: (() => Exclude<T, Promise<unknown>>) | Promise<Exclude<T, Promise<unknown>>>, handler?: (error: unknown) => void | E): Promise<Result<T, E>>;
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
- export function stringifyError(error: unknown): string;
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
- export type YSuccess<T> = Success<T> & { success: true };
149
- export type YFailure<S> = { fails: false; success: false; type: S; value: null; error: null };
150
- export type YResult<T, E, S> = Failure<E> | YSuccess<T> | YFailure<S>;
151
-
152
- export function yok<T>(value: T): YSuccess<T>;
153
- export function yerr<S>(type: S): YFailure<S>;
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, S> = Failure<E> | YSuccess<T> | YFailure<S>;
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<S> = {
6
+ export type YFailure<M> = {
7
7
  fails: false;
8
8
  success: false;
9
- type: S;
10
- value: null;
11
- error: null;
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<S>(type: S): YFailure<S>;
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: null,
6
+ error: undefined,
7
7
  };
8
8
  }
9
- export function yerr(type) {
9
+ export function yerr(miscue) {
10
10
  return {
11
11
  fails: false,
12
12
  success: false,
13
- type,
14
- value: null,
15
- error: null,
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: null;
4
+ error: undefined;
5
5
  };
6
6
  export type Failure<E> = {
7
7
  fails: true;
8
- value: null;
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
@@ -1,14 +1,14 @@
1
1
  export function ok(value) {
2
2
  return {
3
3
  fails: false,
4
- value,
5
- error: null,
4
+ value: value,
5
+ error: undefined,
6
6
  };
7
7
  }
8
8
  export function err(error) {
9
9
  return {
10
10
  fails: true,
11
- value: null,
11
+ value: undefined,
12
12
  error,
13
13
  };
14
14
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zokugun/xtry",
3
3
  "description": "simple try/catch wrapper returning Result",
4
- "version": "0.3.0",
4
+ "version": "0.4.1",
5
5
  "author": {
6
6
  "name": "Baptiste Augrain",
7
7
  "email": "daiyam@zokugun.org"