Repository: karen-irc/option-t
Pull request #1552 (2023-02-03 17:50:33)

Move _unwrap_ to next to the `PlainOption` type definition
This do same things with #1550


Patch:
diff --git a/src/PlainOption/Option.ts b/src/PlainOption/Option.ts
index 37741be0..68fa88b9 100644
--- a/src/PlainOption/Option.ts
+++ b/src/PlainOption/Option.ts
@@ -1,3 +1,5 @@
+import { ERR_MSG_UNWRAP_SOME_BUT_INPUT_IS_NONE } from './internal/ErrorMessage.js';
+
 /**
  *  @deprecated
  *  Consider to use `Nullable<T>`, `Undefinable<T>`, or `Maybe<T>` to express an absence of a value.
@@ -44,8 +46,8 @@ export type Option<T> = Some<T> | None;
  *  But we recommend to use factory and utility functions for forward compatibility.
  *
  *  - {@link createSome()} to create a value of `Some(T)`.
- *  - {@link isSome} to check whether the value is `Some(T)`.
- *  - `unwrap()` to get an inner value in `Some(T)`.
+ *  - {@link isSome()} to check whether the value is `Some(T)`.
+ *  - {@link unwrapSome()} to get an inner value in `Some(T)`.
  *  - `unwrapOr()` to get either an inner value in `Some(T)` or a fallback default value.
  *  - ...and more.
  */
@@ -63,7 +65,7 @@ export interface Some<T> {
     /**
      *  Don't touch this property directly from an user project
      *  except 3rd party project that does not install this package but uses a value returned from an other project.
-     *  Instead, use `unwrap()` operator to get an inner value.
+     *  Instead, use {@link unwrapSome()} operator to get an inner value.
      *
      *  Historically, this type was created to target a JSVM that supports ES5.
      *  Then there was no well optimized `Symbol` to achieve a private property.
@@ -138,7 +140,7 @@ export interface None {
     /**
      *  Don't touch this property directly from an user project
      *  except 3rd party project that does not install this package but uses a value returned from an other project.
-     *  Instead, use `unwrap()` operator to get an inner value.
+     *  Instead, use {@link unwrapSome()} operator to get an inner value.
      *
      *  Historically, this type was created to target a JSVM that supports ES5.
      *  Then there was no well optimized `Symbol` to achieve a private property.
@@ -160,3 +162,28 @@ export function createNone(): None {
     };
     return r;
 }
+
+/**
+ *  Return the inner `T` of a `Some(T)`.
+ *
+ *  @throws {TypeError}
+ *      Throws if the self is a `None`.
+ */
+export function unwrapSome<T>(input: Option<T>): T {
+    return expectSome(input, ERR_MSG_UNWRAP_SOME_BUT_INPUT_IS_NONE);
+}
+
+/**
+ *  Return _input_ as `T` if the passed _input_ is `Some(T)`.
+ *  Otherwise, throw `TypeError` with the passed `msg`.
+ *
+ *  @throws {TypeError}
+ *      Throws if the self is a `None`.
+ */
+export function expectSome<T>(input: Option<T>, msg: string): T {
+    if (!input.ok) {
+        throw new TypeError(msg);
+    }
+
+    return input.val;
+}
diff --git a/src/PlainOption/expect.ts b/src/PlainOption/expect.ts
index edde93f5..785d189a 100644
--- a/src/PlainOption/expect.ts
+++ b/src/PlainOption/expect.ts
@@ -1,19 +1,21 @@
-import { Option } from './Option.js';
-
 /**
- *  Return _input_ as `T` if the passed _input_ is `Some(T)`.
- *  Otherwise, throw `TypeError` with the passed `msg`.
+ *  @deprecated
+ *  This module is kept for backward compatibility.
+ *  Please use `option-t/PlainOption/Option` directly.
  */
-export function expectSomeForOption<T>(input: Option<T>, msg: string): T | never {
-    if (!input.ok) {
-        throw new TypeError(msg);
-    }
+import { expectSome } from './Option.js';
 
-    return input.val;
-}
+/**
+ *  This is an alias for backward compatibility.
+ *  Please use {@link expectSome} instead.
+ *
+ *  @see {@link expectSome}
+ */
+export const expectSomeForOption: typeof expectSome = expectSome;
 
 /**
  *  @deprecated
- *  Use {@link expectSomeForOption}
+ *  This is an alias for backward compatibility.
+ *  Please use {@link expectSome} instead.
  */
-export const expectIsSome: typeof expectSomeForOption = expectSomeForOption;
+export const expectIsSome: typeof expectSome = expectSome;
diff --git a/src/PlainOption/index.ts b/src/PlainOption/index.ts
index 1711c27b..9dd0f4ef 100644
--- a/src/PlainOption/index.ts
+++ b/src/PlainOption/index.ts
@@ -24,13 +24,23 @@
  *  ```
  */
 
-export { Option, Some, None, createSome, createNone, isSome, isNone } from './Option.js';
+export {
+    Option,
+    Some,
+    None,
+    createSome,
+    createNone,
+    isSome,
+    isNone,
+    unwrapSome,
+    expectSome,
+} from './Option.js';
 
 export { andForOption as and } from './and.js';
 export { andThenForOption as andThen } from './andThen.js';
 export { andThenAsyncForOption as andThenAsync } from './andThenAsync.js';
 export { equalForOption as equal } from './equal.js';
-export { expectSomeForOption as expect, expectSomeForOption as expectSome } from './expect.js';
+export { expectIsSome as expect } from './expect.js';
 export { filterForOption as filter } from './filter.js';
 export { flattenForOption as flatten } from './flatten.js';
 export { inspectOption as inspect } from './inspect.js';
@@ -44,7 +54,7 @@ export { orForOption as or } from './or.js';
 export { orElseForOption as orElse } from './orElse.js';
 export { orElseAsyncForOption as orElseAsync } from './orElseAsync.js';
 export { transposeForOption as transpose } from './transpose.js';
-export { unwrapOption as unwrap, unwrapOption as unwrapSome } from './unwrap.js';
+export { unwrapSomeFromOption as unwrap } from './unwrap.js';
 export { unwrapOrFromOption as unwrapOr } from './unwrapOr.js';
 export { unwrapOrElseFromOption as unwrapOrElse } from './unwrapOrElse.js';
 export { unwrapOrElseAsyncFromOption as unwrapOrElseAsync } from './unwrapOrElseAsync.js';
diff --git a/src/PlainOption/internal/ErrorMessage.ts b/src/PlainOption/internal/ErrorMessage.ts
new file mode 100644
index 00000000..84ed5e98
--- /dev/null
+++ b/src/PlainOption/internal/ErrorMessage.ts
@@ -0,0 +1,5 @@
+import { ERR_MSG_CALLED_WITH } from '../../internal/ErrorMessage.js';
+
+const NONE_TYPE_STR = '`None`';
+
+export const ERR_MSG_UNWRAP_SOME_BUT_INPUT_IS_NONE = ERR_MSG_CALLED_WITH + NONE_TYPE_STR;
diff --git a/src/PlainOption/unwrap.ts b/src/PlainOption/unwrap.ts
index 3d7429c1..e891d330 100644
--- a/src/PlainOption/unwrap.ts
+++ b/src/PlainOption/unwrap.ts
@@ -1,14 +1,19 @@
-import { expectSomeForOption } from './expect.js';
-import { Option } from './Option.js';
+/**
+ *  @deprecated
+ *  This module is kept for backward compatibility.
+ *  Please use `option-t/PlainOption/Option` directly.
+ */
+import { unwrapSome } from './Option.js';
 
 /**
- *  Return the inner `T` of a `Some(T)`.
- *
- *  @throws {TypeError}
- *      Throws if the self is a `None`.
+ *  This is an alias for backward compatibility.
+ *  Please use {@link unwrapSome} instead.
  */
-export function unwrapOption<T>(input: Option<T>): T | never {
-    return expectSomeForOption(input, 'called with `None`');
-}
+export const unwrapOption: typeof unwrapSome = unwrapSome;
 
-export { unwrapOption as unwrapSomeFromOption };
+/**
+ *  @deprecated
+ *  This is an alias for backward compatibility.
+ *  Please use {@link unwrapSome} instead.
+ */
+export const unwrapSomeFromOption: typeof unwrapSome = unwrapSome;
diff --git a/src/PlainResult/Result.ts b/src/PlainResult/Result.ts
index 816c9efc..c88223fa 100644
--- a/src/PlainResult/Result.ts
+++ b/src/PlainResult/Result.ts
@@ -232,7 +232,7 @@ export function unwrapErr<E>(input: Result<unknown, E>): E {
  *  @throws {TypeError}
  *      Throws if the self is a `Err`.
  */
-export function expectOk<T, E>(input: Result<T, E>, msg: string): T | never {
+export function expectOk<T, E>(input: Result<T, E>, msg: string): T {
     if (!input.ok) {
         throw new TypeError(msg);
     }
@@ -247,7 +247,7 @@ export function expectOk<T, E>(input: Result<T, E>, msg: string): T | never {
  *  @throws {TypeError}
  *      Throws if the self is a `Ok`.
  */
-export function expectErr<T, E>(input: Result<T, E>, msg: string): E | never {
+export function expectErr<T, E>(input: Result<T, E>, msg: string): E {
     if (input.ok) {
         throw new TypeError(msg);
     }
diff --git a/tools/pkg_files.json b/tools/pkg_files.json
index be7eac40..7cd1aba9 100644
--- a/tools/pkg_files.json
+++ b/tools/pkg_files.json
@@ -119,6 +119,8 @@
     "cjs/Nullable/xor.cjs": null,
     "cjs/Option.d.cts": null,
     "cjs/Option.cjs": null,
+    "cjs/PlainOption/internal/ErrorMessage.d.cts": null,
+    "cjs/PlainOption/internal/ErrorMessage.cjs": null,
     "cjs/PlainOption/Option.d.cts": null,
     "cjs/PlainOption/Option.cjs": null,
     "cjs/PlainOption/and.d.cts": null,
@@ -421,6 +423,8 @@
     "esm/Nullable/xor.js": null,
     "esm/Option.d.ts": null,
     "esm/Option.js": null,
+    "esm/PlainOption/internal/ErrorMessage.d.ts": null,
+    "esm/PlainOption/internal/ErrorMessage.js": null,
     "esm/PlainOption/Option.d.ts": null,
     "esm/PlainOption/Option.js": null,
     "esm/PlainOption/and.d.ts": null,
@@ -657,6 +661,7 @@
     "lib/Nullable/unwrapOrElseAsync.d.ts": null,
     "lib/Nullable/xor.d.ts": null,
     "lib/Option.d.ts": null,
+    "lib/PlainOption/internal/ErrorMessage.d.ts": null,
     "lib/PlainOption/Option.d.ts": null,
     "lib/PlainOption/and.d.ts": null,
     "lib/PlainOption/andThen.d.ts": null,
diff --git a/tools/public_api/table.mjs b/tools/public_api/table.mjs
index cb76cbe2..a9bd8472 100644
--- a/tools/public_api/table.mjs
+++ b/tools/public_api/table.mjs
@@ -399,7 +399,9 @@ export const apiTable = Object.freeze({
             'isSome',
             'isNone',
             'createSome',
-            'createNone'
+            'createNone',
+            'unwrapSome',
+            'expectSome',
         ]
     },
     'PlainOption/or': {


Test patch:
diff --git a/__tests__/ClassicResult/compat.test.mjs b/__tests__/ClassicResult/compat.test.mjs
index 6878e04b..33b4d72f 100644
--- a/__tests__/ClassicResult/compat.test.mjs
+++ b/__tests__/ClassicResult/compat.test.mjs
@@ -1,13 +1,7 @@
 import test from 'ava';
 
-import {
-    createOk,
-    createErr,
-    isOk,
-    isErr,
-    unwrapOk as unwrapOkFromResult,
-    unwrapErr as unwrapErrFromResult,
-} from '../../__dist/esm/PlainResult/Result.js';
+import { createOk, createErr, isOk, isErr } from '../../__dist/esm/PlainResult/Result.js';
+import { unwrapOkFromResult, unwrapErrFromResult } from '../../__dist/esm/PlainResult/unwrap.js';
 import {
     createClassicOk,
     createClassicErr,
diff --git a/__tests__/PlainOption/ok_or.test.mjs b/__tests__/PlainOption/ok_or.test.mjs
index f911003c..e9b32ac1 100644
--- a/__tests__/PlainOption/ok_or.test.mjs
+++ b/__tests__/PlainOption/ok_or.test.mjs
@@ -2,10 +2,7 @@ import test from 'ava';
 
 import { createSome, createNone } from '../../__dist/esm/PlainOption/Option.js';
 import { okOrForPlainOption } from '../../__dist/esm/PlainOption/okOr.js';
-import {
-    unwrapOk as unwrapOkFromResult,
-    unwrapErr as unwrapErrFromResult,
-} from '../../__dist/esm/PlainResult/Result.js';
+import { unwrapErrFromResult, unwrapOkFromResult } from '../../__dist/esm/PlainResult/unwrap.js';
 
 test('the input is Some', (t) => {
     const OK_VAL = Symbol('ok');
diff --git a/__tests__/PlainOption/ok_or_else.test.mjs b/__tests__/PlainOption/ok_or_else.test.mjs
index ced1453e..6a7fb767 100644
--- a/__tests__/PlainOption/ok_or_else.test.mjs
+++ b/__tests__/PlainOption/ok_or_else.test.mjs
@@ -2,10 +2,7 @@ import test from 'ava';
 
 import { createSome, createNone } from '../../__dist/esm/PlainOption/Option.js';
 import { okOrElseForPlainOption } from '../../__dist/esm/PlainOption/okOrElse.js';
-import {
-    unwrapOk as unwrapOkFromResult,
-    unwrapErr as unwrapErrFromResult,
-} from '../../__dist/esm/PlainResult/Result.js';
+import { unwrapErrFromResult, unwrapOkFromResult } from '../../__dist/esm/PlainResult/unwrap.js';
 
 test('the input is Some', (t) => {
     t.plan(2);
diff --git a/__tests__/PlainResult/equal.test.mjs b/__tests__/PlainResult/equal.test.mjs
index 02ae78a6..9f52e5fe 100644
--- a/__tests__/PlainResult/equal.test.mjs
+++ b/__tests__/PlainResult/equal.test.mjs
@@ -1,14 +1,8 @@
 import test from 'ava';
 
-import {
-    createOk,
-    createErr,
-    isOk,
-    isErr,
-    unwrapOk as unwrapOkFromResult,
-    unwrapErr as unwrapErrFromResult,
-} from '../../__dist/esm/PlainResult/Result.js';
+import { createOk, createErr, isOk, isErr } from '../../__dist/esm/PlainResult/Result.js';
 import { equalForResult } from '../../__dist/esm/PlainResult/equal.js';
+import { unwrapOkFromResult, unwrapErrFromResult } from '../../__dist/esm/PlainResult/unwrap.js';
 
 const INNER_VALUE_A = Symbol('A');
 const INNER_VALUE_B = Symbol('B');
diff --git a/__tests__/PlainResult/expect/test_expect_err.test.mjs b/__tests__/PlainResult/expect/test_expect_err.test.mjs
index d4689de9..a9e69706 100644
--- a/__tests__/PlainResult/expect/test_expect_err.test.mjs
+++ b/__tests__/PlainResult/expect/test_expect_err.test.mjs
@@ -1,10 +1,7 @@
 import test from 'ava';
 
-import {
-    createOk,
-    createErr,
-    expectErr as expectErrForResult,
-} from '../../../__dist/esm/PlainResult/Result.js';
+import { createOk, createErr } from '../../../__dist/esm/PlainResult/Result.js';
+import { expectErrForResult } from '../../../__dist/esm/PlainResult/expect.js';
 
 test('input=Ok(T), expect=Err(E)', (t) => {
     const NOT_EXPECTED = Symbol('not expected');
diff --git a/__tests__/PlainResult/expect/test_expect_ok.test.mjs b/__tests__/PlainResult/expect/test_expect_ok.test.mjs
index d2fd6b23..9a36c019 100644
--- a/__tests__/PlainResult/expect/test_expect_ok.test.mjs
+++ b/__tests__/PlainResult/expect/test_expect_ok.test.mjs
@@ -1,10 +1,7 @@
 import test from 'ava';
 
-import {
-    createOk,
-    createErr,
-    expectOk as expectOkForResult,
-} from '../../../__dist/esm/PlainResult/Result.js';
+import { createOk, createErr } from '../../../__dist/esm/PlainResult/Result.js';
+import { expectOkForResult } from '../../../__dist/esm/PlainResult/expect.js';
 
 test('input=Ok(T), expect=Ok(T)', (t) => {
     const EXPECTED = Symbol('expected');
diff --git a/__tests__/PlainResult/from_promise_settled_result.test.mjs b/__tests__/PlainResult/from_promise_settled_result.test.mjs
index e3e498fc..f1d14979 100644
--- a/__tests__/PlainResult/from_promise_settled_result.test.mjs
+++ b/__tests__/PlainResult/from_promise_settled_result.test.mjs
@@ -1,12 +1,8 @@
 import test from 'ava';
 
-import {
-    isOk,
-    isErr,
-    unwrapOk as unwrapOkFromResult,
-    unwrapErr as unwrapErrFromResult,
-} from '../../__dist/esm/PlainResult/Result.js';
+import { isOk, isErr } from '../../__dist/esm/PlainResult/Result.js';
 import { fromPromiseSettledResultToResult } from '../../__dist/esm/PlainResult/fromPromiseSettledResult.js';
+import { unwrapOkFromResult, unwrapErrFromResult } from '../../__dist/esm/PlainResult/unwrap.js';
 
 test('should be Ok(T)', async (t) => {
     const VALUE_T = Symbol('value');
diff --git a/__tests__/PlainResult/try_catch/try_catch.test.mjs b/__tests__/PlainResult/try_catch/try_catch.test.mjs
index a221815a..3f7e6a8c 100644
--- a/__tests__/PlainResult/try_catch/try_catch.test.mjs
+++ b/__tests__/PlainResult/try_catch/try_catch.test.mjs
@@ -1,12 +1,8 @@
 import test from 'ava';
 
-import {
-    isOk,
-    isErr,
-    unwrapOk as unwrapOkFromResult,
-    unwrapErr as unwrapErrFromResult,
-} from '../../../__dist/esm/PlainResult/Result.js';
+import { isOk, isErr } from '../../../__dist/esm/PlainResult/Result.js';
 import { tryCatchIntoResult } from '../../../__dist/esm/PlainResult/tryCatch.js';
+import { unwrapErrFromResult, unwrapOkFromResult } from '../../../__dist/esm/PlainResult/unwrap.js';
 
 test('output=Ok(T)', (t) => {
     t.plan(3);
diff --git a/__tests__/PlainResult/try_catch/try_catch_with_ensure_error.test.mjs b/__tests__/PlainResult/try_catch/try_catch_with_ensure_error.test.mjs
index 2fc9122f..b7549c87 100644
--- a/__tests__/PlainResult/try_catch/try_catch_with_ensure_error.test.mjs
+++ b/__tests__/PlainResult/try_catch/try_catch_with_ensure_error.test.mjs
@@ -1,13 +1,9 @@
 import test from 'ava';
 import { webcrypto } from 'node:crypto';
 
-import {
-    isOk,
-    isErr,
-    unwrapOk as unwrapOkFromResult,
-    unwrapErr as unwrapErrFromResult,
-} from '../../../__dist/esm/PlainResult/Result.js';
+import { isOk, isErr } from '../../../__dist/esm/PlainResult/Result.js';
 import { tryCatchIntoResultWithEnsureError } from '../../../__dist/esm/PlainResult/tryCatch.js';
+import { unwrapErrFromResult, unwrapOkFromResult } from '../../../__dist/esm/PlainResult/unwrap.js';
 
 test('output=Ok(T)', (t) => {
     t.plan(3);
diff --git a/__tests__/PlainResult/try_catch_async/try_catch_async.test.mjs b/__tests__/PlainResult/try_catch_async/try_catch_async.test.mjs
index 1e294613..2e496c9f 100644
--- a/__tests__/PlainResult/try_catch_async/try_catch_async.test.mjs
+++ b/__tests__/PlainResult/try_catch_async/try_catch_async.test.mjs
@@ -1,12 +1,8 @@
 import test from 'ava';
 
-import {
-    isOk,
-    isErr,
-    unwrapOk as unwrapOkFromResult,
-    unwrapErr as unwrapErrFromResult,
-} from '../../../__dist/esm/PlainResult/Result.js';
+import { isOk, isErr } from '../../../__dist/esm/PlainResult/Result.js';
 import { tryCatchIntoResultAsync } from '../../../__dist/esm/PlainResult/tryCatchAsync.js';
+import { unwrapErrFromResult, unwrapOkFromResult } from '../../../__dist/esm/PlainResult/unwrap.js';
 
 test('output=Ok(T): producer is async fn', async (t) => {
     t.plan(4);
diff --git a/__tests__/PlainResult/try_catch_async/try_catch_with_ensure_error_async.test.mjs b/__tests__/PlainResult/try_catch_async/try_catch_with_ensure_error_async.test.mjs
index be13ceee..3abeabb5 100644
--- a/__tests__/PlainResult/try_catch_async/try_catch_with_ensure_error_async.test.mjs
+++ b/__tests__/PlainResult/try_catch_async/try_catch_with_ensure_error_async.test.mjs
@@ -1,12 +1,8 @@
 import test from 'ava';
 
-import {
-    isOk,
-    isErr,
-    unwrapOk as unwrapOkFromResult,
-    unwrapErr as unwrapErrFromResult,
-} from '../../../__dist/esm/PlainResult/Result.js';
+import { isOk, isErr } from '../../../__dist/esm/PlainResult/Result.js';
 import { tryCatchIntoResultWithEnsureErrorAsync } from '../../../__dist/esm/PlainResult/tryCatchAsync.js';
+import { unwrapErrFromResult, unwrapOkFromResult } from '../../../__dist/esm/PlainResult/unwrap.js';
 
 test('output=Ok(T): producer is async fn', async (t) => {
     t.plan(4);
diff --git a/__tests__/PlainResult/unwrap/unwrap_err.test.mjs b/__tests__/PlainResult/unwrap/unwrap_err.test.mjs
index d8e0738a..45b46a5b 100644
--- a/__tests__/PlainResult/unwrap/unwrap_err.test.mjs
+++ b/__tests__/PlainResult/unwrap/unwrap_err.test.mjs
@@ -1,10 +1,7 @@
 import test from 'ava';
 
-import {
-    createOk,
-    createErr,
-    unwrapErr as unwrapErrFromResult,
-} from '../../../__dist/esm/PlainResult/Result.js';
+import { createOk, createErr } from '../../../__dist/esm/PlainResult/Result.js';
+import { unwrapErrFromResult } from '../../../__dist/esm/PlainResult/unwrap.js';
 
 const EXPECTED_OK = Symbol('expected_ok');
 const EXPECTED_ERR = Symbol('expected_err');
diff --git a/__tests__/PlainResult/unwrap/unwrap_ok.test.mjs b/__tests__/PlainResult/unwrap/unwrap_ok.test.mjs
index 77e8b0a1..6ec8d5e2 100644
--- a/__tests__/PlainResult/unwrap/unwrap_ok.test.mjs
+++ b/__tests__/PlainResult/unwrap/unwrap_ok.test.mjs
@@ -1,10 +1,7 @@
 import test from 'ava';
 
-import {
-    createOk,
-    createErr,
-    unwrapOk as unwrapOkFromResult,
-} from '../../../__dist/esm/PlainResult/Result.js';
+import { createOk, createErr } from '../../../__dist/esm/PlainResult/Result.js';
+import { unwrapOkFromResult } from '../../../__dist/esm/PlainResult/unwrap.js';
 
 const EXPECTED_OK = Symbol('expected_ok');
 const EXPECTED_ERR = Symbol('expected_err');