@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,19 +0,0 @@
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 DELETED
@@ -1,41 +0,0 @@
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
- };
18
- }
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
- };
28
- }
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
- };
38
- }
39
-
40
- //#endregion
41
- export { xdefer, xdeferAsync, xdeferSync };
@@ -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 { 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 DELETED
@@ -1,7 +0,0 @@
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 };
@@ -1,30 +0,0 @@
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 };
@@ -1,44 +0,0 @@
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
- };
11
- }
12
- function yerr(miscue) {
13
- return {
14
- fails: false,
15
- success: false,
16
- miscue,
17
- value: void 0,
18
- error: void 0
19
- };
20
- }
21
- function yres(result) {
22
- if (isPromiseLike(result)) return result.then(yresSync);
23
- return yresSync(result);
24
- }
25
- function yresSync(result) {
26
- if (result.fails) return result;
27
- return yep(result);
28
- }
29
- async function yresAsync(promise) {
30
- return promise.then(yresSync);
31
- }
32
- function yep(result) {
33
- return {
34
- ...result,
35
- success: true
36
- };
37
- }
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 };
@@ -1,21 +0,0 @@
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 DELETED
@@ -1,22 +0,0 @@
1
- //#region src/result.ts
2
- function ok(value) {
3
- return {
4
- fails: false,
5
- value,
6
- error: void 0
7
- };
8
- }
9
- function err(error) {
10
- return {
11
- fails: true,
12
- value: void 0,
13
- error
14
- };
15
- }
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,4 +0,0 @@
1
- //#region src/stringify-error.d.ts
2
- declare function stringifyError(error: unknown): string;
3
- //#endregion
4
- export { stringifyError as stringifyError$1 };
@@ -1,13 +0,0 @@
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);
10
- }
11
-
12
- //#endregion
13
- export { stringifyError };
package/lib/esm/sync.d.ts DELETED
@@ -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 { 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 DELETED
@@ -1,7 +0,0 @@
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 DELETED
@@ -1,12 +0,0 @@
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 DELETED
@@ -1,49 +0,0 @@
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
- }
22
- }
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
- }
34
- }
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
- }
46
- }
47
-
48
- //#endregion
49
- export { xtry, xtryAsync, xtrySync };
@@ -1,7 +0,0 @@
1
- //#region src/utils/is-promise-like.ts
2
- function isPromiseLike(value) {
3
- return typeof value === "object" && value !== null && typeof value.then === "function";
4
- }
5
-
6
- //#endregion
7
- export { isPromiseLike };
@@ -1,6 +0,0 @@
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 };