@zokugun/xtry 0.7.2 → 0.9.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 +28 -6
- package/lib/cjs/async.cjs +13 -3
- package/lib/cjs/async.d.cts +5 -2
- package/lib/cjs/index.cjs +12 -2
- package/lib/cjs/index.d.cts +5 -2
- package/lib/cjs/json.cjs +6 -0
- package/lib/cjs/json.d.cts +2 -0
- package/lib/cjs/partial.cjs +3 -2
- package/lib/cjs/partial.d.cts +2 -1
- package/lib/cjs/result.cjs +3 -2
- package/lib/cjs/result.d.cts +2 -1
- package/lib/cjs/sync.cjs +13 -3
- package/lib/cjs/sync.d.cts +5 -2
- package/lib/cjs/tryify.cjs +27 -0
- package/lib/cjs/tryify.d.cts +45 -0
- package/lib/esm/async.d.mts +5 -2
- package/lib/esm/async.mjs +4 -2
- package/lib/esm/index.d.mts +5 -2
- package/lib/esm/index.mjs +4 -2
- package/lib/esm/json.d.mts +2 -0
- package/lib/esm/json.mjs +3 -0
- package/lib/esm/partial.d.mts +2 -1
- package/lib/esm/partial.mjs +2 -1
- package/lib/esm/result.d.mts +2 -1
- package/lib/esm/result.mjs +2 -1
- package/lib/esm/sync.d.mts +5 -2
- package/lib/esm/sync.mjs +4 -2
- package/lib/esm/tryify.d.mts +45 -0
- package/lib/esm/tryify.mjs +21 -0
- package/package.json +15 -1
package/README.md
CHANGED
|
@@ -136,12 +136,13 @@ function err<E>(error: E): Failure<E>;
|
|
|
136
136
|
|
|
137
137
|
To minimize allocations when returning the same `Success` shape, you can reuse the exported frozen helpers:
|
|
138
138
|
|
|
139
|
-
| Constant
|
|
140
|
-
|
|
|
141
|
-
| `OK`
|
|
142
|
-
| `OK_NULL`
|
|
143
|
-
| `
|
|
144
|
-
| `
|
|
139
|
+
| Constant | Wrapped value | Type | Typical usage |
|
|
140
|
+
| -------------- | --------------- | -------------------- | ------------------------------------------------------ |
|
|
141
|
+
| `OK` | `ok()` | `Success<void>` | Generic void success (e.g., cleanup, notifications) |
|
|
142
|
+
| `OK_NULL` | `ok(null)` | `Success<null>` | APIs that explicitly signal "nothing" with `null` |
|
|
143
|
+
| `OK_UNDEFINED` | `ok(undefined)` | `Success<undefined>` | APIs that explicitly signal "nothing" with `undefined` |
|
|
144
|
+
| `OK_TRUE` | `ok(true)` | `Success<true>` | Flag-style functions (`enable()` / `disable()`) |
|
|
145
|
+
| `OK_FALSE` | `ok(false)` | `Success<false>` | Guard checks that succeed with `false` |
|
|
145
146
|
|
|
146
147
|
### Try helpers
|
|
147
148
|
|
|
@@ -174,6 +175,27 @@ All helpers:
|
|
|
174
175
|
- execute the supplied function and capture thrown values;
|
|
175
176
|
- call the optional `handler` before turning that value into `err(error)`;
|
|
176
177
|
|
|
178
|
+
### xtryify helpers
|
|
179
|
+
|
|
180
|
+
`xtryify*` helpers turn any function into a reusable wrapper that always yields a `Result`, saving you from retyping `xtry(…)` every time you call it.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { xtryifyAsync, xtryifySync } from '@zokugun/xtry'
|
|
184
|
+
|
|
185
|
+
const fetchUserSafely = xtryifyAsync((id: string) => fetch(`/users/${id}`).then(r => r.json()));
|
|
186
|
+
const parseConfig = xtryifySync(() => JSON.parse(readFileSync('config.json', 'utf8')));
|
|
187
|
+
|
|
188
|
+
const userResult = await fetchUserSafely('42');
|
|
189
|
+
const configResult = parseConfig();
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Available variants mirror the regular helpers:
|
|
193
|
+
|
|
194
|
+
- `xtryifySync(fn)` and `xtryifyAsync(fn)` return new functions that forward arguments to `fn` and capture thrown/errors as `Result` objects.
|
|
195
|
+
- `xtryifySyncIterable(fn)` and `xtryifyAsyncIterable(fn)` wrap functions that return iterables/async iterables, yielding streams of `Result` values.
|
|
196
|
+
|
|
197
|
+
Because the returned function already encapsulates the try/catch logic, you can share it across modules (e.g., inject into DI containers or export once for common utilities) while keeping strong `Result` typing for every call site.
|
|
198
|
+
|
|
177
199
|
### Partial helpers
|
|
178
200
|
|
|
179
201
|
```typescript
|
package/lib/cjs/async.cjs
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.xtrySyncIterable = exports.xtrySync = exports.xtryIterable = exports.xtry = exports.xtryUnknown = exports.toStringFailure = exports.stringifyError = exports.
|
|
3
|
+
exports.xtryifySyncIterable = exports.xtryifySync = exports.xtryifyIterable = exports.xtryify = exports.xtrySyncIterable = exports.xtrySync = exports.xtryIterable = exports.xtry = exports.xtryUnknown = exports.toStringFailure = exports.stringifyError = exports.OK_UNDEFINED = exports.OK_TRUE = exports.OK_NULL = exports.OK_FALSE = exports.OK = exports.err = exports.ok = exports.YOK_UNDEFINED = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK_FALSE = exports.YOK = exports.yep = exports.yresSync = exports.yres = exports.yresUnknown = exports.yerr = exports.yok = exports.parseJson = exports.parseJSON = exports.xdeferSync = exports.xdefer = exports.xdeferUnknown = void 0;
|
|
4
4
|
var defer_js_1 = require("./defer.cjs");
|
|
5
5
|
Object.defineProperty(exports, "xdeferUnknown", { enumerable: true, get: function () { return defer_js_1.xdefer; } });
|
|
6
6
|
Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdeferAsync; } });
|
|
7
7
|
Object.defineProperty(exports, "xdeferSync", { enumerable: true, get: function () { return defer_js_1.xdeferSync; } });
|
|
8
|
+
var json_js_1 = require("./json.cjs");
|
|
9
|
+
Object.defineProperty(exports, "parseJSON", { enumerable: true, get: function () { return json_js_1.parseJSON; } });
|
|
10
|
+
Object.defineProperty(exports, "parseJson", { enumerable: true, get: function () { return json_js_1.parseJson; } });
|
|
8
11
|
var partial_js_1 = require("./partial.cjs");
|
|
9
12
|
Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
|
|
10
13
|
Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
|
|
@@ -13,16 +16,18 @@ Object.defineProperty(exports, "yres", { enumerable: true, get: function () { re
|
|
|
13
16
|
Object.defineProperty(exports, "yresSync", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
|
|
14
17
|
Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
|
|
15
18
|
Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
|
|
19
|
+
Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
|
|
16
20
|
Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
|
|
17
21
|
Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
|
|
18
|
-
Object.defineProperty(exports, "
|
|
22
|
+
Object.defineProperty(exports, "YOK_UNDEFINED", { enumerable: true, get: function () { return partial_js_1.YOK_UNDEFINED; } });
|
|
19
23
|
var result_js_1 = require("./result.cjs");
|
|
20
24
|
Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
|
|
21
25
|
Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
|
|
22
26
|
Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
|
|
27
|
+
Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
|
|
23
28
|
Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
|
|
24
29
|
Object.defineProperty(exports, "OK_TRUE", { enumerable: true, get: function () { return result_js_1.OK_TRUE; } });
|
|
25
|
-
Object.defineProperty(exports, "
|
|
30
|
+
Object.defineProperty(exports, "OK_UNDEFINED", { enumerable: true, get: function () { return result_js_1.OK_UNDEFINED; } });
|
|
26
31
|
var stringify_error_js_1 = require("./stringify-error.cjs");
|
|
27
32
|
Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
|
|
28
33
|
var to_string_failure_js_1 = require("./to-string-failure.cjs");
|
|
@@ -33,3 +38,8 @@ Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { re
|
|
|
33
38
|
Object.defineProperty(exports, "xtryIterable", { enumerable: true, get: function () { return try_js_1.xtryAsyncIterable; } });
|
|
34
39
|
Object.defineProperty(exports, "xtrySync", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
|
|
35
40
|
Object.defineProperty(exports, "xtrySyncIterable", { enumerable: true, get: function () { return try_js_1.xtrySyncIterable; } });
|
|
41
|
+
var tryify_js_1 = require("./tryify.cjs");
|
|
42
|
+
Object.defineProperty(exports, "xtryify", { enumerable: true, get: function () { return tryify_js_1.xtryifyAsync; } });
|
|
43
|
+
Object.defineProperty(exports, "xtryifyIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifyAsyncIterable; } });
|
|
44
|
+
Object.defineProperty(exports, "xtryifySync", { enumerable: true, get: function () { return tryify_js_1.xtryifySync; } });
|
|
45
|
+
Object.defineProperty(exports, "xtryifySyncIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifySyncIterable; } });
|
package/lib/cjs/async.d.cts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export type { XDeferAsync, XDeferSync } from './defer.cjs';
|
|
2
2
|
export { xdefer as xdeferUnknown, xdeferAsync as xdefer, xdeferSync } from './defer.cjs';
|
|
3
|
+
export { parseJSON, parseJson } from './json.cjs';
|
|
3
4
|
export type { YResult, YSuccess, YFailure } from './partial.cjs';
|
|
4
|
-
export { yok, yerr, yres as yresUnknown, yresAsync as yres, yresSync, yep, YOK, YOK_NULL, YOK_TRUE,
|
|
5
|
+
export { yok, yerr, yres as yresUnknown, yresAsync as yres, yresSync, yep, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YOK_UNDEFINED } from './partial.cjs';
|
|
5
6
|
export type { Success, Failure, Result } from './result.cjs';
|
|
6
|
-
export { ok, err, OK, OK_NULL, OK_TRUE,
|
|
7
|
+
export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.cjs';
|
|
7
8
|
export { stringifyError } from './stringify-error.cjs';
|
|
8
9
|
export { toStringFailure } from './to-string-failure.cjs';
|
|
9
10
|
export { xtry as xtryUnknown, xtryAsync as xtry, xtryAsyncIterable as xtryIterable, xtrySync, xtrySyncIterable } from './try.cjs';
|
|
11
|
+
export type { AsyncIteratableResult, AsyncIteratorElement, AsyncResult, SyncIteratableResult, SyncIteratorElement, SyncResult } from './tryify.cjs';
|
|
12
|
+
export { xtryifyAsync as xtryify, xtryifyAsyncIterable as xtryifyIterable, xtryifySync, xtryifySyncIterable } from './tryify.cjs';
|
package/lib/cjs/index.cjs
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.xtrySyncIterable = exports.xtrySync = exports.xtryAsyncIterable = exports.xtryAsync = exports.xtry = exports.toStringFailure = exports.stringifyError = exports.
|
|
3
|
+
exports.xtryifySyncIterable = exports.xtryifySync = exports.xtryifyAsyncIterable = exports.xtryifyAsync = exports.xtrySyncIterable = exports.xtrySync = exports.xtryAsyncIterable = exports.xtryAsync = exports.xtry = exports.toStringFailure = exports.stringifyError = exports.OK_UNDEFINED = exports.OK_TRUE = exports.OK_NULL = exports.OK_FALSE = exports.OK = exports.err = exports.ok = exports.YOK_UNDEFINED = exports.YOK_FALSE = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK = exports.yep = exports.yresSync = exports.yresAsync = exports.yres = exports.yerr = exports.yok = exports.parseJson = exports.parseJSON = exports.xdeferSync = exports.xdeferAsync = exports.xdefer = void 0;
|
|
4
4
|
var defer_js_1 = require("./defer.cjs");
|
|
5
5
|
Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdefer; } });
|
|
6
6
|
Object.defineProperty(exports, "xdeferAsync", { enumerable: true, get: function () { return defer_js_1.xdeferAsync; } });
|
|
7
7
|
Object.defineProperty(exports, "xdeferSync", { enumerable: true, get: function () { return defer_js_1.xdeferSync; } });
|
|
8
|
+
var json_js_1 = require("./json.cjs");
|
|
9
|
+
Object.defineProperty(exports, "parseJSON", { enumerable: true, get: function () { return json_js_1.parseJSON; } });
|
|
10
|
+
Object.defineProperty(exports, "parseJson", { enumerable: true, get: function () { return json_js_1.parseJson; } });
|
|
8
11
|
var partial_js_1 = require("./partial.cjs");
|
|
9
12
|
Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
|
|
10
13
|
Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
|
|
@@ -16,13 +19,15 @@ Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { ret
|
|
|
16
19
|
Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
|
|
17
20
|
Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
|
|
18
21
|
Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
|
|
22
|
+
Object.defineProperty(exports, "YOK_UNDEFINED", { enumerable: true, get: function () { return partial_js_1.YOK_UNDEFINED; } });
|
|
19
23
|
var result_js_1 = require("./result.cjs");
|
|
20
24
|
Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
|
|
21
25
|
Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
|
|
22
26
|
Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
|
|
27
|
+
Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
|
|
23
28
|
Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
|
|
24
29
|
Object.defineProperty(exports, "OK_TRUE", { enumerable: true, get: function () { return result_js_1.OK_TRUE; } });
|
|
25
|
-
Object.defineProperty(exports, "
|
|
30
|
+
Object.defineProperty(exports, "OK_UNDEFINED", { enumerable: true, get: function () { return result_js_1.OK_UNDEFINED; } });
|
|
26
31
|
var stringify_error_js_1 = require("./stringify-error.cjs");
|
|
27
32
|
Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
|
|
28
33
|
var to_string_failure_js_1 = require("./to-string-failure.cjs");
|
|
@@ -33,3 +38,8 @@ Object.defineProperty(exports, "xtryAsync", { enumerable: true, get: function ()
|
|
|
33
38
|
Object.defineProperty(exports, "xtryAsyncIterable", { enumerable: true, get: function () { return try_js_1.xtryAsyncIterable; } });
|
|
34
39
|
Object.defineProperty(exports, "xtrySync", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
|
|
35
40
|
Object.defineProperty(exports, "xtrySyncIterable", { enumerable: true, get: function () { return try_js_1.xtrySyncIterable; } });
|
|
41
|
+
var tryify_js_1 = require("./tryify.cjs");
|
|
42
|
+
Object.defineProperty(exports, "xtryifyAsync", { enumerable: true, get: function () { return tryify_js_1.xtryifyAsync; } });
|
|
43
|
+
Object.defineProperty(exports, "xtryifyAsyncIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifyAsyncIterable; } });
|
|
44
|
+
Object.defineProperty(exports, "xtryifySync", { enumerable: true, get: function () { return tryify_js_1.xtryifySync; } });
|
|
45
|
+
Object.defineProperty(exports, "xtryifySyncIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifySyncIterable; } });
|
package/lib/cjs/index.d.cts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export type { XDeferAsync, XDeferSync } from './defer.cjs';
|
|
2
2
|
export { xdefer, xdeferAsync, xdeferSync } from './defer.cjs';
|
|
3
|
+
export { parseJSON, parseJson } from './json.cjs';
|
|
3
4
|
export type { YResult, YSuccess, YFailure } from './partial.cjs';
|
|
4
|
-
export { yok, yerr, yres, yresAsync, yresSync, yep, YOK, YOK_NULL, YOK_TRUE, YOK_FALSE } from './partial.cjs';
|
|
5
|
+
export { yok, yerr, yres, yresAsync, yresSync, yep, YOK, YOK_NULL, YOK_TRUE, YOK_FALSE, YOK_UNDEFINED } from './partial.cjs';
|
|
5
6
|
export type { Success, Failure, Result } from './result.cjs';
|
|
6
|
-
export { ok, err, OK, OK_NULL, OK_TRUE,
|
|
7
|
+
export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.cjs';
|
|
7
8
|
export { stringifyError } from './stringify-error.cjs';
|
|
8
9
|
export { toStringFailure } from './to-string-failure.cjs';
|
|
9
10
|
export { xtry, xtryAsync, xtryAsyncIterable, xtrySync, xtrySyncIterable } from './try.cjs';
|
|
11
|
+
export type { AsyncIteratableResult, AsyncIteratorElement, AsyncResult, SyncIteratableResult, SyncIteratorElement, SyncResult } from './tryify.cjs';
|
|
12
|
+
export { xtryifyAsync, xtryifyAsyncIterable, xtryifySync, xtryifySyncIterable } from './tryify.cjs';
|
package/lib/cjs/json.cjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseJSON = exports.parseJson = void 0;
|
|
4
|
+
const tryify_js_1 = require("./tryify.cjs");
|
|
5
|
+
exports.parseJson = (0, tryify_js_1.xtryifySync)(JSON.parse);
|
|
6
|
+
exports.parseJSON = exports.parseJson;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const parseJson: ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>);
|
|
2
|
+
export declare const parseJSON: ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>);
|
package/lib/cjs/partial.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.YOK_UNDEFINED = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK_FALSE = exports.YOK = void 0;
|
|
4
4
|
exports.yok = yok;
|
|
5
5
|
exports.yerr = yerr;
|
|
6
6
|
exports.yres = yres;
|
|
@@ -47,6 +47,7 @@ function yep(result) {
|
|
|
47
47
|
};
|
|
48
48
|
}
|
|
49
49
|
exports.YOK = Object.freeze(yok());
|
|
50
|
+
exports.YOK_FALSE = Object.freeze(yok(false));
|
|
50
51
|
exports.YOK_NULL = Object.freeze(yok(null));
|
|
51
52
|
exports.YOK_TRUE = Object.freeze(yok(true));
|
|
52
|
-
exports.
|
|
53
|
+
exports.YOK_UNDEFINED = Object.freeze(yok(undefined));
|
package/lib/cjs/partial.d.cts
CHANGED
|
@@ -21,7 +21,8 @@ export declare function yresSync<T, E>(result: NotPromise<Result<T, E>>): YRResu
|
|
|
21
21
|
export declare function yresAsync<T, E>(promise: Promise<Result<T, E>>): Promise<YRResult<T, E>>;
|
|
22
22
|
export declare function yep<T>(result: Success<T>): YSuccess<T>;
|
|
23
23
|
export declare const YOK: Readonly<YSuccess<void>>;
|
|
24
|
+
export declare const YOK_FALSE: Readonly<YSuccess<boolean>>;
|
|
24
25
|
export declare const YOK_NULL: Readonly<YSuccess<null>>;
|
|
25
26
|
export declare const YOK_TRUE: Readonly<YSuccess<boolean>>;
|
|
26
|
-
export declare const
|
|
27
|
+
export declare const YOK_UNDEFINED: Readonly<YSuccess<undefined>>;
|
|
27
28
|
export {};
|
package/lib/cjs/result.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.OK_UNDEFINED = exports.OK_TRUE = exports.OK_NULL = exports.OK_FALSE = exports.OK = void 0;
|
|
4
4
|
exports.ok = ok;
|
|
5
5
|
exports.err = err;
|
|
6
6
|
function ok(value) {
|
|
@@ -18,6 +18,7 @@ function err(error) {
|
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
exports.OK = Object.freeze(ok());
|
|
21
|
+
exports.OK_FALSE = Object.freeze(ok(false));
|
|
21
22
|
exports.OK_NULL = Object.freeze(ok(null));
|
|
22
23
|
exports.OK_TRUE = Object.freeze(ok(true));
|
|
23
|
-
exports.
|
|
24
|
+
exports.OK_UNDEFINED = Object.freeze(ok(undefined));
|
package/lib/cjs/result.d.cts
CHANGED
|
@@ -13,6 +13,7 @@ export declare function ok(): Success<void>;
|
|
|
13
13
|
export declare function ok<T>(value: T): Success<T>;
|
|
14
14
|
export declare function err<E>(error: E): Failure<E>;
|
|
15
15
|
export declare const OK: Readonly<Success<void>>;
|
|
16
|
+
export declare const OK_FALSE: Readonly<Success<boolean>>;
|
|
16
17
|
export declare const OK_NULL: Readonly<Success<null>>;
|
|
17
18
|
export declare const OK_TRUE: Readonly<Success<boolean>>;
|
|
18
|
-
export declare const
|
|
19
|
+
export declare const OK_UNDEFINED: Readonly<Success<undefined>>;
|
package/lib/cjs/sync.cjs
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.xtryIterable = exports.xtry = exports.xtryAsyncIterable = exports.xtryAsync = exports.xtryUnknown = exports.toStringFailure = exports.stringifyError = exports.
|
|
3
|
+
exports.xtryifyIterable = exports.xtryify = exports.xtryifyAsyncIterable = exports.xtryifyAsync = exports.xtryIterable = exports.xtry = exports.xtryAsyncIterable = exports.xtryAsync = exports.xtryUnknown = exports.toStringFailure = exports.stringifyError = exports.OK_UNDEFINED = exports.OK_TRUE = exports.OK_NULL = exports.OK_FALSE = exports.OK = exports.err = exports.ok = exports.YOK_UNDEFINED = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK_FALSE = exports.YOK = exports.yep = exports.yres = exports.yresAsync = exports.yresUnknown = exports.yerr = exports.yok = exports.parseJson = exports.parseJSON = exports.xdefer = exports.xdeferAsync = exports.xdeferUnknown = void 0;
|
|
4
4
|
var defer_js_1 = require("./defer.cjs");
|
|
5
5
|
Object.defineProperty(exports, "xdeferUnknown", { enumerable: true, get: function () { return defer_js_1.xdefer; } });
|
|
6
6
|
Object.defineProperty(exports, "xdeferAsync", { enumerable: true, get: function () { return defer_js_1.xdeferAsync; } });
|
|
7
7
|
Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdeferSync; } });
|
|
8
|
+
var json_js_1 = require("./json.cjs");
|
|
9
|
+
Object.defineProperty(exports, "parseJSON", { enumerable: true, get: function () { return json_js_1.parseJSON; } });
|
|
10
|
+
Object.defineProperty(exports, "parseJson", { enumerable: true, get: function () { return json_js_1.parseJson; } });
|
|
8
11
|
var partial_js_1 = require("./partial.cjs");
|
|
9
12
|
Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
|
|
10
13
|
Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
|
|
@@ -13,16 +16,18 @@ Object.defineProperty(exports, "yresAsync", { enumerable: true, get: function ()
|
|
|
13
16
|
Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
|
|
14
17
|
Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
|
|
15
18
|
Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
|
|
19
|
+
Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
|
|
16
20
|
Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
|
|
17
21
|
Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
|
|
18
|
-
Object.defineProperty(exports, "
|
|
22
|
+
Object.defineProperty(exports, "YOK_UNDEFINED", { enumerable: true, get: function () { return partial_js_1.YOK_UNDEFINED; } });
|
|
19
23
|
var result_js_1 = require("./result.cjs");
|
|
20
24
|
Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
|
|
21
25
|
Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
|
|
22
26
|
Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
|
|
27
|
+
Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
|
|
23
28
|
Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
|
|
24
29
|
Object.defineProperty(exports, "OK_TRUE", { enumerable: true, get: function () { return result_js_1.OK_TRUE; } });
|
|
25
|
-
Object.defineProperty(exports, "
|
|
30
|
+
Object.defineProperty(exports, "OK_UNDEFINED", { enumerable: true, get: function () { return result_js_1.OK_UNDEFINED; } });
|
|
26
31
|
var stringify_error_js_1 = require("./stringify-error.cjs");
|
|
27
32
|
Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
|
|
28
33
|
var to_string_failure_js_1 = require("./to-string-failure.cjs");
|
|
@@ -33,3 +38,8 @@ Object.defineProperty(exports, "xtryAsync", { enumerable: true, get: function ()
|
|
|
33
38
|
Object.defineProperty(exports, "xtryAsyncIterable", { enumerable: true, get: function () { return try_js_1.xtryAsyncIterable; } });
|
|
34
39
|
Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
|
|
35
40
|
Object.defineProperty(exports, "xtryIterable", { enumerable: true, get: function () { return try_js_1.xtrySyncIterable; } });
|
|
41
|
+
var tryify_js_1 = require("./tryify.cjs");
|
|
42
|
+
Object.defineProperty(exports, "xtryifyAsync", { enumerable: true, get: function () { return tryify_js_1.xtryifyAsync; } });
|
|
43
|
+
Object.defineProperty(exports, "xtryifyAsyncIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifyAsyncIterable; } });
|
|
44
|
+
Object.defineProperty(exports, "xtryify", { enumerable: true, get: function () { return tryify_js_1.xtryifySync; } });
|
|
45
|
+
Object.defineProperty(exports, "xtryifyIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifySyncIterable; } });
|
package/lib/cjs/sync.d.cts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export type { XDeferAsync, XDeferSync } from './defer.cjs';
|
|
2
2
|
export { xdefer as xdeferUnknown, xdeferAsync, xdeferSync as xdefer } from './defer.cjs';
|
|
3
|
+
export { parseJSON, parseJson } from './json.cjs';
|
|
3
4
|
export type { YResult, YSuccess, YFailure } from './partial.cjs';
|
|
4
|
-
export { yok, yerr, yres as yresUnknown, yresAsync, yresSync as yres, yep, YOK, YOK_NULL, YOK_TRUE,
|
|
5
|
+
export { yok, yerr, yres as yresUnknown, yresAsync, yresSync as yres, yep, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YOK_UNDEFINED } from './partial.cjs';
|
|
5
6
|
export type { Success, Failure, Result } from './result.cjs';
|
|
6
|
-
export { ok, err, OK, OK_NULL, OK_TRUE,
|
|
7
|
+
export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.cjs';
|
|
7
8
|
export { stringifyError } from './stringify-error.cjs';
|
|
8
9
|
export { toStringFailure } from './to-string-failure.cjs';
|
|
9
10
|
export { xtry as xtryUnknown, xtryAsync, xtryAsyncIterable, xtrySync as xtry, xtrySyncIterable as xtryIterable } from './try.cjs';
|
|
11
|
+
export type { AsyncIteratableResult, AsyncIteratorElement, AsyncResult, SyncIteratableResult, SyncIteratorElement, SyncResult } from './tryify.cjs';
|
|
12
|
+
export { xtryifyAsync, xtryifyAsyncIterable, xtryifySync as xtryify, xtryifySyncIterable as xtryifyIterable } from './tryify.cjs';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xtryifyAsync = xtryifyAsync;
|
|
4
|
+
exports.xtryifyAsyncIterable = xtryifyAsyncIterable;
|
|
5
|
+
exports.xtryifySync = xtryifySync;
|
|
6
|
+
exports.xtryifySyncIterable = xtryifySyncIterable;
|
|
7
|
+
const try_js_1 = require("./try.cjs");
|
|
8
|
+
function xtryifyAsync(fn) {
|
|
9
|
+
return async function (...args) {
|
|
10
|
+
return (0, try_js_1.xtryAsync)(async () => fn(...args));
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function xtryifyAsyncIterable(fn) {
|
|
14
|
+
return function (...args) {
|
|
15
|
+
return (0, try_js_1.xtryAsyncIterable)(fn(...args));
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function xtryifySync(fn) {
|
|
19
|
+
return function (...args) {
|
|
20
|
+
return (0, try_js_1.xtrySync)(() => fn(...args));
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function xtryifySyncIterable(fn) {
|
|
24
|
+
return function (...args) {
|
|
25
|
+
return (0, try_js_1.xtrySyncIterable)(fn(...args));
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type Result } from './result.cjs';
|
|
2
|
+
import { type NotPromise } from './utils/types.cjs';
|
|
3
|
+
type AnyAsyncFunction = (...args: any[]) => Promise<unknown>;
|
|
4
|
+
type AnySyncFunction = (...args: any[]) => unknown;
|
|
5
|
+
type LastIsFunction<T extends readonly any[]> = T extends [...infer Rest, infer Last] ? Last extends (...args: any[]) => any ? true : false : false;
|
|
6
|
+
type OverloadsUnion<Fn extends Function> = Fn extends {
|
|
7
|
+
(...args: infer A1): infer R1;
|
|
8
|
+
(...args: infer A2): infer R2;
|
|
9
|
+
(...args: infer A3): infer R3;
|
|
10
|
+
(...args: infer A4): infer R4;
|
|
11
|
+
(...args: infer A5): infer R5;
|
|
12
|
+
(...args: infer A6): infer R6;
|
|
13
|
+
(...args: infer A7): infer R7;
|
|
14
|
+
(...args: infer A8): infer R8;
|
|
15
|
+
(...args: infer A9): infer R9;
|
|
16
|
+
(...args: infer A10): infer R10;
|
|
17
|
+
} ? ((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3) | ((...args: A4) => R4) | ((...args: A5) => R5) | ((...args: A6) => R6) | ((...args: A7) => R7) | ((...args: A8) => R8) | ((...args: A9) => R9) | ((...args: A10) => R10) : Fn;
|
|
18
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
19
|
+
type WrapAsyncOverload<Fn extends AnyAsyncFunction, Err extends Error> = Fn extends (...args: infer Args) => unknown ? (...args: Args) => AsyncResult<Fn, Err> : never;
|
|
20
|
+
type WrapAsyncIterableOverload<Fn extends AsyncIterableFunction, Err extends Error> = Fn extends (...args: infer Args) => AsyncIterable<unknown> ? (...args: Args) => AsyncIteratableResult<Fn, Err> : never;
|
|
21
|
+
type WrapSyncOverload<Fn extends AnySyncFunction, Err extends Error> = Fn extends (...args: infer Args) => unknown ? (...args: Args) => SyncResult<Fn, Err> : never;
|
|
22
|
+
type WrapSyncIterableOverload<Fn extends SyncIterableFunction, Err extends Error> = Fn extends (...args: infer Args) => Iterable<unknown> ? (...args: Args) => SyncIteratableResult<Fn, Err> : never;
|
|
23
|
+
export type AsyncFunction<Fn extends (...args: any[]) => any> = ReturnType<Fn> extends Promise<any> ? Fn : never;
|
|
24
|
+
export type AsyncIterableFunction = (...args: any[]) => AsyncIterable<unknown>;
|
|
25
|
+
export type AsyncIteratorElement<T> = T extends Promise<infer P> ? AsyncIteratorElement<P> : T extends AsyncIterator<infer E> ? E : T extends AsyncIterable<infer E> ? E : T extends {
|
|
26
|
+
[Symbol.asyncIterator]: (...args: unknown[]) => infer I;
|
|
27
|
+
} ? AsyncIteratorElement<I> : unknown;
|
|
28
|
+
export type AsyncResult<T extends (...args: unknown[]) => unknown, Err extends Error> = Promise<Result<Awaited<ReturnType<T>>, Err>>;
|
|
29
|
+
export type AsyncIteratableResult<T extends (...args: unknown[]) => unknown, Err extends Error> = AsyncIterable<Result<AsyncIteratorElement<ReturnType<T>>, Err>, unknown, unknown>;
|
|
30
|
+
export type PreserveAsyncOverloads<Fn extends AnyAsyncFunction, Err extends Error> = UnionToIntersection<WrapAsyncOverload<OverloadsUnion<Fn>, Err>>;
|
|
31
|
+
export type PreserveAsyncIterableOverloads<Fn extends AsyncIterableFunction, Err extends Error> = UnionToIntersection<WrapAsyncIterableOverload<OverloadsUnion<Fn>, Err>>;
|
|
32
|
+
export type PreserveSyncOverloads<Fn extends AnySyncFunction, Err extends Error> = UnionToIntersection<WrapSyncOverload<OverloadsUnion<Fn>, Err>>;
|
|
33
|
+
export type PreserveSyncIterableOverloads<Fn extends SyncIterableFunction, Err extends Error> = UnionToIntersection<WrapSyncIterableOverload<OverloadsUnion<Fn>, Err>>;
|
|
34
|
+
export type SyncFunction<Fn extends (...args: any[]) => any> = LastIsFunction<Parameters<Fn>> extends true ? never : ReturnType<Fn> extends Promise<any> ? never : Fn;
|
|
35
|
+
export type SyncIterableFunction = (...args: any[]) => Iterable<unknown>;
|
|
36
|
+
export type SyncIteratorElement<T> = T extends Iterator<infer E> ? E : T extends Iterable<infer E> ? E : T extends {
|
|
37
|
+
[Symbol.iterator]: (...args: unknown[]) => infer I;
|
|
38
|
+
} ? SyncIteratorElement<I> : unknown;
|
|
39
|
+
export type SyncResult<T extends (...args: unknown[]) => NotPromise<unknown>, Err extends Error> = Result<ReturnType<T>, Err>;
|
|
40
|
+
export type SyncIteratableResult<T extends (...args: unknown[]) => unknown, Err extends Error> = Iterable<Result<SyncIteratorElement<ReturnType<T>>, Err>, unknown, unknown>;
|
|
41
|
+
export declare function xtryifyAsync<Fn extends AnyAsyncFunction, Err extends Error>(fn: AsyncFunction<Fn>): PreserveAsyncOverloads<Fn, Err>;
|
|
42
|
+
export declare function xtryifyAsyncIterable<Fn extends AsyncIterableFunction, Err extends Error>(fn: Fn): PreserveAsyncIterableOverloads<Fn, Err>;
|
|
43
|
+
export declare function xtryifySync<Fn extends AnySyncFunction, Err extends Error>(fn: SyncFunction<Fn>): PreserveSyncOverloads<Fn, Err>;
|
|
44
|
+
export declare function xtryifySyncIterable<Fn extends SyncIterableFunction, Err extends Error>(fn: Fn): PreserveSyncIterableOverloads<Fn, Err>;
|
|
45
|
+
export {};
|
package/lib/esm/async.d.mts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export type { XDeferAsync, XDeferSync } from './defer.mjs';
|
|
2
2
|
export { xdefer as xdeferUnknown, xdeferAsync as xdefer, xdeferSync } from './defer.mjs';
|
|
3
|
+
export { parseJSON, parseJson } from './json.mjs';
|
|
3
4
|
export type { YResult, YSuccess, YFailure } from './partial.mjs';
|
|
4
|
-
export { yok, yerr, yres as yresUnknown, yresAsync as yres, yresSync, yep, YOK, YOK_NULL, YOK_TRUE,
|
|
5
|
+
export { yok, yerr, yres as yresUnknown, yresAsync as yres, yresSync, yep, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YOK_UNDEFINED } from './partial.mjs';
|
|
5
6
|
export type { Success, Failure, Result } from './result.mjs';
|
|
6
|
-
export { ok, err, OK, OK_NULL, OK_TRUE,
|
|
7
|
+
export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.mjs';
|
|
7
8
|
export { stringifyError } from './stringify-error.mjs';
|
|
8
9
|
export { toStringFailure } from './to-string-failure.mjs';
|
|
9
10
|
export { xtry as xtryUnknown, xtryAsync as xtry, xtryAsyncIterable as xtryIterable, xtrySync, xtrySyncIterable } from './try.mjs';
|
|
11
|
+
export type { AsyncIteratableResult, AsyncIteratorElement, AsyncResult, SyncIteratableResult, SyncIteratorElement, SyncResult } from './tryify.mjs';
|
|
12
|
+
export { xtryifyAsync as xtryify, xtryifyAsyncIterable as xtryifyIterable, xtryifySync, xtryifySyncIterable } from './tryify.mjs';
|
package/lib/esm/async.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { xdefer as xdeferUnknown, xdeferAsync as xdefer, xdeferSync } from './defer.mjs';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
2
|
+
export { parseJSON, parseJson } from './json.mjs';
|
|
3
|
+
export { yok, yerr, yres as yresUnknown, yresAsync as yres, yresSync, yep, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YOK_UNDEFINED } from './partial.mjs';
|
|
4
|
+
export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.mjs';
|
|
4
5
|
export { stringifyError } from './stringify-error.mjs';
|
|
5
6
|
export { toStringFailure } from './to-string-failure.mjs';
|
|
6
7
|
export { xtry as xtryUnknown, xtryAsync as xtry, xtryAsyncIterable as xtryIterable, xtrySync, xtrySyncIterable } from './try.mjs';
|
|
8
|
+
export { xtryifyAsync as xtryify, xtryifyAsyncIterable as xtryifyIterable, xtryifySync, xtryifySyncIterable } from './tryify.mjs';
|
package/lib/esm/index.d.mts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export type { XDeferAsync, XDeferSync } from './defer.mjs';
|
|
2
2
|
export { xdefer, xdeferAsync, xdeferSync } from './defer.mjs';
|
|
3
|
+
export { parseJSON, parseJson } from './json.mjs';
|
|
3
4
|
export type { YResult, YSuccess, YFailure } from './partial.mjs';
|
|
4
|
-
export { yok, yerr, yres, yresAsync, yresSync, yep, YOK, YOK_NULL, YOK_TRUE, YOK_FALSE } from './partial.mjs';
|
|
5
|
+
export { yok, yerr, yres, yresAsync, yresSync, yep, YOK, YOK_NULL, YOK_TRUE, YOK_FALSE, YOK_UNDEFINED } from './partial.mjs';
|
|
5
6
|
export type { Success, Failure, Result } from './result.mjs';
|
|
6
|
-
export { ok, err, OK, OK_NULL, OK_TRUE,
|
|
7
|
+
export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.mjs';
|
|
7
8
|
export { stringifyError } from './stringify-error.mjs';
|
|
8
9
|
export { toStringFailure } from './to-string-failure.mjs';
|
|
9
10
|
export { xtry, xtryAsync, xtryAsyncIterable, xtrySync, xtrySyncIterable } from './try.mjs';
|
|
11
|
+
export type { AsyncIteratableResult, AsyncIteratorElement, AsyncResult, SyncIteratableResult, SyncIteratorElement, SyncResult } from './tryify.mjs';
|
|
12
|
+
export { xtryifyAsync, xtryifyAsyncIterable, xtryifySync, xtryifySyncIterable } from './tryify.mjs';
|
package/lib/esm/index.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { xdefer, xdeferAsync, xdeferSync } from './defer.mjs';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
2
|
+
export { parseJSON, parseJson } from './json.mjs';
|
|
3
|
+
export { yok, yerr, yres, yresAsync, yresSync, yep, YOK, YOK_NULL, YOK_TRUE, YOK_FALSE, YOK_UNDEFINED } from './partial.mjs';
|
|
4
|
+
export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.mjs';
|
|
4
5
|
export { stringifyError } from './stringify-error.mjs';
|
|
5
6
|
export { toStringFailure } from './to-string-failure.mjs';
|
|
6
7
|
export { xtry, xtryAsync, xtryAsyncIterable, xtrySync, xtrySyncIterable } from './try.mjs';
|
|
8
|
+
export { xtryifyAsync, xtryifyAsyncIterable, xtryifySync, xtryifySyncIterable } from './tryify.mjs';
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const parseJson: ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>);
|
|
2
|
+
export declare const parseJSON: ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>) & ((text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => import("./tryify.js").SyncResult<(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any, Error>);
|
package/lib/esm/json.mjs
ADDED
package/lib/esm/partial.d.mts
CHANGED
|
@@ -21,7 +21,8 @@ export declare function yresSync<T, E>(result: NotPromise<Result<T, E>>): YRResu
|
|
|
21
21
|
export declare function yresAsync<T, E>(promise: Promise<Result<T, E>>): Promise<YRResult<T, E>>;
|
|
22
22
|
export declare function yep<T>(result: Success<T>): YSuccess<T>;
|
|
23
23
|
export declare const YOK: Readonly<YSuccess<void>>;
|
|
24
|
+
export declare const YOK_FALSE: Readonly<YSuccess<boolean>>;
|
|
24
25
|
export declare const YOK_NULL: Readonly<YSuccess<null>>;
|
|
25
26
|
export declare const YOK_TRUE: Readonly<YSuccess<boolean>>;
|
|
26
|
-
export declare const
|
|
27
|
+
export declare const YOK_UNDEFINED: Readonly<YSuccess<undefined>>;
|
|
27
28
|
export {};
|
package/lib/esm/partial.mjs
CHANGED
|
@@ -38,6 +38,7 @@ export function yep(result) {
|
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
export const YOK = Object.freeze(yok());
|
|
41
|
+
export const YOK_FALSE = Object.freeze(yok(false));
|
|
41
42
|
export const YOK_NULL = Object.freeze(yok(null));
|
|
42
43
|
export const YOK_TRUE = Object.freeze(yok(true));
|
|
43
|
-
export const
|
|
44
|
+
export const YOK_UNDEFINED = Object.freeze(yok(undefined));
|
package/lib/esm/result.d.mts
CHANGED
|
@@ -13,6 +13,7 @@ export declare function ok(): Success<void>;
|
|
|
13
13
|
export declare function ok<T>(value: T): Success<T>;
|
|
14
14
|
export declare function err<E>(error: E): Failure<E>;
|
|
15
15
|
export declare const OK: Readonly<Success<void>>;
|
|
16
|
+
export declare const OK_FALSE: Readonly<Success<boolean>>;
|
|
16
17
|
export declare const OK_NULL: Readonly<Success<null>>;
|
|
17
18
|
export declare const OK_TRUE: Readonly<Success<boolean>>;
|
|
18
|
-
export declare const
|
|
19
|
+
export declare const OK_UNDEFINED: Readonly<Success<undefined>>;
|
package/lib/esm/result.mjs
CHANGED
|
@@ -13,6 +13,7 @@ export function err(error) {
|
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
15
|
export const OK = Object.freeze(ok());
|
|
16
|
+
export const OK_FALSE = Object.freeze(ok(false));
|
|
16
17
|
export const OK_NULL = Object.freeze(ok(null));
|
|
17
18
|
export const OK_TRUE = Object.freeze(ok(true));
|
|
18
|
-
export const
|
|
19
|
+
export const OK_UNDEFINED = Object.freeze(ok(undefined));
|
package/lib/esm/sync.d.mts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export type { XDeferAsync, XDeferSync } from './defer.mjs';
|
|
2
2
|
export { xdefer as xdeferUnknown, xdeferAsync, xdeferSync as xdefer } from './defer.mjs';
|
|
3
|
+
export { parseJSON, parseJson } from './json.mjs';
|
|
3
4
|
export type { YResult, YSuccess, YFailure } from './partial.mjs';
|
|
4
|
-
export { yok, yerr, yres as yresUnknown, yresAsync, yresSync as yres, yep, YOK, YOK_NULL, YOK_TRUE,
|
|
5
|
+
export { yok, yerr, yres as yresUnknown, yresAsync, yresSync as yres, yep, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YOK_UNDEFINED } from './partial.mjs';
|
|
5
6
|
export type { Success, Failure, Result } from './result.mjs';
|
|
6
|
-
export { ok, err, OK, OK_NULL, OK_TRUE,
|
|
7
|
+
export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.mjs';
|
|
7
8
|
export { stringifyError } from './stringify-error.mjs';
|
|
8
9
|
export { toStringFailure } from './to-string-failure.mjs';
|
|
9
10
|
export { xtry as xtryUnknown, xtryAsync, xtryAsyncIterable, xtrySync as xtry, xtrySyncIterable as xtryIterable } from './try.mjs';
|
|
11
|
+
export type { AsyncIteratableResult, AsyncIteratorElement, AsyncResult, SyncIteratableResult, SyncIteratorElement, SyncResult } from './tryify.mjs';
|
|
12
|
+
export { xtryifyAsync, xtryifyAsyncIterable, xtryifySync as xtryify, xtryifySyncIterable as xtryifyIterable } from './tryify.mjs';
|
package/lib/esm/sync.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { xdefer as xdeferUnknown, xdeferAsync, xdeferSync as xdefer } from './defer.mjs';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
2
|
+
export { parseJSON, parseJson } from './json.mjs';
|
|
3
|
+
export { yok, yerr, yres as yresUnknown, yresAsync, yresSync as yres, yep, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YOK_UNDEFINED } from './partial.mjs';
|
|
4
|
+
export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.mjs';
|
|
4
5
|
export { stringifyError } from './stringify-error.mjs';
|
|
5
6
|
export { toStringFailure } from './to-string-failure.mjs';
|
|
6
7
|
export { xtry as xtryUnknown, xtryAsync, xtryAsyncIterable, xtrySync as xtry, xtrySyncIterable as xtryIterable } from './try.mjs';
|
|
8
|
+
export { xtryifyAsync, xtryifyAsyncIterable, xtryifySync as xtryify, xtryifySyncIterable as xtryifyIterable } from './tryify.mjs';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type Result } from './result.mjs';
|
|
2
|
+
import { type NotPromise } from './utils/types.mjs';
|
|
3
|
+
type AnyAsyncFunction = (...args: any[]) => Promise<unknown>;
|
|
4
|
+
type AnySyncFunction = (...args: any[]) => unknown;
|
|
5
|
+
type LastIsFunction<T extends readonly any[]> = T extends [...infer Rest, infer Last] ? Last extends (...args: any[]) => any ? true : false : false;
|
|
6
|
+
type OverloadsUnion<Fn extends Function> = Fn extends {
|
|
7
|
+
(...args: infer A1): infer R1;
|
|
8
|
+
(...args: infer A2): infer R2;
|
|
9
|
+
(...args: infer A3): infer R3;
|
|
10
|
+
(...args: infer A4): infer R4;
|
|
11
|
+
(...args: infer A5): infer R5;
|
|
12
|
+
(...args: infer A6): infer R6;
|
|
13
|
+
(...args: infer A7): infer R7;
|
|
14
|
+
(...args: infer A8): infer R8;
|
|
15
|
+
(...args: infer A9): infer R9;
|
|
16
|
+
(...args: infer A10): infer R10;
|
|
17
|
+
} ? ((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3) | ((...args: A4) => R4) | ((...args: A5) => R5) | ((...args: A6) => R6) | ((...args: A7) => R7) | ((...args: A8) => R8) | ((...args: A9) => R9) | ((...args: A10) => R10) : Fn;
|
|
18
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
19
|
+
type WrapAsyncOverload<Fn extends AnyAsyncFunction, Err extends Error> = Fn extends (...args: infer Args) => unknown ? (...args: Args) => AsyncResult<Fn, Err> : never;
|
|
20
|
+
type WrapAsyncIterableOverload<Fn extends AsyncIterableFunction, Err extends Error> = Fn extends (...args: infer Args) => AsyncIterable<unknown> ? (...args: Args) => AsyncIteratableResult<Fn, Err> : never;
|
|
21
|
+
type WrapSyncOverload<Fn extends AnySyncFunction, Err extends Error> = Fn extends (...args: infer Args) => unknown ? (...args: Args) => SyncResult<Fn, Err> : never;
|
|
22
|
+
type WrapSyncIterableOverload<Fn extends SyncIterableFunction, Err extends Error> = Fn extends (...args: infer Args) => Iterable<unknown> ? (...args: Args) => SyncIteratableResult<Fn, Err> : never;
|
|
23
|
+
export type AsyncFunction<Fn extends (...args: any[]) => any> = ReturnType<Fn> extends Promise<any> ? Fn : never;
|
|
24
|
+
export type AsyncIterableFunction = (...args: any[]) => AsyncIterable<unknown>;
|
|
25
|
+
export type AsyncIteratorElement<T> = T extends Promise<infer P> ? AsyncIteratorElement<P> : T extends AsyncIterator<infer E> ? E : T extends AsyncIterable<infer E> ? E : T extends {
|
|
26
|
+
[Symbol.asyncIterator]: (...args: unknown[]) => infer I;
|
|
27
|
+
} ? AsyncIteratorElement<I> : unknown;
|
|
28
|
+
export type AsyncResult<T extends (...args: unknown[]) => unknown, Err extends Error> = Promise<Result<Awaited<ReturnType<T>>, Err>>;
|
|
29
|
+
export type AsyncIteratableResult<T extends (...args: unknown[]) => unknown, Err extends Error> = AsyncIterable<Result<AsyncIteratorElement<ReturnType<T>>, Err>, unknown, unknown>;
|
|
30
|
+
export type PreserveAsyncOverloads<Fn extends AnyAsyncFunction, Err extends Error> = UnionToIntersection<WrapAsyncOverload<OverloadsUnion<Fn>, Err>>;
|
|
31
|
+
export type PreserveAsyncIterableOverloads<Fn extends AsyncIterableFunction, Err extends Error> = UnionToIntersection<WrapAsyncIterableOverload<OverloadsUnion<Fn>, Err>>;
|
|
32
|
+
export type PreserveSyncOverloads<Fn extends AnySyncFunction, Err extends Error> = UnionToIntersection<WrapSyncOverload<OverloadsUnion<Fn>, Err>>;
|
|
33
|
+
export type PreserveSyncIterableOverloads<Fn extends SyncIterableFunction, Err extends Error> = UnionToIntersection<WrapSyncIterableOverload<OverloadsUnion<Fn>, Err>>;
|
|
34
|
+
export type SyncFunction<Fn extends (...args: any[]) => any> = LastIsFunction<Parameters<Fn>> extends true ? never : ReturnType<Fn> extends Promise<any> ? never : Fn;
|
|
35
|
+
export type SyncIterableFunction = (...args: any[]) => Iterable<unknown>;
|
|
36
|
+
export type SyncIteratorElement<T> = T extends Iterator<infer E> ? E : T extends Iterable<infer E> ? E : T extends {
|
|
37
|
+
[Symbol.iterator]: (...args: unknown[]) => infer I;
|
|
38
|
+
} ? SyncIteratorElement<I> : unknown;
|
|
39
|
+
export type SyncResult<T extends (...args: unknown[]) => NotPromise<unknown>, Err extends Error> = Result<ReturnType<T>, Err>;
|
|
40
|
+
export type SyncIteratableResult<T extends (...args: unknown[]) => unknown, Err extends Error> = Iterable<Result<SyncIteratorElement<ReturnType<T>>, Err>, unknown, unknown>;
|
|
41
|
+
export declare function xtryifyAsync<Fn extends AnyAsyncFunction, Err extends Error>(fn: AsyncFunction<Fn>): PreserveAsyncOverloads<Fn, Err>;
|
|
42
|
+
export declare function xtryifyAsyncIterable<Fn extends AsyncIterableFunction, Err extends Error>(fn: Fn): PreserveAsyncIterableOverloads<Fn, Err>;
|
|
43
|
+
export declare function xtryifySync<Fn extends AnySyncFunction, Err extends Error>(fn: SyncFunction<Fn>): PreserveSyncOverloads<Fn, Err>;
|
|
44
|
+
export declare function xtryifySyncIterable<Fn extends SyncIterableFunction, Err extends Error>(fn: Fn): PreserveSyncIterableOverloads<Fn, Err>;
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { xtryAsync, xtryAsyncIterable, xtrySync, xtrySyncIterable } from './try.mjs';
|
|
2
|
+
export function xtryifyAsync(fn) {
|
|
3
|
+
return async function (...args) {
|
|
4
|
+
return xtryAsync(async () => fn(...args));
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
export function xtryifyAsyncIterable(fn) {
|
|
8
|
+
return function (...args) {
|
|
9
|
+
return xtryAsyncIterable(fn(...args));
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export function xtryifySync(fn) {
|
|
13
|
+
return function (...args) {
|
|
14
|
+
return xtrySync(() => fn(...args));
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export function xtryifySyncIterable(fn) {
|
|
18
|
+
return function (...args) {
|
|
19
|
+
return xtrySyncIterable(fn(...args));
|
|
20
|
+
};
|
|
21
|
+
}
|
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.9.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Baptiste Augrain",
|
|
7
7
|
"email": "daiyam@zokugun.org"
|
|
@@ -25,6 +25,14 @@
|
|
|
25
25
|
"import": "./lib/esm/async.mjs",
|
|
26
26
|
"require": "./lib/cjs/async.cjs"
|
|
27
27
|
},
|
|
28
|
+
"./json": {
|
|
29
|
+
"import": "./lib/esm/json.mjs",
|
|
30
|
+
"require": "./lib/cjs/json.cjs"
|
|
31
|
+
},
|
|
32
|
+
"./result": {
|
|
33
|
+
"import": "./lib/esm/result.mjs",
|
|
34
|
+
"require": "./lib/cjs/result.cjs"
|
|
35
|
+
},
|
|
28
36
|
"./sync": {
|
|
29
37
|
"import": "./lib/esm/sync.mjs",
|
|
30
38
|
"require": "./lib/cjs/sync.cjs"
|
|
@@ -40,6 +48,12 @@
|
|
|
40
48
|
"async": [
|
|
41
49
|
"./lib/cjs/async.d.cts"
|
|
42
50
|
],
|
|
51
|
+
"json": [
|
|
52
|
+
"./lib/cjs/json.d.cts"
|
|
53
|
+
],
|
|
54
|
+
"result": [
|
|
55
|
+
"./lib/cjs/result.d.cts"
|
|
56
|
+
],
|
|
43
57
|
"sync": [
|
|
44
58
|
"./lib/cjs/sync.d.cts"
|
|
45
59
|
]
|