@zokugun/xtry 0.11.6 → 0.12.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 CHANGED
@@ -1,11 +1,13 @@
1
1
  [@zokugun/xtry](https://github.com/zokugun/node-xtry)
2
- ==========================================================
2
+ =====================================================
3
3
 
4
- [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
5
- [![NPM Version](https://img.shields.io/npm/v/@zokugun/xtry.svg?colorB=green)](https://www.npmjs.com/package/@zokugun/xtry)
6
- [![Donation](https://img.shields.io/badge/donate-ko--fi-green)](https://ko-fi.com/daiyam)
7
- [![Donation](https://img.shields.io/badge/donate-liberapay-green)](https://liberapay.com/daiyam/donate)
8
- [![Donation](https://img.shields.io/badge/donate-paypal-green)](https://paypal.me/daiyam99)
4
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue)](./LICENSE)
5
+ [![NPM Version](https://img.shields.io/npm/v/@zokugun/xtry?color=brightgreen)](https://www.npmjs.com/package/@zokugun/xtry)
6
+ [![NodeJS Version](https://img.shields.io/badge/node.js-%3E%3D%2018-green)](#requirements)
7
+ [![Modules](https://img.shields.io/badge/modules-ESM%20%7C%20CJS-green)](#requirements)
8
+ [![Ko-fi](https://img.shields.io/badge/Ko--fi-f87171?logo=kofi&logoColor=white)](https://ko-fi.com/daiyam)
9
+ [![Liberapay](https://img.shields.io/badge/Liberapay-facc15?logo=liberapay&logoColor=black)](https://liberapay.com/daiyam/donate)
10
+ [![PayPal](https://img.shields.io/badge/PayPal-5277C3?logo=paypal&logoColor=white)](https://paypal.me/daiyam99)
9
11
 
10
12
  Simple `try/catch` wrappers that always return a `Result` discriminated union, plus ready-made helpers (`ok`, `err`) for predictable control flow.
11
13
 
@@ -23,17 +25,23 @@ Installation
23
25
  npm install @zokugun/xtry
24
26
  ```
25
27
 
28
+ Requirements
29
+ ------------
30
+
31
+ - **Node.js**: `v18` or higher
32
+ - **Modules**: ECMAScript Modules (`ESM`) or CommonJS (`CJS`)
33
+
26
34
  Quick Start
27
35
  -----------
28
36
 
29
37
  ```typescript
30
- import { xtry } from '@zokugun/xtry'
38
+ import { xtry } from '@zokugun/xtry';
31
39
 
32
40
  const userResult = await xtry(fetchUserFromApi());
33
41
 
34
42
  if(userResult.fails) {
35
- console.error(userResult.error);
36
- return;
43
+ console.error(userResult.error);
44
+ return;
37
45
  }
38
46
 
39
47
  console.log('User loaded:', userResult.value);
@@ -43,30 +51,30 @@ Advanced Example
43
51
  ----------------
44
52
 
45
53
  ```typescript
46
- import { err, type Result, xtry } from '@zokugun/xtry'
54
+ import { err, type Result, xtry } from '@zokugun/xtry';
47
55
 
48
56
  export type FoobarError = { type: 'FOOBAR'; message: string };
49
57
 
50
58
  async function foobar(): Result<number, FoobarError> {
51
- const result = await xtry(fetchUserFromApi());
59
+ const result = await xtry(fetchUserFromApi());
52
60
 
53
- if(fails) {
54
- return err({ type: 'FOOBAR', message: 'The promise has failed...' });
55
- }
61
+ if(fails) {
62
+ return err({ type: 'FOOBAR', message: 'The promise has failed...' });
63
+ }
56
64
 
57
- return xtry(() => calculateAge(result.value));
65
+ return xtry(() => calculateAge(result.value));
58
66
  }
59
67
 
60
- async function main() {
61
- const result = await foobar();
68
+ async function main(): void {
69
+ const result = await foobar();
62
70
 
63
- if(result.fails) {
64
- console.error(result.error.message);
71
+ if(result.fails) {
72
+ console.error(result.error.message);
65
73
 
66
- return;
67
- }
74
+ return;
75
+ }
68
76
 
69
- console.log(result.value);
77
+ console.log(result.value);
70
78
  }
71
79
  ```
72
80
 
@@ -76,52 +84,52 @@ Partial Example
76
84
  `YResult` extends the base `Result` union with a `success` flag so you can distinguish "valid failure" states from true errors.
77
85
 
78
86
  ```typescript
79
- import { err, ok, yerr, yok, type YResult } from '@zokugun/xtry'
87
+ import { err, ok, yerr, yok, type YResult } from '@zokugun/xtry';
80
88
 
81
89
  function toNumber(input: string): YResult<number, MyError, 'empty-string'> {
82
- if(input.length > 0) {
83
- return yerr('empty-string');
84
- }
90
+ if(input.length > 0) {
91
+ return yerr('empty-string');
92
+ }
85
93
 
86
- const floatValue = Number.parseFloat(input);
94
+ const floatValue = Number.parseFloat(input);
87
95
 
88
- if(Number.isNaN(floatValue)) {
89
- return err({ type: '#VALUE!' });
90
- }
96
+ if(Number.isNaN(floatValue)) {
97
+ return err({ type: '#VALUE!' });
98
+ }
91
99
 
92
- return yok(floatValue);
100
+ return yok(floatValue);
93
101
  }
94
102
 
95
103
  function add(_x: string, _y: number): Result<number, MyError> {
96
- const x = toNumber(_x);
97
- if(x.fails) {
98
- return x;
99
- }
100
- if(!x.success) {
101
- return ok(0);
102
- }
103
-
104
- const y = toNumber(_y);
105
- if(y.fails) {
106
- return y;
107
- }
108
- if(!y.success) {
109
- return ok(0);
110
- }
111
-
112
- return x.value + y.value;
104
+ const x = toNumber(_x);
105
+ if(x.fails) {
106
+ return x;
107
+ }
108
+ if(!x.success) {
109
+ return ok(0);
110
+ }
111
+
112
+ const y = toNumber(_y);
113
+ if(y.fails) {
114
+ return y;
115
+ }
116
+ if(!y.success) {
117
+ return ok(0);
118
+ }
119
+
120
+ return x.value + y.value;
113
121
  }
114
122
  ```
115
123
 
116
124
  Tips
117
125
  ----
118
126
 
119
- - Narrow on `fails` first, then use other flags (`success`, custom `miscue` or `value`) for the happy-path branching.
127
+ - Narrow on `fails` first, then use other flags (`success`, custom `issue` or `value`) for the happy-path branching.
120
128
 
121
- API reference
129
+ API Reference
122
130
  -------------
123
131
 
124
- ### Result helpers
132
+ ### Result Helpers
125
133
 
126
134
  ```typescript
127
135
  type Success<T> = { fails: false; value: T; error: undefined };
@@ -132,11 +140,11 @@ function ok<T>(value?: T): Success<T>;
132
140
  function err<E>(error: E): Failure<E>;
133
141
  ```
134
142
 
135
- #### Pre-built `ok` constants
143
+ #### Pre-built `ok` Constants
136
144
 
137
145
  To minimize allocations when returning the same `Success` shape, you can reuse the exported frozen helpers:
138
146
 
139
- | Constant | Wrapped value | Type | Typical usage |
147
+ | Constant | Wrapped Value | Type | Typical Usage |
140
148
  | -------------- | --------------- | -------------------- | ------------------------------------------------------ |
141
149
  | `OK` | `ok()` | `Success<void>` | Generic void success (e.g., cleanup, notifications) |
142
150
  | `OK_NULL` | `ok(null)` | `Success<null>` | APIs that explicitly signal "nothing" with `null` |
@@ -144,7 +152,7 @@ To minimize allocations when returning the same `Success` shape, you can reuse t
144
152
  | `OK_TRUE` | `ok(true)` | `Success<true>` | Flag-style functions (`enable()` / `disable()`) |
145
153
  | `OK_FALSE` | `ok(false)` | `Success<false>` | Guard checks that succeed with `false` |
146
154
 
147
- ### Try helpers
155
+ ### Try Helpers
148
156
 
149
157
  ```typescript
150
158
  function xtry<T, E>(func: (() => MaybePromise<T>) | Promise<T>, handler?: (error: unknown) => void | E): MaybePromise<Result<T, E>>;
@@ -154,7 +162,6 @@ function xtryAsync<T, E>(func: (() => Promise<T>) | Promise<T>, handler?: (error
154
162
  function xtryAsyncIterable<T, E>(iterable: (() => MaybePromise<AsyncIterable<T>>) | MaybePromise<AsyncIterable<T>>, handler?: (error: unknown) => void | E): AsyncIterable<Result<T, E>>;
155
163
  function xtrySync<T, E>(func: () => Exclude<T, Promise<unknown>>, handler?: (error: unknown) => void | E): Result<T, E>;
156
164
  function xtrySyncIterable<T, E>(iterable: (() => Iterable<T>) | Iterable<T>, handler?: (error: unknown) => void | E): Iterable<Result<T, E>>;
157
- (error: unknown) => void | E): AsyncIterable<Result<T, E>>;
158
165
 
159
166
  function stringifyError(error: unknown): string;
160
167
  ```
@@ -175,14 +182,14 @@ All helpers:
175
182
  - execute the supplied function and capture thrown values;
176
183
  - call the optional `handler` before turning that value into `err(error)`;
177
184
 
178
- ### xtryify helpers
185
+ ### Xtryify Helpers
179
186
 
180
187
  `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
188
 
182
189
  ```typescript
183
- import { xtryifyAsync, xtryifySync } from '@zokugun/xtry'
190
+ import { xtryifyAsync, xtryifySync } from '@zokugun/xtry';
184
191
 
185
- const fetchUserSafely = xtryifyAsync((id: string) => fetch(`/users/${id}`).then(r => r.json()));
192
+ const fetchUserSafely = xtryifyAsync((id: string) => fetch(`/users/${id}`).then((r) => r.json()));
186
193
  const parseConfig = xtryifySync(() => JSON.parse(readFileSync('config.json', 'utf8')));
187
194
 
188
195
  const userResult = await fetchUserSafely('42');
@@ -196,15 +203,15 @@ Available variants mirror the regular helpers:
196
203
 
197
204
  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
205
 
199
- ### Partial helpers
206
+ ### Partial Helpers
200
207
 
201
208
  ```typescript
202
209
  type YSuccess<T> = Success<T> & { success: true };
203
- type YFailure<M> = { fails: false; success: false; miscue: M; value: undefined; error: undefined };
204
- type YResult<T, E, M> = Failure<E> | YSuccess<T> | YFailure<M>;
210
+ type YFailure<I> = { fails: false; success: false; issue: I; value: undefined; error: undefined };
211
+ type YResult<T, E, I> = Failure<E> | YSuccess<T> | YFailure<I>;
205
212
 
206
213
  function yok<T>(value: T): YSuccess<T>;
207
- function yerr<M>(type: M): YFailure<M>;
214
+ function yerr<I>(type: I): YFailure<I>;
208
215
  function yres<T, E>(result: MaybePromise<Result<T, E>>): MaybePromise<Failure<E> | YSuccess<T>>;
209
216
  function yresSync<T, E>(result: Result<T, E>): Failure<E> | YSuccess<T>;
210
217
  function yresAsync<T, E>(promise: Promise<Result<T, E>>): Promise<Failure<E> | YSuccess<T>>;
@@ -213,7 +220,7 @@ function yep<T>(result: Success<T>): YSuccess<T>;
213
220
 
214
221
  These helpers are useful when you need to separate soft rejections (`success: false`) from hard failures (`fails: true`).
215
222
 
216
- ### Defer helpers
223
+ ### Defer Helpers
217
224
 
218
225
  ```typescript
219
226
  type DeferSync<E> = (result?: Result<unknown, E>) => Result<unknown, E> | Success<void>;
@@ -227,37 +234,69 @@ function xdeferAsync<E>(callback: (() => Promise<Result<unknown, E>>) | Promise<
227
234
  Use these helpers to express "finally" logic that can also fail while preserving the original result when needed:
228
235
 
229
236
  ```typescript
230
- import { stringifyError, xdefer, xtry } from '@zokugun/xtry/async'
237
+ import { stringifyError, xdefer, xtry } from '@zokugun/xtry/async';
231
238
 
232
239
  function test(): Result<void, string> {
233
- const closeConnection = xdefer(xtry(connection.close()));
240
+ const closeConnection = xdefer(xtry(connection.close()));
234
241
 
235
- const queryResult = await xtry(connection.query('SELECT 1'));
242
+ const queryResult = await xtry(connection.query('SELECT 1'));
236
243
 
237
- if(queryResult.fails) {
238
- return closeConnection(err(stringifyError(queryResult.error)))
239
- }
244
+ if(queryResult.fails) {
245
+ return closeConnection(err(stringifyError(queryResult.error)));
246
+ }
240
247
 
241
- ...
248
+ // ...
242
249
 
243
- return closeConnection();
250
+ return closeConnection();
244
251
  }
245
252
  ```
246
253
 
247
254
  - `xdefer` inspects the callback result: if it fails, it becomes the returned error unless the main result already failed.
248
255
  - Passing a promise (or async factory) makes the defer helper async-aware; `xdeferSync`/`xdeferAsync` let you pin the behavior explicitly for bundlers.
249
- - Calling the returned function with no arguments just runs the deferred work and yields `ok()`.
256
+ - Calling the returnedfunction with no arguments just runs the deferred work and yields `ok()`.
257
+
258
+ ### Unwrap Helpers
259
+
260
+ The unwrap entry point provides functional helpers for consuming `Result` values:
261
+
262
+ ```typescript
263
+ import { map, match, unwrap, unwrapOr } from '@zokugun/xtry/unwrap';
264
+
265
+ const content1 = unwrap(readFile(filePath)); // throw if the result failed
266
+ const content2 = unwrapOr(readFile(filePath), 'fallback');
267
+ const content3 = map(readFile(filePath), (value) => value.toString());
268
+
269
+ const content4 = match(readFile(filePath), {
270
+ failure: (error) => `Failed: ${error}`,
271
+ success: (value) => `Value: ${value}`,
272
+ });
273
+ ```
274
+
275
+ ```typescript
276
+ function map<T, E, U>(result: Result<T, E>, transform: (value: T) => U): Result<U, E>;
277
+ function match<T, E, R>(result: Result<T, E>, handlers: { failure: (error: E) => R; success: (value: T) => R }): R;
278
+ function unwrap<T, E>(result: Result<T, E>): T;
279
+ function unwrapOr<T, E, F>(result: Result<T, E>, fallback: F): F | T;
280
+ ```
281
+
282
+ - `map` transforms successful values and preserves failures unchanged.
283
+ - `match` invokes exactly one handler based on the result state.
284
+ - `unwrap` returns the successful value, rethrows `Error` failures, and converts other failures to `Error` instances.
285
+ - `unwrapOr` returns the successful value or the supplied fallback.
250
286
 
251
- Module entry points
287
+ Module Entry Points
252
288
  -------------------
253
289
 
254
290
  Choose the entry point that matches your environment and naming preferences:
255
291
 
256
- | Import path | Description | `xtry` name | `xdefer` name | Extra alias |
257
- | --------------------- | --------------------------- | ------------------------------- | ------------------------------------- | ------------------------------- |
258
- | `@zokugun/xtry` | Both sync, async and hybrid | `xtry`, `xtryAsync`, `xtrySync` | `xdefer`, `xdeferAsync`, `xdeferSync` | `yres`, `yresAsync`, `yresSync` |
259
- | `@zokugun/xtry/async` | Async-only | `xtryAsync` as `xtry` | `xdeferAsync` as `xdefer` | `yresAsync` as `yres` |
260
- | `@zokugun/xtry/sync` | Synchronous-only | `xtrySync` as `xtry` | `xdeferSync` as `xdefer` | `yresSync` as `yres` |
292
+ | Import Path | Description | `xtry` Name | `xdefer` Name | Extra Alias |
293
+ | ---------------------- | ---------------------------- | ------------------------------- | ------------------------------------- | ------------------------------- |
294
+ | `@zokugun/xtry` | All sync, async and hybrid | `xtry`, `xtryAsync`, `xtrySync` | `xdefer`, `xdeferAsync`, `xdeferSync` | `yres`, `yresAsync`, `yresSync` |
295
+ | `@zokugun/xtry/async` | Async-only | `xtryAsync` as `xtry` | `xdeferAsync` as `xdefer` | `yresAsync` as `yres` |
296
+ | `@zokugun/xtry/json` | JSON helpers | | | |
297
+ | `@zokugun/xtry/result` | Result types, `ok` and `err` | | | |
298
+ | `@zokugun/xtry/sync` | Synchronous-only | `xtrySync` as `xtry` | `xdeferSync` as `xdefer` | `yresSync` as `yres` |
299
+ | `@zokugun/xtry/unwrap` | Unwrap helpers | | | |
261
300
 
262
301
  All modules share the same `Result`, `Partial`, and `stringifyError` exports, so you can swap entry points without refactoring types.
263
302
 
package/lib/cjs/async.cjs CHANGED
@@ -1,28 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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;
3
+ exports.unwrapOr = exports.unwrap = exports.match = exports.map = exports.xtryifySyncIterable = exports.xtryifySync = exports.xtryifyIterable = exports.xtryify = exports.xtryUnknown = exports.xtrySyncIterable = exports.xtrySync = exports.xtryIterable = exports.xtry = exports.toStringFailure = exports.stringifyError = exports.OK_UNDEFINED = exports.OK_TRUE = exports.OK_NULL = exports.OK_FALSE = exports.OK = exports.ok = exports.err = exports.yresUnknown = exports.yresSync = exports.yres = exports.YOK_UNDEFINED = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK_FALSE = exports.YOK = exports.yok = exports.yerr = exports.yep = exports.parseJson = exports.parseJSON = exports.xdeferUnknown = exports.xdeferSync = exports.xdefer = void 0;
4
4
  var defer_js_1 = require("./defer.cjs");
5
- Object.defineProperty(exports, "xdeferUnknown", { enumerable: true, get: function () { return defer_js_1.xdefer; } });
6
5
  Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdeferAsync; } });
7
6
  Object.defineProperty(exports, "xdeferSync", { enumerable: true, get: function () { return defer_js_1.xdeferSync; } });
7
+ Object.defineProperty(exports, "xdeferUnknown", { enumerable: true, get: function () { return defer_js_1.xdefer; } });
8
8
  var json_js_1 = require("./json.cjs");
9
9
  Object.defineProperty(exports, "parseJSON", { enumerable: true, get: function () { return json_js_1.parseJSON; } });
10
10
  Object.defineProperty(exports, "parseJson", { enumerable: true, get: function () { return json_js_1.parseJson; } });
11
11
  var partial_js_1 = require("./partial.cjs");
12
- Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
13
- Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
14
- Object.defineProperty(exports, "yresUnknown", { enumerable: true, get: function () { return partial_js_1.yres; } });
15
- Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yresAsync; } });
16
- Object.defineProperty(exports, "yresSync", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
17
12
  Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
13
+ Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
14
+ Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
18
15
  Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
19
16
  Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
20
17
  Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
21
18
  Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
22
19
  Object.defineProperty(exports, "YOK_UNDEFINED", { enumerable: true, get: function () { return partial_js_1.YOK_UNDEFINED; } });
20
+ Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yresAsync; } });
21
+ Object.defineProperty(exports, "yresSync", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
22
+ Object.defineProperty(exports, "yresUnknown", { enumerable: true, get: function () { return partial_js_1.yres; } });
23
23
  var result_js_1 = require("./result.cjs");
24
- Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
25
24
  Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
25
+ Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
26
26
  Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
27
27
  Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
28
28
  Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
@@ -33,13 +33,18 @@ Object.defineProperty(exports, "stringifyError", { enumerable: true, get: functi
33
33
  var to_string_failure_js_1 = require("./to-string-failure.cjs");
34
34
  Object.defineProperty(exports, "toStringFailure", { enumerable: true, get: function () { return to_string_failure_js_1.toStringFailure; } });
35
35
  var try_js_1 = require("./try.cjs");
36
- Object.defineProperty(exports, "xtryUnknown", { enumerable: true, get: function () { return try_js_1.xtry; } });
37
36
  Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtryAsync; } });
38
37
  Object.defineProperty(exports, "xtryIterable", { enumerable: true, get: function () { return try_js_1.xtryAsyncIterable; } });
39
38
  Object.defineProperty(exports, "xtrySync", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
40
39
  Object.defineProperty(exports, "xtrySyncIterable", { enumerable: true, get: function () { return try_js_1.xtrySyncIterable; } });
40
+ Object.defineProperty(exports, "xtryUnknown", { enumerable: true, get: function () { return try_js_1.xtry; } });
41
41
  var tryify_js_1 = require("./tryify.cjs");
42
42
  Object.defineProperty(exports, "xtryify", { enumerable: true, get: function () { return tryify_js_1.xtryifyAsync; } });
43
43
  Object.defineProperty(exports, "xtryifyIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifyAsyncIterable; } });
44
44
  Object.defineProperty(exports, "xtryifySync", { enumerable: true, get: function () { return tryify_js_1.xtryifySync; } });
45
45
  Object.defineProperty(exports, "xtryifySyncIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifySyncIterable; } });
46
+ var unwrap_js_1 = require("./unwrap.cjs");
47
+ Object.defineProperty(exports, "map", { enumerable: true, get: function () { return unwrap_js_1.map; } });
48
+ Object.defineProperty(exports, "match", { enumerable: true, get: function () { return unwrap_js_1.match; } });
49
+ Object.defineProperty(exports, "unwrap", { enumerable: true, get: function () { return unwrap_js_1.unwrap; } });
50
+ Object.defineProperty(exports, "unwrapOr", { enumerable: true, get: function () { return unwrap_js_1.unwrapOr; } });
@@ -1,12 +1,13 @@
1
1
  export type { XDeferAsync, XDeferSync } from './defer.cjs';
2
- export { xdefer as xdeferUnknown, xdeferAsync as xdefer, xdeferSync } from './defer.cjs';
2
+ export { xdeferAsync as xdefer, xdeferSync, xdefer as xdeferUnknown } from './defer.cjs';
3
3
  export { parseJSON, parseJson } from './json.cjs';
4
- export type { YResult, YSuccess, YFailure } from './partial.cjs';
5
- export { yok, yerr, yres as yresUnknown, yresAsync as yres, yresSync, yep, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YOK_UNDEFINED } from './partial.cjs';
6
- export type { Success, Failure, Result, AsyncResult, DResult, AsyncDResult } from './result.cjs';
7
- export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.cjs';
4
+ export type { AsyncYDResult, AsyncYResult, YDResult, YFailure, YResult, YSuccess } from './partial.cjs';
5
+ export { yep, yerr, yok, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YOK_UNDEFINED, yresAsync as yres, yresSync, yres as yresUnknown } from './partial.cjs';
6
+ export type { AsyncDResult, AsyncResult, DResult, Failure, Result, Success } from './result.cjs';
7
+ export { err, ok, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.cjs';
8
8
  export { stringifyError } from './stringify-error.cjs';
9
9
  export { toStringFailure } from './to-string-failure.cjs';
10
- export { xtry as xtryUnknown, xtryAsync as xtry, xtryAsyncIterable as xtryIterable, xtrySync, xtrySyncIterable } from './try.cjs';
11
- export type { AsyncFunction, AsyncIterableFunction, AsyncIteratableFunctionResult, AsyncIteratorElement, AsyncFunctionResult, PreserveAsyncIterableOverloads, PreserveAsyncOverloads, PreserveSyncIterableOverloads, PreserveSyncOverloads, SyncFunction, SyncIterableFunction, SyncIteratableFunctionResult, SyncIteratorElement, SyncFunctionResult } from './tryify.cjs';
10
+ export { xtryAsync as xtry, xtryAsyncIterable as xtryIterable, xtrySync, xtrySyncIterable, xtry as xtryUnknown } from './try.cjs';
11
+ export type { AsyncFunction, AsyncFunctionResult, AsyncIterableFunction, AsyncIteratableFunctionResult, AsyncIteratorElement, PreserveAsyncIterableOverloads, PreserveAsyncOverloads, PreserveSyncIterableOverloads, PreserveSyncOverloads, SyncFunction, SyncFunctionResult, SyncIterableFunction, SyncIteratableFunctionResult, SyncIteratorElement } from './tryify.cjs';
12
12
  export { xtryifyAsync as xtryify, xtryifyAsyncIterable as xtryifyIterable, xtryifySync, xtryifySyncIterable } from './tryify.cjs';
13
+ export { map, match, unwrap, unwrapOr } from './unwrap.cjs';
package/lib/cjs/defer.cjs CHANGED
@@ -5,9 +5,9 @@ exports.xdeferAsync = xdeferAsync;
5
5
  exports.xdeferSync = xdeferSync;
6
6
  const result_js_1 = require("./result.cjs");
7
7
  const is_promise_like_js_1 = require("./utils/is-promise-like.cjs");
8
- /* eslint-enable @typescript-eslint/unified-signatures */
8
+ /* eslint-enable ts/unified-signatures */
9
9
  function xdefer(callback, thisArg, ...args) {
10
- // eslint-disable-next-line @typescript-eslint/promise-function-async
10
+ // eslint-disable-next-line ts/promise-function-async
11
11
  return ((result) => {
12
12
  const finalize = (deferResult) => {
13
13
  if (deferResult.fails) {
@@ -36,7 +36,7 @@ function xdefer(callback, thisArg, ...args) {
36
36
  return finalize(deferredValue);
37
37
  });
38
38
  }
39
- /* eslint-enable @typescript-eslint/unified-signatures */
39
+ /* eslint-enable ts/unified-signatures */
40
40
  function xdeferAsync(callback, thisArg, ...args) {
41
41
  return (async (result) => {
42
42
  let deferResult;
@@ -58,7 +58,7 @@ function xdeferAsync(callback, thisArg, ...args) {
58
58
  return result ?? (0, result_js_1.ok)();
59
59
  });
60
60
  }
61
- /* eslint-enable @typescript-eslint/unified-signatures */
61
+ /* eslint-enable ts/unified-signatures */
62
62
  function xdeferSync(callback, thisArg, ...args) {
63
63
  return ((result) => {
64
64
  const deferResult = thisArg ? callback.apply(thisArg, args) : callback(...args);
@@ -1,5 +1,5 @@
1
+ import type { NonPromiseCallback } from './utils/types.cjs';
1
2
  import { type Failure, type Result, type Success } from './result.cjs';
2
- import { type NonPromiseCallback } from './utils/types.cjs';
3
3
  export type XDeferAsync<E> = {
4
4
  (): Promise<Success<void>>;
5
5
  <T>(result: Success<T>): Promise<Result<T, E>>;
package/lib/cjs/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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;
3
+ exports.unwrapOr = exports.unwrap = exports.match = exports.map = 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.ok = exports.err = exports.yresSync = exports.yresAsync = exports.yres = exports.YOK_UNDEFINED = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK_FALSE = exports.YOK = exports.yok = exports.yerr = exports.yep = 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; } });
@@ -9,20 +9,20 @@ var json_js_1 = require("./json.cjs");
9
9
  Object.defineProperty(exports, "parseJSON", { enumerable: true, get: function () { return json_js_1.parseJSON; } });
10
10
  Object.defineProperty(exports, "parseJson", { enumerable: true, get: function () { return json_js_1.parseJson; } });
11
11
  var partial_js_1 = require("./partial.cjs");
12
- Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
13
- Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
14
- Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yres; } });
15
- Object.defineProperty(exports, "yresAsync", { enumerable: true, get: function () { return partial_js_1.yresAsync; } });
16
- Object.defineProperty(exports, "yresSync", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
17
12
  Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
13
+ Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
14
+ Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
18
15
  Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
16
+ Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
19
17
  Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
20
18
  Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
21
- Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
22
19
  Object.defineProperty(exports, "YOK_UNDEFINED", { enumerable: true, get: function () { return partial_js_1.YOK_UNDEFINED; } });
20
+ Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yres; } });
21
+ Object.defineProperty(exports, "yresAsync", { enumerable: true, get: function () { return partial_js_1.yresAsync; } });
22
+ Object.defineProperty(exports, "yresSync", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
23
23
  var result_js_1 = require("./result.cjs");
24
- Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
25
24
  Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
25
+ Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
26
26
  Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
27
27
  Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
28
28
  Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
@@ -43,3 +43,8 @@ Object.defineProperty(exports, "xtryifyAsync", { enumerable: true, get: function
43
43
  Object.defineProperty(exports, "xtryifyAsyncIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifyAsyncIterable; } });
44
44
  Object.defineProperty(exports, "xtryifySync", { enumerable: true, get: function () { return tryify_js_1.xtryifySync; } });
45
45
  Object.defineProperty(exports, "xtryifySyncIterable", { enumerable: true, get: function () { return tryify_js_1.xtryifySyncIterable; } });
46
+ var unwrap_js_1 = require("./unwrap.cjs");
47
+ Object.defineProperty(exports, "map", { enumerable: true, get: function () { return unwrap_js_1.map; } });
48
+ Object.defineProperty(exports, "match", { enumerable: true, get: function () { return unwrap_js_1.match; } });
49
+ Object.defineProperty(exports, "unwrap", { enumerable: true, get: function () { return unwrap_js_1.unwrap; } });
50
+ Object.defineProperty(exports, "unwrapOr", { enumerable: true, get: function () { return unwrap_js_1.unwrapOr; } });
@@ -1,12 +1,13 @@
1
1
  export type { XDeferAsync, XDeferSync } from './defer.cjs';
2
2
  export { xdefer, xdeferAsync, xdeferSync } from './defer.cjs';
3
3
  export { parseJSON, parseJson } from './json.cjs';
4
- export type { YResult, YSuccess, YFailure } from './partial.cjs';
5
- export { yok, yerr, yres, yresAsync, yresSync, yep, YOK, YOK_NULL, YOK_TRUE, YOK_FALSE, YOK_UNDEFINED } from './partial.cjs';
6
- export type { Success, Failure, Result, AsyncResult, DResult, AsyncDResult } from './result.cjs';
7
- export { ok, err, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.cjs';
4
+ export type { AsyncYDResult, AsyncYResult, YDResult, YFailure, YResult, YSuccess } from './partial.cjs';
5
+ export { yep, yerr, yok, YOK, YOK_FALSE, YOK_NULL, YOK_TRUE, YOK_UNDEFINED, yres, yresAsync, yresSync } from './partial.cjs';
6
+ export type { AsyncDResult, AsyncResult, DResult, Failure, Result, Success } from './result.cjs';
7
+ export { err, ok, OK, OK_FALSE, OK_NULL, OK_TRUE, OK_UNDEFINED } from './result.cjs';
8
8
  export { stringifyError } from './stringify-error.cjs';
9
9
  export { toStringFailure } from './to-string-failure.cjs';
10
10
  export { xtry, xtryAsync, xtryAsyncIterable, xtrySync, xtrySyncIterable } from './try.cjs';
11
- export type { AsyncFunction, AsyncIterableFunction, AsyncIteratableFunctionResult, AsyncIteratorElement, AsyncFunctionResult, PreserveAsyncIterableOverloads, PreserveAsyncOverloads, PreserveSyncIterableOverloads, PreserveSyncOverloads, SyncFunction, SyncIterableFunction, SyncIteratableFunctionResult, SyncIteratorElement, SyncFunctionResult } from './tryify.cjs';
11
+ export type { AsyncFunction, AsyncFunctionResult, AsyncIterableFunction, AsyncIteratableFunctionResult, AsyncIteratorElement, PreserveAsyncIterableOverloads, PreserveAsyncOverloads, PreserveSyncIterableOverloads, PreserveSyncOverloads, SyncFunction, SyncFunctionResult, SyncIterableFunction, SyncIteratableFunctionResult, SyncIteratorElement } from './tryify.cjs';
12
12
  export { xtryifyAsync, xtryifyAsyncIterable, xtryifySync, xtryifySyncIterable } from './tryify.cjs';
13
+ export { map, match, unwrap, unwrapOr } from './unwrap.cjs';
@@ -1,28 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.YOK_UNDEFINED = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK_FALSE = exports.YOK = void 0;
4
- exports.yok = yok;
4
+ exports.yep = yep;
5
5
  exports.yerr = yerr;
6
+ exports.yok = yok;
6
7
  exports.yres = yres;
7
- exports.yresSync = yresSync;
8
8
  exports.yresAsync = yresAsync;
9
- exports.yep = yep;
9
+ exports.yresSync = yresSync;
10
10
  const is_promise_like_js_1 = require("./utils/is-promise-like.cjs");
11
- function yok(value) {
11
+ function yep(result) {
12
12
  return {
13
- fails: false,
13
+ ...result,
14
14
  success: true,
15
- value: value,
16
- error: undefined,
17
15
  };
18
16
  }
19
- function yerr(miscue) {
17
+ function yerr(issue) {
20
18
  return {
19
+ error: undefined,
21
20
  fails: false,
21
+ issue,
22
22
  success: false,
23
- miscue,
24
23
  value: undefined,
24
+ };
25
+ }
26
+ function yok(value) {
27
+ return {
25
28
  error: undefined,
29
+ fails: false,
30
+ success: true,
31
+ value: value,
26
32
  };
27
33
  }
28
34
  function yres(result) {
@@ -31,21 +37,15 @@ function yres(result) {
31
37
  }
32
38
  return yresSync(result);
33
39
  }
40
+ async function yresAsync(promise) {
41
+ return promise.then(yresSync);
42
+ }
34
43
  function yresSync(result) {
35
44
  if (result.fails) {
36
45
  return result;
37
46
  }
38
47
  return yep(result);
39
48
  }
40
- async function yresAsync(promise) {
41
- return promise.then(yresSync);
42
- }
43
- function yep(result) {
44
- return {
45
- ...result,
46
- success: true,
47
- };
48
- }
49
49
  exports.YOK = Object.freeze(yok());
50
50
  exports.YOK_FALSE = Object.freeze(yok(false));
51
51
  exports.YOK_NULL = Object.freeze(yok(null));
@@ -1,25 +1,28 @@
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;
6
- };
7
- export type YFailure<M> = {
1
+ import type { Failure, Result, Success } from './result.cjs';
2
+ import type { NotPromise } from './utils/types.cjs';
3
+ export type AsyncYDResult<T = void, E = string, I = string> = Promise<YResult<T, E, I>>;
4
+ export type AsyncYResult<T, E, I> = Promise<YResult<T, E, I>>;
5
+ export type YDResult<T = void, E = string, I = string> = YResult<T, E, I>;
6
+ export type YFailure<I> = {
7
+ error: undefined;
8
8
  fails: false;
9
+ issue: I;
9
10
  success: false;
10
- miscue: M;
11
11
  value: undefined;
12
- error: undefined;
13
12
  };
13
+ export type YResult<T, E, I> = Failure<E> | YFailure<I> | YSuccess<T>;
14
+ export type YSuccess<T> = {
15
+ success: true;
16
+ } & Success<T>;
17
+ type YRResult<T, E> = Failure<E> | YSuccess<T>;
18
+ export declare function yep<T>(result: Success<T>): YSuccess<T>;
19
+ export declare function yerr<I>(issue: I): YFailure<I>;
14
20
  export declare function yok(): YSuccess<void>;
15
21
  export declare function yok<T>(value: T): YSuccess<T>;
16
- export declare function yerr<M>(miscue: M): YFailure<M>;
17
- type YRResult<T, E> = Failure<E> | YSuccess<T>;
18
22
  export declare function yres<T, E>(result: NotPromise<Result<T, E>>): YRResult<T, E>;
19
23
  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
24
  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>;
25
+ export declare function yresSync<T, E>(result: NotPromise<Result<T, E>>): YRResult<T, E>;
23
26
  export declare const YOK: Readonly<YSuccess<void>>;
24
27
  export declare const YOK_FALSE: Readonly<YSuccess<boolean>>;
25
28
  export declare const YOK_NULL: Readonly<YSuccess<null>>;