@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
package/README.md
CHANGED
|
@@ -147,17 +147,32 @@ To minimize allocations when returning the same `Success` shape, you can reuse t
|
|
|
147
147
|
|
|
148
148
|
```
|
|
149
149
|
function xtry<T, E>(func: (() => MaybePromise<T>) | Promise<T>, handler?: (error: unknown) => void | E): MaybePromise<Result<T, E>>;
|
|
150
|
-
function
|
|
150
|
+
function xtry<T, E>(iterable: (() => Iterable<T>) | Iterable<T>, handler?: (error: unknown) => void | E): Iterable<Result<T, E>>;
|
|
151
|
+
function xtry<T, E>(iterable: (() => AsyncIterable<T>) | AsyncIterable<T>, handler?: (error: unknown) => void | E): AsyncIterable<Result<T, E>>;
|
|
151
152
|
function xtryAsync<T, E>(func: (() => Promise<T>) | Promise<T>, handler?: (error: unknown) => void | E): Promise<Result<T, E>>;
|
|
153
|
+
function xtryAsyncIterable<T, E>(iterable: (() => MaybePromise<AsyncIterable<T>>) | MaybePromise<AsyncIterable<T>>, handler?: (error: unknown) => void | E): AsyncIterable<Result<T, E>>;
|
|
154
|
+
function xtrySync<T, E>(func: () => Exclude<T, Promise<unknown>>, handler?: (error: unknown) => void | E): Result<T, E>;
|
|
155
|
+
function xtrySyncIterable<T, E>(iterable: (() => Iterable<T>) | Iterable<T>, handler?: (error: unknown) => void | E): Iterable<Result<T, E>>;
|
|
156
|
+
(error: unknown) => void | E): AsyncIterable<Result<T, E>>;
|
|
152
157
|
|
|
153
158
|
function stringifyError(error: unknown): string;
|
|
154
159
|
```
|
|
155
160
|
|
|
156
|
-
`xtry` inspects the supplied value
|
|
161
|
+
`xtry` inspects the supplied value and chooses the matching shape:
|
|
162
|
+
|
|
163
|
+
- promises or async factories → `Promise<Result<…>>`
|
|
164
|
+
- async iterables → `AsyncIterable<Result<…>>`
|
|
165
|
+
- iterators/generators → `Iterable<Result<…>>`
|
|
166
|
+
- everything else → plain `Result<…>`
|
|
167
|
+
|
|
168
|
+
Wrap default collections (arrays, sets, maps, strings, typed arrays, …) inside a function if you need them treated as raw values instead of iterables.<br />
|
|
169
|
+
Reach for `xtrySync`/`xtryAsync` when you want to force a specific mode, and prefer the explicit `xtrySyncIterable`/`xtryAsyncIterable` exports whenever you always expect iterable outputs.<br />
|
|
170
|
+
`xtryIterable` and `xtryAsyncIterable` wrap synchronous or asynchronous iterables, yielding a stream of `Result` values and emitting a single `err(...)` before stopping when the underlying iterator throws or rejects.
|
|
171
|
+
|
|
172
|
+
All helpers:
|
|
157
173
|
|
|
158
174
|
- execute the supplied function and capture thrown values;
|
|
159
175
|
- call the optional `handler` before turning that value into `err(error)`;
|
|
160
|
-
- never throw, making the return signature a reliable discriminated union.
|
|
161
176
|
|
|
162
177
|
### Partial helpers
|
|
163
178
|
|
package/lib/cjs/async.cjs
CHANGED
|
@@ -1,23 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
exports
|
|
8
|
-
|
|
9
|
-
exports
|
|
10
|
-
exports
|
|
11
|
-
exports
|
|
12
|
-
exports
|
|
13
|
-
exports
|
|
14
|
-
exports
|
|
15
|
-
exports
|
|
16
|
-
exports
|
|
17
|
-
exports
|
|
18
|
-
exports
|
|
19
|
-
|
|
20
|
-
exports
|
|
21
|
-
exports
|
|
22
|
-
exports
|
|
23
|
-
exports
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xtrySyncIterable = exports.xtrySync = exports.xtryIterable = exports.xtry = exports.xtryUnknown = exports.toStringFailure = 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.yres = exports.yresUnknown = exports.yerr = exports.yok = exports.xdeferSync = exports.xdefer = exports.xdeferUnknown = void 0;
|
|
4
|
+
var defer_js_1 = require("./defer.cjs");
|
|
5
|
+
Object.defineProperty(exports, "xdeferUnknown", { enumerable: true, get: function () { return defer_js_1.xdefer; } });
|
|
6
|
+
Object.defineProperty(exports, "xdefer", { 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, "yresUnknown", { enumerable: true, get: function () { return partial_js_1.yres; } });
|
|
12
|
+
Object.defineProperty(exports, "yres", { 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 to_string_failure_js_1 = require("./to-string-failure.cjs");
|
|
29
|
+
Object.defineProperty(exports, "toStringFailure", { enumerable: true, get: function () { return to_string_failure_js_1.toStringFailure; } });
|
|
30
|
+
var try_js_1 = require("./try.cjs");
|
|
31
|
+
Object.defineProperty(exports, "xtryUnknown", { enumerable: true, get: function () { return try_js_1.xtry; } });
|
|
32
|
+
Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtryAsync; } });
|
|
33
|
+
Object.defineProperty(exports, "xtryIterable", { enumerable: true, get: function () { return try_js_1.xtryAsyncIterable; } });
|
|
34
|
+
Object.defineProperty(exports, "xtrySync", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
|
|
35
|
+
Object.defineProperty(exports, "xtrySyncIterable", { enumerable: true, get: function () { return try_js_1.xtrySyncIterable; } });
|
package/lib/cjs/async.d.cts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export {
|
|
1
|
+
export { xdefer as xdeferUnknown, xdeferAsync as xdefer, xdeferSync } from './defer.cjs';
|
|
2
|
+
export type { YResult, YSuccess, YFailure } from './partial.cjs';
|
|
3
|
+
export { yok, yerr, yres as yresUnknown, yresAsync as yres, 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 { toStringFailure } from './to-string-failure.cjs';
|
|
8
|
+
export { xtry as xtryUnknown, xtryAsync as xtry, xtryAsyncIterable as xtryIterable, xtrySync, xtrySyncIterable } from './try.cjs';
|
package/lib/cjs/defer.cjs
CHANGED
|
@@ -1,43 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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");
|
|
5
8
|
function xdefer(callback) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
+
});
|
|
18
26
|
}
|
|
19
27
|
function xdeferAsync(callback) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
+
});
|
|
28
38
|
}
|
|
29
39
|
function xdeferSync(callback) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
+
});
|
|
38
50
|
}
|
|
39
|
-
|
|
40
|
-
//#endregion
|
|
41
|
-
exports.xdefer = xdefer;
|
|
42
|
-
exports.xdeferAsync = xdeferAsync;
|
|
43
|
-
exports.xdeferSync = xdeferSync;
|
package/lib/cjs/defer.d.cts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { Result, Success } from
|
|
2
|
-
import { NonPromiseCallback, PromiseCallback } from
|
|
3
|
-
|
|
4
|
-
//#region src/defer.d.ts
|
|
1
|
+
import { type Failure, type Result, type Success } from './result.cjs';
|
|
2
|
+
import { type NonPromiseCallback, type PromiseCallback } from './utils/types.cjs';
|
|
5
3
|
type DeferAsync<E> = {
|
|
6
|
-
|
|
7
|
-
|
|
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
8
|
};
|
|
9
9
|
type DeferSync<E> = {
|
|
10
|
-
|
|
11
|
-
|
|
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>;
|
|
12
14
|
};
|
|
13
|
-
|
|
14
|
-
declare function xdefer<E>(callback:
|
|
15
|
-
declare function
|
|
16
|
-
declare function
|
|
17
|
-
|
|
18
|
-
//#endregion
|
|
19
|
-
export { xdefer, xdeferAsync, xdeferSync };
|
|
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,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
exports
|
|
8
|
-
|
|
9
|
-
exports
|
|
10
|
-
exports
|
|
11
|
-
exports
|
|
12
|
-
exports
|
|
13
|
-
exports
|
|
14
|
-
exports
|
|
15
|
-
exports
|
|
16
|
-
exports
|
|
17
|
-
exports
|
|
18
|
-
exports
|
|
19
|
-
|
|
20
|
-
exports
|
|
21
|
-
exports
|
|
22
|
-
exports
|
|
23
|
-
exports
|
|
24
|
-
exports
|
|
25
|
-
exports
|
|
26
|
-
|
|
27
|
-
exports
|
|
28
|
-
|
|
29
|
-
exports
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xtrySyncIterable = exports.xtrySync = exports.xtryAsyncIterable = exports.xtryAsync = exports.xtry = exports.toStringFailure = 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 to_string_failure_js_1 = require("./to-string-failure.cjs");
|
|
29
|
+
Object.defineProperty(exports, "toStringFailure", { enumerable: true, get: function () { return to_string_failure_js_1.toStringFailure; } });
|
|
30
|
+
var try_js_1 = require("./try.cjs");
|
|
31
|
+
Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtry; } });
|
|
32
|
+
Object.defineProperty(exports, "xtryAsync", { enumerable: true, get: function () { return try_js_1.xtryAsync; } });
|
|
33
|
+
Object.defineProperty(exports, "xtryAsyncIterable", { enumerable: true, get: function () { return try_js_1.xtryAsyncIterable; } });
|
|
34
|
+
Object.defineProperty(exports, "xtrySync", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
|
|
35
|
+
Object.defineProperty(exports, "xtrySyncIterable", { enumerable: true, get: function () { return try_js_1.xtrySyncIterable; } });
|
package/lib/cjs/index.d.cts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export {
|
|
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 { toStringFailure } from './to-string-failure.cjs';
|
|
8
|
+
export { xtry, xtryAsync, xtryAsyncIterable, xtrySync, xtrySyncIterable } from './try.cjs';
|
package/lib/cjs/partial.cjs
CHANGED
|
@@ -1,53 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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");
|
|
4
11
|
function yok(value) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
return {
|
|
13
|
+
fails: false,
|
|
14
|
+
success: true,
|
|
15
|
+
value: value,
|
|
16
|
+
error: undefined,
|
|
17
|
+
};
|
|
11
18
|
}
|
|
12
19
|
function yerr(miscue) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
return {
|
|
21
|
+
fails: false,
|
|
22
|
+
success: false,
|
|
23
|
+
miscue,
|
|
24
|
+
value: undefined,
|
|
25
|
+
error: undefined,
|
|
26
|
+
};
|
|
20
27
|
}
|
|
21
28
|
function yres(result) {
|
|
22
|
-
|
|
23
|
-
|
|
29
|
+
if ((0, is_promise_like_js_1.isPromiseLike)(result)) {
|
|
30
|
+
return result.then(yresSync);
|
|
31
|
+
}
|
|
32
|
+
return yresSync(result);
|
|
24
33
|
}
|
|
25
34
|
function yresSync(result) {
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
if (result.fails) {
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
return yep(result);
|
|
28
39
|
}
|
|
29
40
|
async function yresAsync(promise) {
|
|
30
|
-
|
|
41
|
+
return promise.then(yresSync);
|
|
31
42
|
}
|
|
32
43
|
function yep(result) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
return {
|
|
45
|
+
...result,
|
|
46
|
+
success: true,
|
|
47
|
+
};
|
|
37
48
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
//#endregion
|
|
44
|
-
exports.YOK = YOK;
|
|
45
|
-
exports.YOK_FALSE = YOK_FALSE;
|
|
46
|
-
exports.YOK_NULL = YOK_NULL;
|
|
47
|
-
exports.YOK_TRUE = YOK_TRUE;
|
|
48
|
-
exports.yep = yep;
|
|
49
|
-
exports.yerr = yerr;
|
|
50
|
-
exports.yok = yok;
|
|
51
|
-
exports.yres = yres;
|
|
52
|
-
exports.yresAsync = yresAsync;
|
|
53
|
-
exports.yresSync = yresSync;
|
|
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));
|
package/lib/cjs/partial.d.cts
CHANGED
|
@@ -1,30 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { NotPromise } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type YSuccess<T> = Success<T> & {
|
|
7
|
-
success: true;
|
|
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;
|
|
8
6
|
};
|
|
9
|
-
type YFailure<M> = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
export type YFailure<M> = {
|
|
8
|
+
fails: false;
|
|
9
|
+
success: false;
|
|
10
|
+
miscue: M;
|
|
11
|
+
value: undefined;
|
|
12
|
+
error: undefined;
|
|
15
13
|
};
|
|
16
|
-
declare function yok(): YSuccess<void>;
|
|
17
|
-
declare function yok<T>(value: T): YSuccess<T>;
|
|
18
|
-
declare function yerr<M>(miscue: M): YFailure<M>;
|
|
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>;
|
|
19
17
|
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<
|
|
27
|
-
declare const YOK_TRUE: Readonly<YSuccess<boolean>>;
|
|
28
|
-
declare const YOK_FALSE: Readonly<YSuccess<boolean>>;
|
|
29
|
-
|
|
30
|
-
export { YFailure, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YResult, YSuccess, yep, yerr, yok, yres, yresAsync, yresSync };
|
|
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 {};
|
package/lib/cjs/result.cjs
CHANGED
|
@@ -1,28 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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;
|
|
3
6
|
function ok(value) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
return {
|
|
8
|
+
fails: false,
|
|
9
|
+
value: value,
|
|
10
|
+
error: undefined,
|
|
11
|
+
};
|
|
9
12
|
}
|
|
10
13
|
function err(error) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
return {
|
|
15
|
+
fails: true,
|
|
16
|
+
value: undefined,
|
|
17
|
+
error,
|
|
18
|
+
};
|
|
16
19
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
//#endregion
|
|
23
|
-
exports.OK = OK;
|
|
24
|
-
exports.OK_FALSE = OK_FALSE;
|
|
25
|
-
exports.OK_NULL = OK_NULL;
|
|
26
|
-
exports.OK_TRUE = OK_TRUE;
|
|
27
|
-
exports.err = err;
|
|
28
|
-
exports.ok = ok;
|
|
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));
|
package/lib/cjs/result.d.cts
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
error: undefined;
|
|
1
|
+
export type Success<T> = {
|
|
2
|
+
fails: false;
|
|
3
|
+
value: T;
|
|
4
|
+
error: undefined;
|
|
6
5
|
};
|
|
7
|
-
type Failure<E> = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
export type Failure<E> = {
|
|
7
|
+
fails: true;
|
|
8
|
+
value: undefined;
|
|
9
|
+
error: E;
|
|
11
10
|
};
|
|
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<
|
|
18
|
-
declare const OK_TRUE: Readonly<Success<boolean>>;
|
|
19
|
-
declare const OK_FALSE: Readonly<Success<boolean>>;
|
|
20
|
-
//#endregion
|
|
21
|
-
export { Failure, OK, OK_FALSE, OK_NULL, OK_TRUE, Result, Success, err, ok };
|
|
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,14 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stringifyError = stringifyError;
|
|
3
4
|
function stringifyError(error) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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);
|
|
11
21
|
}
|
|
12
|
-
|
|
13
|
-
//#endregion
|
|
14
|
-
exports.stringifyError = stringifyError;
|