@zokugun/xtry 0.6.0 → 0.6.2
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/lib/cjs/index.cjs +29 -0
- package/lib/esm/async.d.ts +6 -0
- package/lib/esm/async.js +7 -5
- package/lib/esm/defer.d.ts +19 -0
- package/lib/esm/defer.js +38 -42
- package/lib/esm/index.d.ts +6 -0
- package/lib/esm/index.js +7 -5
- package/lib/esm/partial.d.ts +30 -0
- package/lib/esm/partial.js +38 -37
- package/lib/esm/result.d.ts +21 -0
- package/lib/esm/result.js +20 -16
- package/lib/esm/stringify-error.d.ts +4 -0
- package/lib/esm/stringify-error.js +12 -17
- package/lib/esm/sync.d.ts +6 -0
- package/lib/esm/sync.js +7 -5
- package/lib/esm/try.d.ts +12 -0
- package/lib/esm/try.js +46 -62
- package/lib/esm/utils/is-promise-like.js +6 -2
- package/lib/esm/utils/types.d.ts +6 -0
- package/package.json +9 -25
- package/lib/esm/utils/types.js +0 -1
- package/lib/types/async.d.ts +0 -7
- package/lib/types/defer.d.ts +0 -16
- package/lib/types/index.d.ts +0 -7
- package/lib/types/partial.d.ts +0 -27
- package/lib/types/result.d.ts +0 -18
- package/lib/types/stringify-error.d.ts +0 -1
- package/lib/types/sync.d.ts +0 -7
- package/lib/types/try.d.ts +0 -9
- package/lib/types/utils/is-promise-like.d.ts +0 -3
- package/lib/types/utils/types.d.ts +0 -5
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const require__home_runner_work_node_xtry_node_xtry_src_result = require('./result.cjs');
|
|
2
|
+
const require__home_runner_work_node_xtry_node_xtry_src_defer = require('./defer.cjs');
|
|
3
|
+
const require__home_runner_work_node_xtry_node_xtry_src_partial = require('./partial.cjs');
|
|
4
|
+
const require__home_runner_work_node_xtry_node_xtry_src_stringify_error = require('./stringify-error.cjs');
|
|
5
|
+
const require__home_runner_work_node_xtry_node_xtry_src_try = require('./try.cjs');
|
|
6
|
+
|
|
7
|
+
exports.OK = require__home_runner_work_node_xtry_node_xtry_src_result.OK;
|
|
8
|
+
exports.OK_FALSE = require__home_runner_work_node_xtry_node_xtry_src_result.OK_FALSE;
|
|
9
|
+
exports.OK_NULL = require__home_runner_work_node_xtry_node_xtry_src_result.OK_NULL;
|
|
10
|
+
exports.OK_TRUE = require__home_runner_work_node_xtry_node_xtry_src_result.OK_TRUE;
|
|
11
|
+
exports.YOK = require__home_runner_work_node_xtry_node_xtry_src_partial.YOK;
|
|
12
|
+
exports.YOK_FALSE = require__home_runner_work_node_xtry_node_xtry_src_partial.YOK_FALSE;
|
|
13
|
+
exports.YOK_NULL = require__home_runner_work_node_xtry_node_xtry_src_partial.YOK_NULL;
|
|
14
|
+
exports.YOK_TRUE = require__home_runner_work_node_xtry_node_xtry_src_partial.YOK_TRUE;
|
|
15
|
+
exports.err = require__home_runner_work_node_xtry_node_xtry_src_result.err;
|
|
16
|
+
exports.ok = require__home_runner_work_node_xtry_node_xtry_src_result.ok;
|
|
17
|
+
exports.stringifyError = require__home_runner_work_node_xtry_node_xtry_src_stringify_error.stringifyError;
|
|
18
|
+
exports.xdefer = require__home_runner_work_node_xtry_node_xtry_src_defer.xdefer;
|
|
19
|
+
exports.xdeferAsync = require__home_runner_work_node_xtry_node_xtry_src_defer.xdeferAsync;
|
|
20
|
+
exports.xdeferSync = require__home_runner_work_node_xtry_node_xtry_src_defer.xdeferSync;
|
|
21
|
+
exports.xtry = require__home_runner_work_node_xtry_node_xtry_src_try.xtry;
|
|
22
|
+
exports.xtryAsync = require__home_runner_work_node_xtry_node_xtry_src_try.xtryAsync;
|
|
23
|
+
exports.xtrySync = require__home_runner_work_node_xtry_node_xtry_src_try.xtrySync;
|
|
24
|
+
exports.yep = require__home_runner_work_node_xtry_node_xtry_src_partial.yep;
|
|
25
|
+
exports.yerr = require__home_runner_work_node_xtry_node_xtry_src_partial.yerr;
|
|
26
|
+
exports.yok = require__home_runner_work_node_xtry_node_xtry_src_partial.yok;
|
|
27
|
+
exports.yres = require__home_runner_work_node_xtry_node_xtry_src_partial.yres;
|
|
28
|
+
exports.yresAsync = require__home_runner_work_node_xtry_node_xtry_src_partial.yresAsync;
|
|
29
|
+
exports.yresSync = require__home_runner_work_node_xtry_node_xtry_src_partial.yresSync;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Failure, OK$1 as OK, OK_FALSE$1 as OK_FALSE, OK_NULL$1 as OK_NULL, OK_TRUE$1 as OK_TRUE, Result, Success, err$1 as err, ok$1 as ok } from "./result.js";
|
|
2
|
+
import { xdeferAsync$1 as xdeferAsync } from "./defer.js";
|
|
3
|
+
import { YFailure, YOK$1 as YOK, YOK_FALSE$1 as YOK_FALSE, YOK_NULL$1 as YOK_NULL, YOK_TRUE$1 as YOK_TRUE, YResult, YSuccess, yep$1 as yep, yerr$1 as yerr, yok$1 as yok, yresAsync$1 as yresAsync } from "./partial.js";
|
|
4
|
+
import { stringifyError$1 as stringifyError } from "./stringify-error.js";
|
|
5
|
+
import { xtryAsync$1 as xtryAsync } from "./try.js";
|
|
6
|
+
export { type Failure, OK, OK_FALSE, OK_NULL, OK_TRUE, type Result, type Success, type YFailure, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, type YResult, type YSuccess, err, ok, stringifyError, xdeferAsync as xdefer, xtryAsync as xtry, yep, yerr, yok, yresAsync as yres };
|
package/lib/esm/async.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { OK, OK_FALSE, OK_NULL, OK_TRUE, err, ok } from "./result.js";
|
|
2
|
+
import { xdeferAsync } from "./defer.js";
|
|
3
|
+
import { YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, yep, yerr, yok, yresAsync } from "./partial.js";
|
|
4
|
+
import { stringifyError } from "./stringify-error.js";
|
|
5
|
+
import { xtryAsync } from "./try.js";
|
|
6
|
+
|
|
7
|
+
export { OK, OK_FALSE, OK_NULL, OK_TRUE, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, err, ok, stringifyError, xdeferAsync as xdefer, xtryAsync as xtry, yep, yerr, yok, yresAsync as yres };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Result, Success } from "./result.js";
|
|
2
|
+
import { NonPromiseCallback, PromiseCallback } from "./utils/types.js";
|
|
3
|
+
|
|
4
|
+
//#region src/defer.d.ts
|
|
5
|
+
type DeferAsync<E> = {
|
|
6
|
+
(): Promise<Success<void>>;
|
|
7
|
+
(result: UnknownResult<E>): Promise<UnknownResult<E>>;
|
|
8
|
+
};
|
|
9
|
+
type DeferSync<E> = {
|
|
10
|
+
(): Success<void>;
|
|
11
|
+
(result: UnknownResult<E>): UnknownResult<E>;
|
|
12
|
+
};
|
|
13
|
+
type UnknownResult<E> = Result<unknown, E>;
|
|
14
|
+
declare function xdefer<E>(callback: NonPromiseCallback<UnknownResult<E>>): DeferSync<E>;
|
|
15
|
+
declare function xdefer<E>(callback: PromiseCallback<UnknownResult<E>>): DeferAsync<E>;
|
|
16
|
+
declare function xdeferAsync<E>(callback: PromiseCallback<UnknownResult<E>>): DeferAsync<E>;
|
|
17
|
+
declare function xdeferSync<E>(callback: NonPromiseCallback<UnknownResult<E>>): DeferSync<E>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { xdefer as xdefer$1, xdeferAsync as xdeferAsync$1, xdeferSync as xdeferSync$1 };
|
package/lib/esm/defer.js
CHANGED
|
@@ -1,45 +1,41 @@
|
|
|
1
|
-
import { ok } from
|
|
2
|
-
import { isPromiseLike } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
return finalize(deferredValue);
|
|
20
|
-
});
|
|
1
|
+
import { ok } from "./result.js";
|
|
2
|
+
import { isPromiseLike } from "./utils/is-promise-like.js";
|
|
3
|
+
|
|
4
|
+
//#region src/defer.ts
|
|
5
|
+
function xdefer(callback) {
|
|
6
|
+
return (result) => {
|
|
7
|
+
const finalize = (deferResult) => {
|
|
8
|
+
if (deferResult.fails) {
|
|
9
|
+
if (result?.fails) return result;
|
|
10
|
+
return deferResult;
|
|
11
|
+
}
|
|
12
|
+
return result ?? ok();
|
|
13
|
+
};
|
|
14
|
+
const deferredValue = callback instanceof Function ? callback() : callback;
|
|
15
|
+
if (isPromiseLike(deferredValue)) return Promise.resolve(deferredValue).then(finalize);
|
|
16
|
+
return finalize(deferredValue);
|
|
17
|
+
};
|
|
21
18
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return result ?? ok();
|
|
32
|
-
});
|
|
19
|
+
function xdeferAsync(callback) {
|
|
20
|
+
return async (result) => {
|
|
21
|
+
const deferResult = await (callback instanceof Promise ? callback : callback());
|
|
22
|
+
if (deferResult.fails) {
|
|
23
|
+
if (result?.fails) return result;
|
|
24
|
+
return deferResult;
|
|
25
|
+
}
|
|
26
|
+
return result ?? ok();
|
|
27
|
+
};
|
|
33
28
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return result ?? ok();
|
|
44
|
-
});
|
|
29
|
+
function xdeferSync(callback) {
|
|
30
|
+
return (result) => {
|
|
31
|
+
const deferResult = callback();
|
|
32
|
+
if (deferResult.fails) {
|
|
33
|
+
if (result?.fails) return result;
|
|
34
|
+
return deferResult;
|
|
35
|
+
}
|
|
36
|
+
return result ?? ok();
|
|
37
|
+
};
|
|
45
38
|
}
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
export { xdefer, xdeferAsync, xdeferSync };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Failure, OK$1 as OK, OK_FALSE$1 as OK_FALSE, OK_NULL$1 as OK_NULL, OK_TRUE$1 as OK_TRUE, Result, Success, err$1 as err, ok$1 as ok } from "./result.js";
|
|
2
|
+
import { xdefer$1 as xdefer, xdeferAsync$1 as xdeferAsync, xdeferSync$1 as xdeferSync } from "./defer.js";
|
|
3
|
+
import { YFailure, YOK$1 as YOK, YOK_FALSE$1 as YOK_FALSE, YOK_NULL$1 as YOK_NULL, YOK_TRUE$1 as YOK_TRUE, YResult, YSuccess, yep$1 as yep, yerr$1 as yerr, yok$1 as yok, yres$1 as yres, yresAsync$1 as yresAsync, yresSync$1 as yresSync } from "./partial.js";
|
|
4
|
+
import { stringifyError$1 as stringifyError } from "./stringify-error.js";
|
|
5
|
+
import { xtry$1 as xtry, xtryAsync$1 as xtryAsync, xtrySync$1 as xtrySync } from "./try.js";
|
|
6
|
+
export { type Failure, OK, OK_FALSE, OK_NULL, OK_TRUE, type Result, type Success, type YFailure, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, type YResult, type YSuccess, err, ok, stringifyError, xdefer, xdeferAsync, xdeferSync, xtry, xtryAsync, xtrySync, yep, yerr, yok, yres, yresAsync, yresSync };
|
package/lib/esm/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { OK, OK_FALSE, OK_NULL, OK_TRUE, err, ok } from "./result.js";
|
|
2
|
+
import { xdefer, xdeferAsync, xdeferSync } from "./defer.js";
|
|
3
|
+
import { YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, yep, yerr, yok, yres, yresAsync, yresSync } from "./partial.js";
|
|
4
|
+
import { stringifyError } from "./stringify-error.js";
|
|
5
|
+
import { xtry, xtryAsync, xtrySync } from "./try.js";
|
|
6
|
+
|
|
7
|
+
export { OK, OK_FALSE, OK_NULL, OK_TRUE, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, err, ok, stringifyError, xdefer, xdeferAsync, xdeferSync, xtry, xtryAsync, xtrySync, yep, yerr, yok, yres, yresAsync, yresSync };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Failure, Result, Success } from "./result.js";
|
|
2
|
+
import { NotPromise } from "./utils/types.js";
|
|
3
|
+
|
|
4
|
+
//#region src/partial.d.ts
|
|
5
|
+
type YResult<T, E, M> = Failure<E> | YSuccess<T> | YFailure<M>;
|
|
6
|
+
type YSuccess<T> = Success<T> & {
|
|
7
|
+
success: true;
|
|
8
|
+
};
|
|
9
|
+
type YFailure<M> = {
|
|
10
|
+
fails: false;
|
|
11
|
+
success: false;
|
|
12
|
+
miscue: M;
|
|
13
|
+
value: undefined;
|
|
14
|
+
error: undefined;
|
|
15
|
+
};
|
|
16
|
+
declare function yok(): YSuccess<void>;
|
|
17
|
+
declare function yok<T>(value: T): YSuccess<T>;
|
|
18
|
+
declare function yerr<M>(miscue: M): YFailure<M>;
|
|
19
|
+
type YRResult<T, E> = Failure<E> | YSuccess<T>;
|
|
20
|
+
declare function yres<T, E>(result: NotPromise<Result<T, E>>): YRResult<T, E>;
|
|
21
|
+
declare function yres<T, E>(result: Promise<Result<T, E>>): Promise<YRResult<T, E>>;
|
|
22
|
+
declare function yresSync<T, E>(result: NotPromise<Result<T, E>>): YRResult<T, E>;
|
|
23
|
+
declare function yresAsync<T, E>(promise: Promise<Result<T, E>>): Promise<YRResult<T, E>>;
|
|
24
|
+
declare function yep<T>(result: Success<T>): YSuccess<T>;
|
|
25
|
+
declare const YOK: Readonly<YSuccess<void>>;
|
|
26
|
+
declare const YOK_NULL: Readonly<YSuccess<any>>;
|
|
27
|
+
declare const YOK_TRUE: Readonly<YSuccess<boolean>>;
|
|
28
|
+
declare const YOK_FALSE: Readonly<YSuccess<boolean>>;
|
|
29
|
+
//#endregion
|
|
30
|
+
export { YFailure, YOK as YOK$1, YOK_FALSE as YOK_FALSE$1, YOK_NULL as YOK_NULL$1, YOK_TRUE as YOK_TRUE$1, YResult, YSuccess, yep as yep$1, yerr as yerr$1, yok as yok$1, yres as yres$1, yresAsync as yresAsync$1, yresSync as yresSync$1 };
|
package/lib/esm/partial.js
CHANGED
|
@@ -1,43 +1,44 @@
|
|
|
1
|
-
import { isPromiseLike } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { isPromiseLike } from "./utils/is-promise-like.js";
|
|
2
|
+
|
|
3
|
+
//#region src/partial.ts
|
|
4
|
+
function yok(value) {
|
|
5
|
+
return {
|
|
6
|
+
fails: false,
|
|
7
|
+
success: true,
|
|
8
|
+
value,
|
|
9
|
+
error: void 0
|
|
10
|
+
};
|
|
9
11
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
function yerr(miscue) {
|
|
13
|
+
return {
|
|
14
|
+
fails: false,
|
|
15
|
+
success: false,
|
|
16
|
+
miscue,
|
|
17
|
+
value: void 0,
|
|
18
|
+
error: void 0
|
|
19
|
+
};
|
|
18
20
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
return yresSync(result);
|
|
21
|
+
function yres(result) {
|
|
22
|
+
if (isPromiseLike(result)) return result.then(yresSync);
|
|
23
|
+
return yresSync(result);
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
return yep(result);
|
|
25
|
+
function yresSync(result) {
|
|
26
|
+
if (result.fails) return result;
|
|
27
|
+
return yep(result);
|
|
30
28
|
}
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
async function yresAsync(promise) {
|
|
30
|
+
return promise.then(yresSync);
|
|
33
31
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
function yep(result) {
|
|
33
|
+
return {
|
|
34
|
+
...result,
|
|
35
|
+
success: true
|
|
36
|
+
};
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
const YOK = Object.freeze(yok());
|
|
39
|
+
const YOK_NULL = Object.freeze(yok(null));
|
|
40
|
+
const YOK_TRUE = Object.freeze(yok(true));
|
|
41
|
+
const YOK_FALSE = Object.freeze(yok(false));
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
export { YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, yep, yerr, yok, yres, yresAsync, yresSync };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/result.d.ts
|
|
2
|
+
type Success<T> = {
|
|
3
|
+
fails: false;
|
|
4
|
+
value: T;
|
|
5
|
+
error: undefined;
|
|
6
|
+
};
|
|
7
|
+
type Failure<E> = {
|
|
8
|
+
fails: true;
|
|
9
|
+
value: undefined;
|
|
10
|
+
error: E;
|
|
11
|
+
};
|
|
12
|
+
type Result<T, E> = Success<T> | Failure<E>;
|
|
13
|
+
declare function ok(): Success<void>;
|
|
14
|
+
declare function ok<T>(value: T): Success<T>;
|
|
15
|
+
declare function err<E>(error: E): Failure<E>;
|
|
16
|
+
declare const OK: Readonly<Success<void>>;
|
|
17
|
+
declare const OK_NULL: Readonly<Success<any>>;
|
|
18
|
+
declare const OK_TRUE: Readonly<Success<boolean>>;
|
|
19
|
+
declare const OK_FALSE: Readonly<Success<boolean>>;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { Failure, OK as OK$1, OK_FALSE as OK_FALSE$1, OK_NULL as OK_NULL$1, OK_TRUE as OK_TRUE$1, Result, Success, err as err$1, ok as ok$1 };
|
package/lib/esm/result.js
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
//#region src/result.ts
|
|
2
|
+
function ok(value) {
|
|
3
|
+
return {
|
|
4
|
+
fails: false,
|
|
5
|
+
value,
|
|
6
|
+
error: void 0
|
|
7
|
+
};
|
|
7
8
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
function err(error) {
|
|
10
|
+
return {
|
|
11
|
+
fails: true,
|
|
12
|
+
value: void 0,
|
|
13
|
+
error
|
|
14
|
+
};
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
const OK = Object.freeze(ok());
|
|
17
|
+
const OK_NULL = Object.freeze(ok(null));
|
|
18
|
+
const OK_TRUE = Object.freeze(ok(true));
|
|
19
|
+
const OK_FALSE = Object.freeze(ok(false));
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { OK, OK_FALSE, OK_NULL, OK_TRUE, err, ok };
|
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (typeof json === 'string') {
|
|
11
|
-
return json;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
catch {
|
|
15
|
-
// fallthrough
|
|
16
|
-
}
|
|
17
|
-
return String(error);
|
|
1
|
+
//#region src/stringify-error.ts
|
|
2
|
+
function stringifyError(error) {
|
|
3
|
+
if (typeof error === "string") return error;
|
|
4
|
+
if (error instanceof Error) return error.message ?? String(error);
|
|
5
|
+
try {
|
|
6
|
+
const json = JSON.stringify(error);
|
|
7
|
+
if (typeof json === "string") return json;
|
|
8
|
+
} catch {}
|
|
9
|
+
return String(error);
|
|
18
10
|
}
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { stringifyError };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Failure, OK$1 as OK, OK_FALSE$1 as OK_FALSE, OK_NULL$1 as OK_NULL, OK_TRUE$1 as OK_TRUE, Result, Success, err$1 as err, ok$1 as ok } from "./result.js";
|
|
2
|
+
import { xdeferSync$1 as xdeferSync } from "./defer.js";
|
|
3
|
+
import { YFailure, YOK$1 as YOK, YOK_FALSE$1 as YOK_FALSE, YOK_NULL$1 as YOK_NULL, YOK_TRUE$1 as YOK_TRUE, YResult, YSuccess, yep$1 as yep, yerr$1 as yerr, yok$1 as yok, yresSync$1 as yresSync } from "./partial.js";
|
|
4
|
+
import { stringifyError$1 as stringifyError } from "./stringify-error.js";
|
|
5
|
+
import { xtrySync$1 as xtrySync } from "./try.js";
|
|
6
|
+
export { type Failure, OK, OK_FALSE, OK_NULL, OK_TRUE, type Result, type Success, type YFailure, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, type YResult, type YSuccess, err, ok, stringifyError, xdeferSync as xdefer, xtrySync as xtry, yep, yerr, yok, yresSync as yres };
|
package/lib/esm/sync.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { OK, OK_FALSE, OK_NULL, OK_TRUE, err, ok } from "./result.js";
|
|
2
|
+
import { xdeferSync } from "./defer.js";
|
|
3
|
+
import { YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, yep, yerr, yok, yresSync } from "./partial.js";
|
|
4
|
+
import { stringifyError } from "./stringify-error.js";
|
|
5
|
+
import { xtrySync } from "./try.js";
|
|
6
|
+
|
|
7
|
+
export { OK, OK_FALSE, OK_NULL, OK_TRUE, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, err, ok, stringifyError, xdeferSync as xdefer, xtrySync as xtry, yep, yerr, yok, yresSync as yres };
|
package/lib/esm/try.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Result } from "./result.js";
|
|
2
|
+
import { NonPromiseCallback, PromiseCallback } from "./utils/types.js";
|
|
3
|
+
|
|
4
|
+
//#region src/try.d.ts
|
|
5
|
+
type ErrorHandler<T> = (error: unknown) => T | undefined;
|
|
6
|
+
type VoidableResult<T, E> = E extends void ? Result<T, unknown> : Result<T, E>;
|
|
7
|
+
declare function xtry<T, E = unknown>(func: NonPromiseCallback<T>, handler?: ErrorHandler<E>): VoidableResult<T, E>;
|
|
8
|
+
declare function xtry<T, E = unknown>(func: PromiseCallback<T>, handler?: ErrorHandler<E>): Promise<VoidableResult<T, E>>;
|
|
9
|
+
declare function xtrySync<T, E = unknown>(func: NonPromiseCallback<T>, handler?: ErrorHandler<E>): VoidableResult<T, E>;
|
|
10
|
+
declare function xtryAsync<T, E = unknown>(func: PromiseCallback<T>, handler?: ErrorHandler<E>): Promise<VoidableResult<T, E>>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { xtry as xtry$1, xtryAsync as xtryAsync$1, xtrySync as xtrySync$1 };
|
package/lib/esm/try.js
CHANGED
|
@@ -1,65 +1,49 @@
|
|
|
1
|
-
import { err, ok } from
|
|
2
|
-
import { isPromiseLike } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
23
|
-
return ok(value);
|
|
24
|
-
}
|
|
25
|
-
catch (error) {
|
|
26
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
27
|
-
return handleError(error);
|
|
28
|
-
}
|
|
1
|
+
import { err, ok } from "./result.js";
|
|
2
|
+
import { isPromiseLike } from "./utils/is-promise-like.js";
|
|
3
|
+
|
|
4
|
+
//#region src/try.ts
|
|
5
|
+
function xtry(func, handler) {
|
|
6
|
+
const run = func instanceof Function ? func : () => func;
|
|
7
|
+
const handleError = (error) => {
|
|
8
|
+
if (handler) {
|
|
9
|
+
const newError = handler(error);
|
|
10
|
+
if (newError !== void 0) return err(newError);
|
|
11
|
+
}
|
|
12
|
+
return err(error);
|
|
13
|
+
};
|
|
14
|
+
const handleSuccess = (value) => ok(value);
|
|
15
|
+
try {
|
|
16
|
+
const value = run();
|
|
17
|
+
if (isPromiseLike(value)) return Promise.resolve(value).then(handleSuccess, handleError);
|
|
18
|
+
return ok(value);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
return handleError(error);
|
|
21
|
+
}
|
|
29
22
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return err(newError);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
45
|
-
return err(error);
|
|
46
|
-
}
|
|
23
|
+
function xtrySync(func, handler) {
|
|
24
|
+
try {
|
|
25
|
+
const value = func();
|
|
26
|
+
return ok(value);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (handler) {
|
|
29
|
+
const newError = handler(error);
|
|
30
|
+
if (newError !== void 0) return err(newError);
|
|
31
|
+
}
|
|
32
|
+
return err(error);
|
|
33
|
+
}
|
|
47
34
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return err(newError);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
63
|
-
return err(error);
|
|
64
|
-
}
|
|
35
|
+
async function xtryAsync(func, handler) {
|
|
36
|
+
try {
|
|
37
|
+
const value = await (func instanceof Promise ? func : Promise.resolve().then(func));
|
|
38
|
+
return ok(value);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (handler) {
|
|
41
|
+
const newError = handler(error);
|
|
42
|
+
if (newError !== void 0) return err(newError);
|
|
43
|
+
}
|
|
44
|
+
return err(error);
|
|
45
|
+
}
|
|
65
46
|
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
export { xtry, xtryAsync, xtrySync };
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/utils/is-promise-like.ts
|
|
2
|
+
function isPromiseLike(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
3
4
|
}
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
7
|
+
export { isPromiseLike };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
//#region src/utils/types.d.ts
|
|
2
|
+
type NotPromise<T> = Exclude<T, Promise<unknown>>;
|
|
3
|
+
type NonPromiseCallback<T> = () => NotPromise<T>;
|
|
4
|
+
type PromiseCallback<T> = (() => Promise<T>) | Promise<T>;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { NonPromiseCallback, NotPromise, PromiseCallback };
|
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.6.
|
|
4
|
+
"version": "0.6.2",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Baptiste Augrain",
|
|
7
7
|
"email": "daiyam@zokugun.org"
|
|
@@ -18,54 +18,37 @@
|
|
|
18
18
|
"type": "module",
|
|
19
19
|
"exports": {
|
|
20
20
|
".": {
|
|
21
|
-
"types": "./lib/types/index.d.ts",
|
|
22
21
|
"import": "./lib/esm/index.js",
|
|
23
|
-
"require": "./lib/cjs/index.
|
|
22
|
+
"require": "./lib/cjs/index.cjs",
|
|
24
23
|
"default": "./lib/esm/index.js"
|
|
25
24
|
},
|
|
26
25
|
"./async": {
|
|
27
|
-
"types": "./lib/types/async.d.ts",
|
|
28
26
|
"import": "./lib/esm/async.js",
|
|
29
|
-
"require": "./lib/cjs/async.
|
|
27
|
+
"require": "./lib/cjs/async.cjs",
|
|
30
28
|
"default": "./lib/esm/async.js"
|
|
31
29
|
},
|
|
32
30
|
"./sync": {
|
|
33
|
-
"types": "./lib/types/sync.d.ts",
|
|
34
31
|
"import": "./lib/esm/sync.js",
|
|
35
|
-
"require": "./lib/cjs/sync.
|
|
32
|
+
"require": "./lib/cjs/sync.cjs",
|
|
36
33
|
"default": "./lib/esm/sync.js"
|
|
37
34
|
}
|
|
38
35
|
},
|
|
39
|
-
"main": "lib/cjs/index.
|
|
36
|
+
"main": "lib/cjs/index.cjs",
|
|
40
37
|
"module": "lib/esm/index.js",
|
|
41
|
-
"types": "lib/types/index.d.ts",
|
|
42
|
-
"typesVersions": {
|
|
43
|
-
"*": {
|
|
44
|
-
"async": [
|
|
45
|
-
"./lib/types/async.d.ts"
|
|
46
|
-
],
|
|
47
|
-
"sync": [
|
|
48
|
-
"./lib/types/sync.d.ts"
|
|
49
|
-
]
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
38
|
"scripts": {
|
|
53
|
-
"build": "
|
|
54
|
-
"build:cjs": "tsc -p src/tsconfig.cjs.json",
|
|
55
|
-
"build:esm": "tsc -p src/tsconfig.json",
|
|
39
|
+
"build": "tsdown",
|
|
56
40
|
"clean": "rimraf lib .test",
|
|
57
41
|
"commit": "cz",
|
|
58
|
-
"compile": "tsc -p src",
|
|
42
|
+
"compile": "tsc -p src/tsconfig.json",
|
|
59
43
|
"lint": "xo",
|
|
60
44
|
"lint:fix": "xo --fix",
|
|
61
45
|
"prepare": "husky; fixpack || true",
|
|
62
|
-
"prepublishOnly": "npm run
|
|
46
|
+
"prepublishOnly": "npm run build",
|
|
63
47
|
"release": "release-it",
|
|
64
48
|
"test": "vitest run",
|
|
65
49
|
"watch:source": "tsc-watch -p src",
|
|
66
50
|
"watch:test": "vitest"
|
|
67
51
|
},
|
|
68
|
-
"dependencies": {},
|
|
69
52
|
"devDependencies": {
|
|
70
53
|
"@commitlint/cli": "^19.7.1",
|
|
71
54
|
"@commitlint/config-conventional": "^19.7.1",
|
|
@@ -79,6 +62,7 @@
|
|
|
79
62
|
"lint-staged": "^16.1.4",
|
|
80
63
|
"release-it": "^18.1.2",
|
|
81
64
|
"tsc-watch": "^6.3.0",
|
|
65
|
+
"tsdown": "^0.12.9",
|
|
82
66
|
"typescript": "^5.7.3",
|
|
83
67
|
"vitest": "^4.0.16",
|
|
84
68
|
"xo": "0.60.0"
|
package/lib/esm/utils/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/types/async.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { xdeferAsync as xdefer } from './defer.js';
|
|
2
|
-
export type { YResult, YSuccess, YFailure } from './partial.js';
|
|
3
|
-
export { yok, yerr, yresAsync as yres, yep, YOK, YOK_NULL, YOK_TRUE, YOK_FALSE } from './partial.js';
|
|
4
|
-
export type { Success, Failure, Result } from './result.js';
|
|
5
|
-
export { ok, err, OK, OK_NULL, OK_TRUE, OK_FALSE } from './result.js';
|
|
6
|
-
export { stringifyError } from './stringify-error.js';
|
|
7
|
-
export { xtryAsync as xtry } from './try.js';
|
package/lib/types/defer.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { type Result, type Success } from './result.js';
|
|
2
|
-
import { type NonPromiseCallback, type PromiseCallback } from './utils/types.js';
|
|
3
|
-
type DeferAsync<E> = {
|
|
4
|
-
(): Promise<Success<void>>;
|
|
5
|
-
(result: UnknownResult<E>): Promise<UnknownResult<E>>;
|
|
6
|
-
};
|
|
7
|
-
type DeferSync<E> = {
|
|
8
|
-
(): Success<void>;
|
|
9
|
-
(result: UnknownResult<E>): UnknownResult<E>;
|
|
10
|
-
};
|
|
11
|
-
type UnknownResult<E> = Result<unknown, E>;
|
|
12
|
-
export declare function xdefer<E>(callback: NonPromiseCallback<UnknownResult<E>>): DeferSync<E>;
|
|
13
|
-
export declare function xdefer<E>(callback: PromiseCallback<UnknownResult<E>>): DeferAsync<E>;
|
|
14
|
-
export declare function xdeferAsync<E>(callback: PromiseCallback<UnknownResult<E>>): DeferAsync<E>;
|
|
15
|
-
export declare function xdeferSync<E>(callback: NonPromiseCallback<UnknownResult<E>>): DeferSync<E>;
|
|
16
|
-
export {};
|
package/lib/types/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { xdefer, xdeferAsync, xdeferSync } from './defer.js';
|
|
2
|
-
export type { YResult, YSuccess, YFailure } from './partial.js';
|
|
3
|
-
export { yok, yerr, yres, yresAsync, yresSync, yep, YOK, YOK_NULL, YOK_TRUE, YOK_FALSE } from './partial.js';
|
|
4
|
-
export type { Success, Failure, Result } from './result.js';
|
|
5
|
-
export { ok, err, OK, OK_NULL, OK_TRUE, OK_FALSE } from './result.js';
|
|
6
|
-
export { stringifyError } from './stringify-error.js';
|
|
7
|
-
export { xtry, xtryAsync, xtrySync } from './try.js';
|
package/lib/types/partial.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { type Result, type Failure, type Success } from './result.js';
|
|
2
|
-
import { type NotPromise } from './utils/types.js';
|
|
3
|
-
export type YResult<T, E, M> = Failure<E> | YSuccess<T> | YFailure<M>;
|
|
4
|
-
export type YSuccess<T> = Success<T> & {
|
|
5
|
-
success: true;
|
|
6
|
-
};
|
|
7
|
-
export type YFailure<M> = {
|
|
8
|
-
fails: false;
|
|
9
|
-
success: false;
|
|
10
|
-
miscue: M;
|
|
11
|
-
value: undefined;
|
|
12
|
-
error: undefined;
|
|
13
|
-
};
|
|
14
|
-
export declare function yok(): YSuccess<void>;
|
|
15
|
-
export declare function yok<T>(value: T): YSuccess<T>;
|
|
16
|
-
export declare function yerr<M>(miscue: M): YFailure<M>;
|
|
17
|
-
type YRResult<T, E> = Failure<E> | YSuccess<T>;
|
|
18
|
-
export declare function yres<T, E>(result: NotPromise<Result<T, E>>): YRResult<T, E>;
|
|
19
|
-
export declare function yres<T, E>(result: Promise<Result<T, E>>): Promise<YRResult<T, E>>;
|
|
20
|
-
export declare function yresSync<T, E>(result: NotPromise<Result<T, E>>): YRResult<T, E>;
|
|
21
|
-
export declare function yresAsync<T, E>(promise: Promise<Result<T, E>>): Promise<YRResult<T, E>>;
|
|
22
|
-
export declare function yep<T>(result: Success<T>): YSuccess<T>;
|
|
23
|
-
export declare const YOK: Readonly<YSuccess<void>>;
|
|
24
|
-
export declare const YOK_NULL: Readonly<YSuccess<null>>;
|
|
25
|
-
export declare const YOK_TRUE: Readonly<YSuccess<boolean>>;
|
|
26
|
-
export declare const YOK_FALSE: Readonly<YSuccess<boolean>>;
|
|
27
|
-
export {};
|
package/lib/types/result.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export type Success<T> = {
|
|
2
|
-
fails: false;
|
|
3
|
-
value: T;
|
|
4
|
-
error: undefined;
|
|
5
|
-
};
|
|
6
|
-
export type Failure<E> = {
|
|
7
|
-
fails: true;
|
|
8
|
-
value: undefined;
|
|
9
|
-
error: E;
|
|
10
|
-
};
|
|
11
|
-
export type Result<T, E> = Success<T> | Failure<E>;
|
|
12
|
-
export declare function ok(): Success<void>;
|
|
13
|
-
export declare function ok<T>(value: T): Success<T>;
|
|
14
|
-
export declare function err<E>(error: E): Failure<E>;
|
|
15
|
-
export declare const OK: Readonly<Success<void>>;
|
|
16
|
-
export declare const OK_NULL: Readonly<Success<null>>;
|
|
17
|
-
export declare const OK_TRUE: Readonly<Success<boolean>>;
|
|
18
|
-
export declare const OK_FALSE: Readonly<Success<boolean>>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function stringifyError(error: unknown): string;
|
package/lib/types/sync.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { xdeferSync as xdefer } from './defer.js';
|
|
2
|
-
export type { YResult, YSuccess, YFailure } from './partial.js';
|
|
3
|
-
export { yok, yerr, yresSync as yres, yep, YOK, YOK_NULL, YOK_TRUE, YOK_FALSE } from './partial.js';
|
|
4
|
-
export type { Success, Failure, Result } from './result.js';
|
|
5
|
-
export { ok, err, OK, OK_NULL, OK_TRUE, OK_FALSE } from './result.js';
|
|
6
|
-
export { stringifyError } from './stringify-error.js';
|
|
7
|
-
export { xtrySync as xtry } from './try.js';
|
package/lib/types/try.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { type Result } from './result.js';
|
|
2
|
-
import { type NonPromiseCallback, type PromiseCallback } from './utils/types.js';
|
|
3
|
-
type ErrorHandler<T> = (error: unknown) => T | undefined;
|
|
4
|
-
type VoidableResult<T, E> = E extends void ? Result<T, unknown> : Result<T, E>;
|
|
5
|
-
export declare function xtry<T, E = unknown>(func: NonPromiseCallback<T>, handler?: ErrorHandler<E>): VoidableResult<T, E>;
|
|
6
|
-
export declare function xtry<T, E = unknown>(func: PromiseCallback<T>, handler?: ErrorHandler<E>): Promise<VoidableResult<T, E>>;
|
|
7
|
-
export declare function xtrySync<T, E = unknown>(func: NonPromiseCallback<T>, handler?: ErrorHandler<E>): VoidableResult<T, E>;
|
|
8
|
-
export declare function xtryAsync<T, E = unknown>(func: PromiseCallback<T>, handler?: ErrorHandler<E>): Promise<VoidableResult<T, E>>;
|
|
9
|
-
export {};
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export type NotPromise<T> = Exclude<T, Promise<unknown>>;
|
|
2
|
-
export type NonPromiseCallback<T> = () => NotPromise<T>;
|
|
3
|
-
export type PromiseCallback<T> = (() => Promise<T>) | Promise<T>;
|
|
4
|
-
export type Callback<T> = NonPromiseCallback<T> | PromiseCallback<T>;
|
|
5
|
-
export type MaybePromise<T> = T | Promise<T>;
|