@zokugun/xtry 0.6.3 → 0.6.4

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