@zokugun/xtry 0.6.2 → 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 -0
  2. package/lib/cjs/async.d.cts +7 -0
  3. package/lib/cjs/defer.cjs +50 -0
  4. package/lib/cjs/defer.d.cts +19 -0
  5. package/lib/cjs/index.cjs +31 -29
  6. package/lib/cjs/index.d.cts +7 -0
  7. package/lib/cjs/partial.cjs +52 -0
  8. package/lib/cjs/partial.d.cts +27 -0
  9. package/lib/cjs/result.cjs +23 -0
  10. package/lib/cjs/result.d.cts +18 -0
  11. package/lib/cjs/stringify-error.cjs +21 -0
  12. package/lib/cjs/stringify-error.d.cts +1 -0
  13. package/lib/cjs/sync.cjs +25 -0
  14. package/lib/cjs/sync.d.cts +7 -0
  15. package/lib/cjs/try.cjs +70 -0
  16. package/lib/cjs/try.d.cts +9 -0
  17. package/lib/cjs/utils/is-promise-like.cjs +6 -0
  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 -0
  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 +32 -17
  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
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.xtry = exports.stringifyError = exports.OK_FALSE = exports.OK_TRUE = exports.OK_NULL = exports.OK = exports.err = exports.ok = exports.YOK_FALSE = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK = exports.yep = exports.yres = exports.yerr = exports.yok = exports.xdefer = void 0;
4
+ var defer_js_1 = require("./defer.cjs");
5
+ Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdeferAsync; } });
6
+ var partial_js_1 = require("./partial.cjs");
7
+ Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
8
+ Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
9
+ Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yresAsync; } });
10
+ Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
11
+ Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
12
+ Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
13
+ Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
14
+ Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
15
+ var result_js_1 = require("./result.cjs");
16
+ Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
17
+ Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
18
+ Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
19
+ Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
20
+ Object.defineProperty(exports, "OK_TRUE", { enumerable: true, get: function () { return result_js_1.OK_TRUE; } });
21
+ Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
22
+ var stringify_error_js_1 = require("./stringify-error.cjs");
23
+ Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
24
+ var try_js_1 = require("./try.cjs");
25
+ Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtryAsync; } });
@@ -0,0 +1,7 @@
1
+ export { xdeferAsync as xdefer } from './defer.cjs';
2
+ export type { YResult, YSuccess, YFailure } from './partial.cjs';
3
+ export { yok, yerr, yresAsync 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 { xtryAsync as xtry } from './try.cjs';
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.xdefer = xdefer;
4
+ exports.xdeferAsync = xdeferAsync;
5
+ exports.xdeferSync = xdeferSync;
6
+ const result_js_1 = require("./result.cjs");
7
+ const is_promise_like_js_1 = require("./utils/is-promise-like.cjs");
8
+ function xdefer(callback) {
9
+ // eslint-disable-next-line @typescript-eslint/promise-function-async
10
+ return ((result) => {
11
+ const finalize = (deferResult) => {
12
+ if (deferResult.fails) {
13
+ if ((result)?.fails) {
14
+ return result;
15
+ }
16
+ return deferResult;
17
+ }
18
+ return (result) ?? (0, result_js_1.ok)();
19
+ };
20
+ const deferredValue = callback instanceof Function ? callback() : callback;
21
+ if ((0, is_promise_like_js_1.isPromiseLike)(deferredValue)) {
22
+ return Promise.resolve(deferredValue).then(finalize);
23
+ }
24
+ return finalize(deferredValue);
25
+ });
26
+ }
27
+ function xdeferAsync(callback) {
28
+ return (async (result) => {
29
+ const deferResult = await (callback instanceof Promise ? callback : callback());
30
+ if (deferResult.fails) {
31
+ if (result?.fails) {
32
+ return result;
33
+ }
34
+ return deferResult;
35
+ }
36
+ return result ?? (0, result_js_1.ok)();
37
+ });
38
+ }
39
+ function xdeferSync(callback) {
40
+ return ((result) => {
41
+ const deferResult = callback();
42
+ if (deferResult.fails) {
43
+ if (result?.fails) {
44
+ return result;
45
+ }
46
+ return deferResult;
47
+ }
48
+ return result ?? (0, result_js_1.ok)();
49
+ });
50
+ }
@@ -0,0 +1,19 @@
1
+ import { type Failure, type Result, type Success } from './result.cjs';
2
+ import { type NonPromiseCallback, type PromiseCallback } from './utils/types.cjs';
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 {};
package/lib/cjs/index.cjs CHANGED
@@ -1,29 +1,31 @@
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;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.xtrySync = exports.xtryAsync = exports.xtry = exports.stringifyError = exports.OK_FALSE = exports.OK_TRUE = exports.OK_NULL = exports.OK = exports.err = exports.ok = exports.YOK_FALSE = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK = exports.yep = exports.yresSync = exports.yresAsync = exports.yres = exports.yerr = exports.yok = exports.xdeferSync = exports.xdeferAsync = exports.xdefer = void 0;
4
+ var defer_js_1 = require("./defer.cjs");
5
+ Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdefer; } });
6
+ Object.defineProperty(exports, "xdeferAsync", { enumerable: true, get: function () { return defer_js_1.xdeferAsync; } });
7
+ Object.defineProperty(exports, "xdeferSync", { enumerable: true, get: function () { return defer_js_1.xdeferSync; } });
8
+ var partial_js_1 = require("./partial.cjs");
9
+ Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
10
+ Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
11
+ Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yres; } });
12
+ Object.defineProperty(exports, "yresAsync", { enumerable: true, get: function () { return partial_js_1.yresAsync; } });
13
+ Object.defineProperty(exports, "yresSync", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
14
+ Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
15
+ Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
16
+ Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
17
+ Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
18
+ Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
19
+ var result_js_1 = require("./result.cjs");
20
+ Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
21
+ Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
22
+ Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
23
+ Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
24
+ Object.defineProperty(exports, "OK_TRUE", { enumerable: true, get: function () { return result_js_1.OK_TRUE; } });
25
+ Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
26
+ var stringify_error_js_1 = require("./stringify-error.cjs");
27
+ Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
28
+ var try_js_1 = require("./try.cjs");
29
+ Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtry; } });
30
+ Object.defineProperty(exports, "xtryAsync", { enumerable: true, get: function () { return try_js_1.xtryAsync; } });
31
+ Object.defineProperty(exports, "xtrySync", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
@@ -0,0 +1,7 @@
1
+ export { xdefer, xdeferAsync, xdeferSync } from './defer.cjs';
2
+ export type { YResult, YSuccess, YFailure } from './partial.cjs';
3
+ export { yok, yerr, yres, yresAsync, yresSync, 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 { xtry, xtryAsync, xtrySync } from './try.cjs';
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.YOK_FALSE = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK = void 0;
4
+ exports.yok = yok;
5
+ exports.yerr = yerr;
6
+ exports.yres = yres;
7
+ exports.yresSync = yresSync;
8
+ exports.yresAsync = yresAsync;
9
+ exports.yep = yep;
10
+ const is_promise_like_js_1 = require("./utils/is-promise-like.cjs");
11
+ function yok(value) {
12
+ return {
13
+ fails: false,
14
+ success: true,
15
+ value: value,
16
+ error: undefined,
17
+ };
18
+ }
19
+ function yerr(miscue) {
20
+ return {
21
+ fails: false,
22
+ success: false,
23
+ miscue,
24
+ value: undefined,
25
+ error: undefined,
26
+ };
27
+ }
28
+ function yres(result) {
29
+ if ((0, is_promise_like_js_1.isPromiseLike)(result)) {
30
+ return result.then(yresSync);
31
+ }
32
+ return yresSync(result);
33
+ }
34
+ function yresSync(result) {
35
+ if (result.fails) {
36
+ return result;
37
+ }
38
+ return yep(result);
39
+ }
40
+ async function yresAsync(promise) {
41
+ return promise.then(yresSync);
42
+ }
43
+ function yep(result) {
44
+ return {
45
+ ...result,
46
+ success: true,
47
+ };
48
+ }
49
+ exports.YOK = Object.freeze(yok());
50
+ exports.YOK_NULL = Object.freeze(yok(null));
51
+ exports.YOK_TRUE = Object.freeze(yok(true));
52
+ exports.YOK_FALSE = Object.freeze(yok(false));
@@ -0,0 +1,27 @@
1
+ import { type Result, type Failure, type Success } from './result.cjs';
2
+ import { type NotPromise } from './utils/types.cjs';
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,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OK_FALSE = exports.OK_TRUE = exports.OK_NULL = exports.OK = void 0;
4
+ exports.ok = ok;
5
+ exports.err = err;
6
+ function ok(value) {
7
+ return {
8
+ fails: false,
9
+ value: value,
10
+ error: undefined,
11
+ };
12
+ }
13
+ function err(error) {
14
+ return {
15
+ fails: true,
16
+ value: undefined,
17
+ error,
18
+ };
19
+ }
20
+ exports.OK = Object.freeze(ok());
21
+ exports.OK_NULL = Object.freeze(ok(null));
22
+ exports.OK_TRUE = Object.freeze(ok(true));
23
+ exports.OK_FALSE = Object.freeze(ok(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,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stringifyError = stringifyError;
4
+ function stringifyError(error) {
5
+ if (typeof error === 'string') {
6
+ return error;
7
+ }
8
+ if (error instanceof Error) {
9
+ return error.message ?? String(error);
10
+ }
11
+ try {
12
+ const json = JSON.stringify(error);
13
+ if (typeof json === 'string') {
14
+ return json;
15
+ }
16
+ }
17
+ catch {
18
+ // fallthrough
19
+ }
20
+ return String(error);
21
+ }
@@ -0,0 +1 @@
1
+ export declare function stringifyError(error: unknown): string;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.xtry = exports.stringifyError = exports.OK_FALSE = exports.OK_TRUE = exports.OK_NULL = exports.OK = exports.err = exports.ok = exports.YOK_FALSE = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK = exports.yep = exports.yres = exports.yerr = exports.yok = exports.xdefer = void 0;
4
+ var defer_js_1 = require("./defer.cjs");
5
+ Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdeferSync; } });
6
+ var partial_js_1 = require("./partial.cjs");
7
+ Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
8
+ Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
9
+ Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
10
+ Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
11
+ Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
12
+ Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
13
+ Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
14
+ Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
15
+ var result_js_1 = require("./result.cjs");
16
+ Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
17
+ Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
18
+ Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
19
+ Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
20
+ Object.defineProperty(exports, "OK_TRUE", { enumerable: true, get: function () { return result_js_1.OK_TRUE; } });
21
+ Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
22
+ var stringify_error_js_1 = require("./stringify-error.cjs");
23
+ Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
24
+ var try_js_1 = require("./try.cjs");
25
+ Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
@@ -0,0 +1,7 @@
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';
@@ -0,0 +1,70 @@
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");
8
+ function xtry(func, handler) {
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
+ }
34
+ }
35
+ function xtrySync(func, handler) {
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
+ }
52
+ }
53
+ async function xtryAsync(func, handler) {
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
+ }
70
+ }
@@ -0,0 +1,9 @@
1
+ import { type Result } from './result.cjs';
2
+ import { type NonPromiseCallback, type PromiseCallback } from './utils/types.cjs';
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,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPromiseLike = isPromiseLike;
4
+ function isPromiseLike(value) {
5
+ return typeof value === 'object' && value !== null && typeof value.then === 'function';
6
+ }
@@ -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 });
@@ -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,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 {};