@zokugun/xtry 0.6.3 → 0.7.0
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/README.md +18 -3
- package/lib/cjs/async.cjs +35 -23
- package/lib/cjs/async.d.cts +8 -6
- package/lib/cjs/defer.cjs +44 -37
- package/lib/cjs/defer.d.cts +15 -15
- package/lib/cjs/index.cjs +35 -29
- package/lib/cjs/index.d.cts +8 -6
- package/lib/cjs/partial.cjs +40 -41
- package/lib/cjs/partial.d.cts +24 -27
- package/lib/cjs/result.cjs +19 -24
- package/lib/cjs/result.d.cts +16 -19
- package/lib/cjs/stringify-error.cjs +19 -12
- package/lib/cjs/stringify-error.d.cts +1 -4
- package/lib/cjs/sync.cjs +35 -23
- package/lib/cjs/sync.d.cts +8 -6
- package/lib/cjs/to-string-failure.cjs +8 -0
- package/lib/cjs/to-string-failure.d.cts +2 -0
- package/lib/cjs/try.cjs +126 -48
- package/lib/cjs/try.d.cts +15 -10
- package/lib/cjs/utils/is-async-iterable.cjs +6 -0
- package/lib/cjs/utils/is-async-iterable.d.cts +1 -0
- package/lib/cjs/utils/is-promise-like.cjs +4 -6
- package/lib/cjs/utils/is-promise-like.d.cts +3 -0
- package/lib/cjs/utils/is-sync-iterable.cjs +6 -0
- package/lib/cjs/utils/is-sync-iterable.d.cts +1 -0
- package/lib/cjs/utils/types.cjs +2 -0
- package/lib/cjs/utils/types.d.cts +5 -6
- package/lib/esm/async.d.mts +8 -0
- package/lib/esm/async.mjs +6 -0
- package/lib/esm/defer.d.mts +19 -0
- package/lib/esm/defer.mjs +45 -0
- package/lib/esm/index.d.mts +8 -0
- package/lib/esm/index.mjs +6 -0
- package/lib/esm/partial.d.mts +27 -0
- package/lib/esm/partial.mjs +43 -0
- package/lib/esm/result.d.mts +18 -0
- package/lib/esm/result.mjs +18 -0
- package/lib/esm/stringify-error.d.mts +1 -0
- package/lib/esm/stringify-error.mjs +18 -0
- package/lib/esm/sync.d.mts +8 -0
- package/lib/esm/sync.mjs +6 -0
- package/lib/esm/to-string-failure.d.mts +2 -0
- package/lib/esm/to-string-failure.mjs +5 -0
- package/lib/esm/try.d.mts +17 -0
- package/lib/esm/try.mjs +122 -0
- package/lib/esm/utils/is-async-iterable.d.mts +1 -0
- package/lib/esm/utils/is-async-iterable.mjs +3 -0
- package/lib/esm/utils/is-promise-like.d.mts +3 -0
- package/lib/esm/utils/is-promise-like.mjs +3 -0
- package/lib/esm/utils/is-sync-iterable.d.mts +1 -0
- package/lib/esm/utils/is-sync-iterable.mjs +3 -0
- package/lib/esm/utils/types.d.mts +5 -0
- package/lib/esm/utils/types.mjs +1 -0
- package/package.json +31 -16
- package/lib/esm/async.d.ts +0 -6
- package/lib/esm/async.js +0 -7
- package/lib/esm/defer.d.ts +0 -19
- package/lib/esm/defer.js +0 -41
- package/lib/esm/index.d.ts +0 -6
- package/lib/esm/index.js +0 -7
- package/lib/esm/partial.d.ts +0 -30
- package/lib/esm/partial.js +0 -44
- package/lib/esm/result.d.ts +0 -21
- package/lib/esm/result.js +0 -22
- package/lib/esm/stringify-error.d.ts +0 -4
- package/lib/esm/stringify-error.js +0 -13
- package/lib/esm/sync.d.ts +0 -6
- package/lib/esm/sync.js +0 -7
- package/lib/esm/try.d.ts +0 -12
- package/lib/esm/try.js +0 -49
- package/lib/esm/utils/is-promise-like.js +0 -7
- package/lib/esm/utils/types.d.ts +0 -6
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Result } from './result.mjs';
|
|
2
|
+
import { type MaybePromise, type NonPromiseCallback, type PromiseCallback } from './utils/types.mjs';
|
|
3
|
+
type AsyncIterableCallback<T> = (() => AsyncIterable<T>) | AsyncIterable<T>;
|
|
4
|
+
type AsyncIterableResult<T, E> = AsyncIterable<VoidableResult<T, E>>;
|
|
5
|
+
type ErrorHandler<T> = (error: unknown) => T | undefined;
|
|
6
|
+
type SyncIterableCallback<T> = (() => Iterable<T>) | Iterable<T>;
|
|
7
|
+
type SyncIterableResult<T, E> = Iterable<VoidableResult<T, E>>;
|
|
8
|
+
type VoidableResult<T, E> = E extends void ? Result<T, unknown> : Result<T, E>;
|
|
9
|
+
export declare function xtry<T, E = unknown>(iterable: SyncIterableCallback<T>, handler?: ErrorHandler<E>): SyncIterableResult<T, E>;
|
|
10
|
+
export declare function xtry<T, E = unknown>(iterable: AsyncIterableCallback<T>, handler?: ErrorHandler<E>): AsyncIterableResult<T, E>;
|
|
11
|
+
export declare function xtry<T, E = unknown>(func: NonPromiseCallback<T>, handler?: ErrorHandler<E>): VoidableResult<T, E>;
|
|
12
|
+
export declare function xtry<T, E = unknown>(func: PromiseCallback<T>, handler?: ErrorHandler<E>): Promise<VoidableResult<T, E>>;
|
|
13
|
+
export declare function xtrySync<T, E = unknown>(func: NonPromiseCallback<T>, handler?: ErrorHandler<E>): VoidableResult<T, E>;
|
|
14
|
+
export declare function xtryAsync<T, E = unknown>(func: PromiseCallback<T>, handler?: ErrorHandler<E>): Promise<VoidableResult<T, E>>;
|
|
15
|
+
export declare function xtrySyncIterable<T, E = unknown>(iterable: Iterable<T> | (() => Iterable<T>), handler?: ErrorHandler<E>): Iterable<VoidableResult<T, E>>;
|
|
16
|
+
export declare function xtryAsyncIterable<T, E = unknown>(iterable: AsyncIterable<T> | Promise<AsyncIterable<T>> | (() => MaybePromise<AsyncIterable<T>>), handler?: ErrorHandler<E>): AsyncIterable<VoidableResult<T, E>>;
|
|
17
|
+
export {};
|
package/lib/esm/try.mjs
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { err, ok } from './result.mjs';
|
|
2
|
+
import { isAsyncIterable } from './utils/is-async-iterable.mjs';
|
|
3
|
+
import { isPromiseLike } from './utils/is-promise-like.mjs';
|
|
4
|
+
import { isSyncIterable } from './utils/is-sync-iterable.mjs';
|
|
5
|
+
function handleError(handler, error) {
|
|
6
|
+
if (handler) {
|
|
7
|
+
const newError = handler(error);
|
|
8
|
+
if (newError !== undefined) {
|
|
9
|
+
return err(newError);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return err(error);
|
|
13
|
+
} // }}}
|
|
14
|
+
export function xtry(func, handler) {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
16
|
+
const run = func instanceof Function ? func : () => func;
|
|
17
|
+
try {
|
|
18
|
+
const value = run();
|
|
19
|
+
if (isPromiseLike(value)) {
|
|
20
|
+
return Promise.resolve(value).then(
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
22
|
+
(value) => ok(value), (error) => handleError(handler, error));
|
|
23
|
+
}
|
|
24
|
+
if (isAsyncIterable(value)) {
|
|
25
|
+
return xtryAsyncIterable(value, handler);
|
|
26
|
+
}
|
|
27
|
+
if (isSyncIterable(value)) {
|
|
28
|
+
return xtrySyncIterable(value, handler);
|
|
29
|
+
}
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
31
|
+
return ok(value);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
return handleError(handler, error);
|
|
35
|
+
}
|
|
36
|
+
} // }}}
|
|
37
|
+
export function xtrySync(func, handler) {
|
|
38
|
+
try {
|
|
39
|
+
const value = func();
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
41
|
+
return ok(value);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
return handleError(handler, error);
|
|
45
|
+
}
|
|
46
|
+
} // }}}
|
|
47
|
+
export async function xtryAsync(func, handler) {
|
|
48
|
+
try {
|
|
49
|
+
const value = await (func instanceof Promise ? func : Promise.resolve().then(func));
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
51
|
+
return ok(value);
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
return handleError(handler, error);
|
|
55
|
+
}
|
|
56
|
+
} // }}}
|
|
57
|
+
export function xtrySyncIterable(iterable, handler) {
|
|
58
|
+
const generator = iterable instanceof Function ? iterable : () => iterable;
|
|
59
|
+
function* iterate() {
|
|
60
|
+
let source;
|
|
61
|
+
try {
|
|
62
|
+
source = generator();
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
yield handleError(handler, error);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (!source || typeof source[Symbol.iterator] !== 'function') {
|
|
69
|
+
yield handleError(handler, new TypeError('xtryIterable expects an Iterable'));
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const iterator = source[Symbol.iterator]();
|
|
73
|
+
while (true) {
|
|
74
|
+
let step;
|
|
75
|
+
try {
|
|
76
|
+
step = iterator.next();
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
yield handleError(handler, error);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (step.done) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
yield ok(step.value);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return iterate();
|
|
89
|
+
} // }}}
|
|
90
|
+
export function xtryAsyncIterable(iterable, handler) {
|
|
91
|
+
const generator = iterable instanceof Function ? iterable : async () => iterable;
|
|
92
|
+
async function* iterate() {
|
|
93
|
+
let source;
|
|
94
|
+
try {
|
|
95
|
+
source = await Promise.resolve(generator());
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
yield handleError(handler, error);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (!source || typeof source[Symbol.asyncIterator] !== 'function') {
|
|
102
|
+
yield handleError(handler, new TypeError('xtryAsyncIterable expects an AsyncIterable'));
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
106
|
+
while (true) {
|
|
107
|
+
let step;
|
|
108
|
+
try {
|
|
109
|
+
step = await iterator.next();
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
yield handleError(handler, error);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (step.done) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
yield ok(step.value);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return iterate();
|
|
122
|
+
} // }}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isAsyncIterable<T>(item: unknown): item is AsyncIterableIterator<T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isSyncIterable<T>(item: unknown): item is IterableIterator<T>;
|
|
@@ -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.
|
|
4
|
+
"version": "0.7.0",
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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": "
|
|
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
|
|
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
|
|
50
|
-
"watch:
|
|
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"
|
package/lib/esm/async.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 { 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 };
|
package/lib/esm/defer.d.ts
DELETED
|
@@ -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 };
|
package/lib/esm/index.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 { 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 };
|
package/lib/esm/partial.d.ts
DELETED
|
@@ -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 };
|
package/lib/esm/partial.js
DELETED
|
@@ -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 };
|
package/lib/esm/result.d.ts
DELETED
|
@@ -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,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 };
|
package/lib/esm/utils/types.d.ts
DELETED
|
@@ -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 };
|