unthrown 5.12.0 → 5.12.1

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/dist/index.cjs CHANGED
@@ -1206,12 +1206,15 @@ function DoAsync() {
1206
1206
  *
1207
1207
  * @remarks
1208
1208
  * `null` and `undefined` map to `Err(onAbsent())`; any other value (including
1209
- * falsy ones like `0`, `""`, `false`) maps to `Ok`.
1209
+ * falsy ones like `0`, `""`, `false`) maps to `Ok`. A throw inside `onAbsent`
1210
+ * becomes a `Defect`. Its return must be synchronous ({@link NotThenable});
1211
+ * a thenable slipped past the types becomes a `Defect`, with an orphaned
1212
+ * Promise silenced and a lazy thenable left unstarted.
1210
1213
  *
1211
1214
  * @typeParam T - the (nullable) value type.
1212
1215
  * @typeParam E - the error produced when the value is absent.
1213
1216
  * @param value - the possibly-absent value.
1214
- * @param onAbsent - lazily produces the error for the absent case.
1217
+ * @param onAbsent - synchronously and lazily produces the error for the absent case.
1215
1218
  *
1216
1219
  * @category Interop
1217
1220
  *
@@ -1226,7 +1229,13 @@ function DoAsync() {
1226
1229
  * ```
1227
1230
  */
1228
1231
  function fromNullable(value, onAbsent) {
1229
- return value === null || value === void 0 ? Err(onAbsent()) : Ok(value);
1232
+ if (value !== null && value !== void 0) return Ok(value);
1233
+ try {
1234
+ const error = onAbsent();
1235
+ return isThenable(error) ? thenableReturnDefect(error, "unthrown: fromNullable's onAbsent must be synchronous — it returned a thenable") : Err(error);
1236
+ } catch (cause) {
1237
+ return defectRes(cause);
1238
+ }
1230
1239
  }
1231
1240
  /**
1232
1241
  * Wrap a throwing synchronous function so it returns a {@link Result} instead of
package/dist/index.d.cts CHANGED
@@ -532,12 +532,15 @@ export declare function DoAsync(): AsyncResult<{}, never>;
532
532
  *
533
533
  * @remarks
534
534
  * `null` and `undefined` map to `Err(onAbsent())`; any other value (including
535
- * falsy ones like `0`, `""`, `false`) maps to `Ok`.
535
+ * falsy ones like `0`, `""`, `false`) maps to `Ok`. A throw inside `onAbsent`
536
+ * becomes a `Defect`. Its return must be synchronous ({@link NotThenable});
537
+ * a thenable slipped past the types becomes a `Defect`, with an orphaned
538
+ * Promise silenced and a lazy thenable left unstarted.
536
539
  *
537
540
  * @typeParam T - the (nullable) value type.
538
541
  * @typeParam E - the error produced when the value is absent.
539
542
  * @param value - the possibly-absent value.
540
- * @param onAbsent - lazily produces the error for the absent case.
543
+ * @param onAbsent - synchronously and lazily produces the error for the absent case.
541
544
  *
542
545
  * @category Interop
543
546
  *
@@ -551,7 +554,7 @@ export declare function DoAsync(): AsyncResult<{}, never>;
551
554
  * fromNullable(0, () => "absent").getOr(-1); // => 0 (falsy but present)
552
555
  * ```
553
556
  */
554
- export declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () => E): Result<NonNullable<T>, E>;
557
+ export declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () => E & NotThenable<E>): Result<NonNullable<T>, E>;
555
558
  /**
556
559
  * Wrap a throwing synchronous function so it returns a {@link Result} instead of
557
560
  * throwing.
package/dist/index.d.mts CHANGED
@@ -532,12 +532,15 @@ export declare function DoAsync(): AsyncResult<{}, never>;
532
532
  *
533
533
  * @remarks
534
534
  * `null` and `undefined` map to `Err(onAbsent())`; any other value (including
535
- * falsy ones like `0`, `""`, `false`) maps to `Ok`.
535
+ * falsy ones like `0`, `""`, `false`) maps to `Ok`. A throw inside `onAbsent`
536
+ * becomes a `Defect`. Its return must be synchronous ({@link NotThenable});
537
+ * a thenable slipped past the types becomes a `Defect`, with an orphaned
538
+ * Promise silenced and a lazy thenable left unstarted.
536
539
  *
537
540
  * @typeParam T - the (nullable) value type.
538
541
  * @typeParam E - the error produced when the value is absent.
539
542
  * @param value - the possibly-absent value.
540
- * @param onAbsent - lazily produces the error for the absent case.
543
+ * @param onAbsent - synchronously and lazily produces the error for the absent case.
541
544
  *
542
545
  * @category Interop
543
546
  *
@@ -551,7 +554,7 @@ export declare function DoAsync(): AsyncResult<{}, never>;
551
554
  * fromNullable(0, () => "absent").getOr(-1); // => 0 (falsy but present)
552
555
  * ```
553
556
  */
554
- export declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () => E): Result<NonNullable<T>, E>;
557
+ export declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () => E & NotThenable<E>): Result<NonNullable<T>, E>;
555
558
  /**
556
559
  * Wrap a throwing synchronous function so it returns a {@link Result} instead of
557
560
  * throwing.
package/dist/index.mjs CHANGED
@@ -1205,12 +1205,15 @@ function DoAsync() {
1205
1205
  *
1206
1206
  * @remarks
1207
1207
  * `null` and `undefined` map to `Err(onAbsent())`; any other value (including
1208
- * falsy ones like `0`, `""`, `false`) maps to `Ok`.
1208
+ * falsy ones like `0`, `""`, `false`) maps to `Ok`. A throw inside `onAbsent`
1209
+ * becomes a `Defect`. Its return must be synchronous ({@link NotThenable});
1210
+ * a thenable slipped past the types becomes a `Defect`, with an orphaned
1211
+ * Promise silenced and a lazy thenable left unstarted.
1209
1212
  *
1210
1213
  * @typeParam T - the (nullable) value type.
1211
1214
  * @typeParam E - the error produced when the value is absent.
1212
1215
  * @param value - the possibly-absent value.
1213
- * @param onAbsent - lazily produces the error for the absent case.
1216
+ * @param onAbsent - synchronously and lazily produces the error for the absent case.
1214
1217
  *
1215
1218
  * @category Interop
1216
1219
  *
@@ -1225,7 +1228,13 @@ function DoAsync() {
1225
1228
  * ```
1226
1229
  */
1227
1230
  function fromNullable(value, onAbsent) {
1228
- return value === null || value === void 0 ? Err(onAbsent()) : Ok(value);
1231
+ if (value !== null && value !== void 0) return Ok(value);
1232
+ try {
1233
+ const error = onAbsent();
1234
+ return isThenable(error) ? thenableReturnDefect(error, "unthrown: fromNullable's onAbsent must be synchronous — it returned a thenable") : Err(error);
1235
+ } catch (cause) {
1236
+ return defectRes(cause);
1237
+ }
1229
1238
  }
1230
1239
  /**
1231
1240
  * Wrap a throwing synchronous function so it returns a {@link Result} instead of
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unthrown",
3
- "version": "5.12.0",
3
+ "version": "5.12.1",
4
4
  "description": "Explicit errors as values, with a separate defect (panic) channel",
5
5
  "keywords": [
6
6
  "defect",