@zokugun/xtry 0.6.1 → 0.6.3

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 (58) hide show
  1. package/lib/cjs/async.cjs +23 -0
  2. package/lib/cjs/async.d.cts +6 -0
  3. package/lib/cjs/defer.cjs +43 -0
  4. package/lib/cjs/defer.d.cts +19 -0
  5. package/lib/cjs/index.cjs +29 -0
  6. package/lib/cjs/index.d.cts +6 -0
  7. package/lib/cjs/partial.cjs +53 -0
  8. package/lib/cjs/partial.d.cts +30 -0
  9. package/lib/cjs/result.cjs +28 -0
  10. package/lib/cjs/result.d.cts +21 -0
  11. package/lib/cjs/stringify-error.cjs +14 -0
  12. package/lib/cjs/stringify-error.d.cts +4 -0
  13. package/lib/cjs/sync.cjs +23 -0
  14. package/lib/cjs/sync.d.cts +6 -0
  15. package/lib/cjs/try.cjs +51 -0
  16. package/lib/cjs/try.d.cts +12 -0
  17. package/lib/cjs/utils/is-promise-like.cjs +8 -0
  18. package/lib/cjs/utils/types.d.cts +6 -0
  19. package/lib/esm/async.d.ts +6 -0
  20. package/lib/esm/async.js +7 -5
  21. package/lib/esm/defer.d.ts +19 -0
  22. package/lib/esm/defer.js +38 -42
  23. package/lib/esm/index.d.ts +6 -0
  24. package/lib/esm/index.js +7 -5
  25. package/lib/esm/partial.d.ts +30 -0
  26. package/lib/esm/partial.js +38 -37
  27. package/lib/esm/result.d.ts +21 -0
  28. package/lib/esm/result.js +20 -16
  29. package/lib/esm/stringify-error.d.ts +4 -0
  30. package/lib/esm/stringify-error.js +12 -17
  31. package/lib/esm/sync.d.ts +6 -0
  32. package/lib/esm/sync.js +7 -5
  33. package/lib/esm/try.d.ts +12 -0
  34. package/lib/esm/try.js +46 -62
  35. package/lib/esm/utils/is-promise-like.js +6 -2
  36. package/lib/esm/utils/types.d.ts +6 -0
  37. package/package.json +9 -25
  38. package/lib/cjs/async.js +0 -25
  39. package/lib/cjs/defer.js +0 -50
  40. package/lib/cjs/index.js +0 -31
  41. package/lib/cjs/partial.js +0 -52
  42. package/lib/cjs/result.js +0 -23
  43. package/lib/cjs/stringify-error.js +0 -21
  44. package/lib/cjs/sync.js +0 -25
  45. package/lib/cjs/try.js +0 -70
  46. package/lib/cjs/utils/is-promise-like.js +0 -6
  47. package/lib/cjs/utils/types.js +0 -2
  48. package/lib/esm/utils/types.js +0 -1
  49. package/lib/types/async.d.ts +0 -7
  50. package/lib/types/defer.d.ts +0 -16
  51. package/lib/types/index.d.ts +0 -7
  52. package/lib/types/partial.d.ts +0 -27
  53. package/lib/types/result.d.ts +0 -18
  54. package/lib/types/stringify-error.d.ts +0 -1
  55. package/lib/types/sync.d.ts +0 -7
  56. package/lib/types/try.d.ts +0 -9
  57. package/lib/types/utils/is-promise-like.d.ts +0 -3
  58. package/lib/types/utils/types.d.ts +0 -5
package/lib/cjs/sync.js DELETED
@@ -1,25 +0,0 @@
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.js");
5
- Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdeferSync; } });
6
- var partial_js_1 = require("./partial.js");
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.js");
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.js");
23
- Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
24
- var try_js_1 = require("./try.js");
25
- Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
package/lib/cjs/try.js DELETED
@@ -1,70 +0,0 @@
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.js");
7
- const is_promise_like_js_1 = require("./utils/is-promise-like.js");
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
- }
@@ -1,6 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +0,0 @@
1
- export {};
@@ -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';
@@ -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 {};
@@ -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';
@@ -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 {};
@@ -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;
@@ -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';
@@ -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,3 +0,0 @@
1
- type MaybePromise<T> = T | PromiseLike<T>;
2
- export declare function isPromiseLike<T>(value: MaybePromise<T>): value is PromiseLike<T>;
3
- 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>;