yield-result 0.1.4 → 0.1.5

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
@@ -45,6 +45,16 @@ const processOrder = (id: string) => safe.async(async function* () {
45
45
  });
46
46
  ```
47
47
 
48
+ ### 🎓 How `yield*` Works Under the Hood (Didactic Guide)
49
+
50
+ Many JavaScript developers assume generators are complex, but in `yield-result`, generator delegation (**`yield*`**) is simply a **native protocol for unwrapping values**:
51
+
52
+ 1. **`Ok<T>` Protocol**: Implements `[Symbol.iterator]` to return `value` immediately without yielding anything. Therefore, `const x = yield* ok(10)` assigns `10` directly to `x` in 0 suspension steps.
53
+ 2. **`Err<E>` Protocol**: Implements `[Symbol.iterator]` to `yield` the `Err` instance once back to `safe.sync` / `safe.async`.
54
+ 3. **Short-Circuiting**: The runner (`safe.sync`) receives the `Err` object, immediately calls `gen.return()` (triggering any `try ... finally` cleanup in your function), and early-returns `err(E)`.
55
+
56
+ No exception throwing, no stack unwinding, 100% native ECMAScript iteration semantics.
57
+
48
58
  ---
49
59
 
50
60
  ## 🚀 Installation
@@ -281,10 +291,12 @@ safe.sync(function* () {
281
291
  * **`safe.sync(function* () { ... })`** / **`Result.gen(function* () { ... })`** — Executes a synchronous generator function with early-return short-circuiting on `Err`.
282
292
  * **`safe.async(async function* () { ... })`** / **`Result.gen.async(async function* () { ... })`** — Executes an async generator function with early-return short-circuiting on `Err`.
283
293
 
284
- ### Exception Wrappers
294
+ ### Exception Wrappers & Function Lifting
285
295
  * **`fromThrowable(fn, errorMapper?)`** — Wraps a throwing function into a `Result<T, E>`.
286
296
  * **`fromPromise(promise, errorMapper?)`** — Wraps a Promise into `Promise<Result<T, E>>`.
287
297
  * **`fromAsyncFn(fn, errorMapper?)`** — Wraps an async function into `Promise<Result<T, E>>`.
298
+ * **`lift(fn, errorMapper?)`** — Lifts a sync throwing function `(...args) => T` into a Result function `(...args) => Result<T, E>`.
299
+ * **`liftAsync(fn, errorMapper?)`** — Lifts an async function `(...args) => Promise<T>` into a Promise<Result> function `(...args) => Promise<Result<T, E>>`.
288
300
 
289
301
  ### Combinators & Utilities
290
302
  * **`map(result, fn)`** — Transforms the `Ok` value.
@@ -14,3 +14,11 @@ export declare const fromPromise: <T, E = unknown>(promise: Promise<T>, errorMap
14
14
  * Catches both synchronous exceptions thrown during execution and async rejections.
15
15
  */
16
16
  export declare const fromAsyncFn: <T, E = unknown>(fn: () => Promise<T>, errorMapper?: (e: unknown) => E) => Promise<Result<T, E>>;
17
+ /**
18
+ * Lifts a standard synchronous function that may throw into a Result-returning function.
19
+ */
20
+ export declare const lift: <A extends any[], T, E = unknown>(fn: (...args: A) => T, errorMapper?: (e: unknown) => E) => ((...args: A) => Result<T, E>);
21
+ /**
22
+ * Lifts an asynchronous function returning a Promise into a Promise<Result<T, E>> function.
23
+ */
24
+ export declare const liftAsync: <A extends any[], T, E = unknown>(fn: (...args: A) => Promise<T>, errorMapper?: (e: unknown) => E) => ((...args: A) => Promise<Result<T, E>>);
package/dist/wrappers.js CHANGED
@@ -37,3 +37,15 @@ export const fromAsyncFn = async (fn, errorMapper) => {
37
37
  return err(errorMapper ? errorMapper(e) : e);
38
38
  }
39
39
  };
40
+ /**
41
+ * Lifts a standard synchronous function that may throw into a Result-returning function.
42
+ */
43
+ export const lift = (fn, errorMapper) => {
44
+ return (...args) => fromThrowable(() => fn(...args), errorMapper);
45
+ };
46
+ /**
47
+ * Lifts an asynchronous function returning a Promise into a Promise<Result<T, E>> function.
48
+ */
49
+ export const liftAsync = (fn, errorMapper) => {
50
+ return (...args) => fromAsyncFn(() => fn(...args), errorMapper);
51
+ };
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "yield-result",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Rust-style ? operator for TypeScript using generators (yield*). Zero dependencies, type-safe error handling.",
5
5
  "type": "module",
6
+ "sideEffects": false,
6
7
  "main": "./dist/index.js",
7
8
  "module": "./dist/index.js",
8
9
  "types": "./dist/index.d.ts",