Repository: karen-irc/option-t
Pull request #1327 (2022-06-18 17:25:20)

Implement `_toOtherType()` for `Undefinable`, `Nullable`, and `Maybe`


Patch:
diff --git a/docs/public_api_list.md b/docs/public_api_list.md
index 2cef3c64..a27272dc 100644
--- a/docs/public_api_list.md
+++ b/docs/public_api_list.md
@@ -28,6 +28,8 @@
 - [option-t/Maybe/orElse](../src/Maybe/orElse.ts)
 - [option-t/Maybe/orElseAsync](../src/Maybe/orElseAsync.ts)
 - [option-t/Maybe/tap](../src/Maybe/tap.ts)
+- [option-t/Maybe/toNullable](../src/Maybe/toNullable.ts)
+- [option-t/Maybe/toUndefinable](../src/Maybe/toUndefinable.ts)
 - [option-t/Maybe/unwrap](../src/Maybe/unwrap.ts)
 - [option-t/Maybe/unwrapOr](../src/Maybe/unwrapOr.ts)
 - [option-t/Maybe/unwrapOrElse](../src/Maybe/unwrapOrElse.ts)
@@ -54,6 +56,7 @@
 - [option-t/Nullable/orElse](../src/Nullable/orElse.ts)
 - [option-t/Nullable/orElseAsync](../src/Nullable/orElseAsync.ts)
 - [option-t/Nullable/tap](../src/Nullable/tap.ts)
+- [option-t/Nullable/toUndefinable](../src/Nullable/toUndefinable.ts)
 - [option-t/Nullable/unwrap](../src/Nullable/unwrap.ts)
 - [option-t/Nullable/unwrapOr](../src/Nullable/unwrapOr.ts)
 - [option-t/Nullable/unwrapOrElse](../src/Nullable/unwrapOrElse.ts)
@@ -148,6 +151,7 @@
 - [option-t/Undefinable/orElse](../src/Undefinable/orElse.ts)
 - [option-t/Undefinable/orElseAsync](../src/Undefinable/orElseAsync.ts)
 - [option-t/Undefinable/tap](../src/Undefinable/tap.ts)
+- [option-t/Undefinable/toNullable](../src/Undefinable/toNullable.ts)
 - [option-t/Undefinable/Undefinable](../src/Undefinable/Undefinable.ts)
 - [option-t/Undefinable/unwrap](../src/Undefinable/unwrap.ts)
 - [option-t/Undefinable/unwrapOr](../src/Undefinable/unwrapOr.ts)
diff --git a/src/Maybe/toNullable.ts b/src/Maybe/toNullable.ts
new file mode 100644
index 00000000..3fc0708f
--- /dev/null
+++ b/src/Maybe/toNullable.ts
@@ -0,0 +1,10 @@
+import type { Nullable } from '../Nullable';
+import { type Maybe, isNullOrUndefined } from './Maybe';
+
+export function toNullableFromMaybe<T>(input: Maybe<T>): Nullable<T> {
+    if (isNullOrUndefined(input)) {
+        return null;
+    }
+
+    return input;
+}
diff --git a/src/Maybe/toUndefinable.ts b/src/Maybe/toUndefinable.ts
new file mode 100644
index 00000000..3f0313d4
--- /dev/null
+++ b/src/Maybe/toUndefinable.ts
@@ -0,0 +1,10 @@
+import type { Undefinable } from '../Undefinable';
+import { type Maybe, isNullOrUndefined } from './Maybe';
+
+export function toUndefinableFromMaybe<T>(input: Maybe<T>): Undefinable<T> {
+    if (isNullOrUndefined(input)) {
+        return undefined;
+    }
+
+    return input;
+}
diff --git a/src/Nullable/toUndefinable.ts b/src/Nullable/toUndefinable.ts
new file mode 100644
index 00000000..585b02e0
--- /dev/null
+++ b/src/Nullable/toUndefinable.ts
@@ -0,0 +1,10 @@
+import type { Undefinable } from '../Undefinable';
+import { type Nullable, isNull } from './Nullable';
+
+export function toUndefinableFromNullable<T>(input: Nullable<T>): Undefinable<T> {
+    if (isNull(input)) {
+        return undefined;
+    }
+
+    return input;
+}
diff --git a/src/Undefinable/toNullable.ts b/src/Undefinable/toNullable.ts
new file mode 100644
index 00000000..7730a17c
--- /dev/null
+++ b/src/Undefinable/toNullable.ts
@@ -0,0 +1,10 @@
+import type { Nullable } from '../Nullable';
+import { isUndefined, type Undefinable } from './Undefinable';
+
+export function toNullableFromUndefinable<T>(input: Undefinable<T>): Nullable<T> {
+    if (isUndefined(input)) {
+        return null;
+    }
+
+    return input;
+}
diff --git a/tools/pkg_files.json b/tools/pkg_files.json
index 6dd63828..3a2aea48 100644
--- a/tools/pkg_files.json
+++ b/tools/pkg_files.json
@@ -55,6 +55,10 @@
     "cjs/Maybe/orElseAsync.js": null,
     "cjs/Maybe/tap.d.ts": null,
     "cjs/Maybe/tap.js": null,
+    "cjs/Maybe/toNullable.d.ts": null,
+    "cjs/Maybe/toNullable.js": null,
+    "cjs/Maybe/toUndefinable.d.ts": null,
+    "cjs/Maybe/toUndefinable.js": null,
     "cjs/Maybe/unwrap.d.ts": null,
     "cjs/Maybe/unwrap.js": null,
     "cjs/Maybe/unwrapOr.d.ts": null,
@@ -101,6 +105,8 @@
     "cjs/Nullable/orElseAsync.js": null,
     "cjs/Nullable/tap.d.ts": null,
     "cjs/Nullable/tap.js": null,
+    "cjs/Nullable/toUndefinable.d.ts": null,
+    "cjs/Nullable/toUndefinable.js": null,
     "cjs/Nullable/unwrap.d.ts": null,
     "cjs/Nullable/unwrap.js": null,
     "cjs/Nullable/unwrapOr.d.ts": null,
@@ -273,6 +279,8 @@
     "cjs/Undefinable/orElseAsync.js": null,
     "cjs/Undefinable/tap.d.ts": null,
     "cjs/Undefinable/tap.js": null,
+    "cjs/Undefinable/toNullable.d.ts": null,
+    "cjs/Undefinable/toNullable.js": null,
     "cjs/Undefinable/unwrap.d.ts": null,
     "cjs/Undefinable/unwrap.js": null,
     "cjs/Undefinable/unwrapOr.d.ts": null,
@@ -344,6 +352,10 @@
     "esm/Maybe/orElseAsync.mjs": null,
     "esm/Maybe/tap.d.ts": null,
     "esm/Maybe/tap.mjs": null,
+    "esm/Maybe/toNullable.d.ts": null,
+    "esm/Maybe/toNullable.mjs": null,
+    "esm/Maybe/toUndefinable.d.ts": null,
+    "esm/Maybe/toUndefinable.mjs": null,
     "esm/Maybe/unwrap.d.ts": null,
     "esm/Maybe/unwrap.mjs": null,
     "esm/Maybe/unwrapOr.d.ts": null,
@@ -390,6 +402,8 @@
     "esm/Nullable/orElseAsync.mjs": null,
     "esm/Nullable/tap.d.ts": null,
     "esm/Nullable/tap.mjs": null,
+    "esm/Nullable/toUndefinable.d.ts": null,
+    "esm/Nullable/toUndefinable.mjs": null,
     "esm/Nullable/unwrap.d.ts": null,
     "esm/Nullable/unwrap.mjs": null,
     "esm/Nullable/unwrapOr.d.ts": null,
@@ -562,6 +576,8 @@
     "esm/Undefinable/orElseAsync.mjs": null,
     "esm/Undefinable/tap.d.ts": null,
     "esm/Undefinable/tap.mjs": null,
+    "esm/Undefinable/toNullable.d.ts": null,
+    "esm/Undefinable/toNullable.mjs": null,
     "esm/Undefinable/unwrap.d.ts": null,
     "esm/Undefinable/unwrap.mjs": null,
     "esm/Undefinable/unwrapOr.d.ts": null,
@@ -600,6 +616,8 @@
     "lib/Maybe/orElse.d.ts": null,
     "lib/Maybe/orElseAsync.d.ts": null,
     "lib/Maybe/tap.d.ts": null,
+    "lib/Maybe/toNullable.d.ts": null,
+    "lib/Maybe/toUndefinable.d.ts": null,
     "lib/Maybe/unwrap.d.ts": null,
     "lib/Maybe/unwrapOr.d.ts": null,
     "lib/Maybe/unwrapOrElse.d.ts": null,
@@ -623,6 +641,7 @@
     "lib/Nullable/orElse.d.ts": null,
     "lib/Nullable/orElseAsync.d.ts": null,
     "lib/Nullable/tap.d.ts": null,
+    "lib/Nullable/toUndefinable.d.ts": null,
     "lib/Nullable/unwrap.d.ts": null,
     "lib/Nullable/unwrapOr.d.ts": null,
     "lib/Nullable/unwrapOrElse.d.ts": null,
@@ -709,6 +728,7 @@
     "lib/Undefinable/orElse.d.ts": null,
     "lib/Undefinable/orElseAsync.d.ts": null,
     "lib/Undefinable/tap.d.ts": null,
+    "lib/Undefinable/toNullable.d.ts": null,
     "lib/Undefinable/unwrap.d.ts": null,
     "lib/Undefinable/unwrapOr.d.ts": null,
     "lib/Undefinable/unwrapOrElse.d.ts": null,
diff --git a/tools/public_api/table.mjs b/tools/public_api/table.mjs
index 074d3341..e401f83d 100644
--- a/tools/public_api/table.mjs
+++ b/tools/public_api/table.mjs
@@ -109,6 +109,16 @@ export const apiTable = Object.freeze({
             'tapMaybe'
         ]
     },
+    'Maybe/toNullable': {
+        'exports': [
+            'toNullableFromMaybe'
+        ]
+    },
+    'Maybe/toUndefinable': {
+        'exports': [
+            'toUndefinableFromMaybe'
+        ]
+    },
     'Maybe/unwrap': {
         'exports': [
             'unwrapMaybe'
@@ -240,6 +250,11 @@ export const apiTable = Object.freeze({
             'tapNullable'
         ]
     },
+    'Nullable/toUndefinable': {
+        'exports': [
+            'toUndefinableFromNullable'
+        ]
+    },
     'Nullable/unwrap': {
         'exports': [
             'unwrapNullable'
@@ -767,6 +782,11 @@ export const apiTable = Object.freeze({
             'tapUndefinable'
         ]
     },
+    'Undefinable/toNullable': {
+        'exports': [
+            'toNullableFromUndefinable'
+        ]
+    },
     'Undefinable/Undefinable': {
         'exports': [
             'isNotUndefined',
@@ -838,6 +858,8 @@ export const legacyApiTable = Object.freeze({
     'cjs/Maybe/orElse': COMPAT_DESCRIPTOR,
     'cjs/Maybe/orElseAsync': COMPAT_DESCRIPTOR,
     'cjs/Maybe/tap': COMPAT_DESCRIPTOR,
+    'cjs/Maybe/toNullable': COMPAT_DESCRIPTOR,
+    'cjs/Maybe/toUndefinable': COMPAT_DESCRIPTOR,
     'cjs/Maybe/unwrap': COMPAT_DESCRIPTOR,
     'cjs/Maybe/unwrapOr': COMPAT_DESCRIPTOR,
     'cjs/Maybe/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -862,6 +884,7 @@ export const legacyApiTable = Object.freeze({
     'cjs/Nullable/orElse': COMPAT_DESCRIPTOR,
     'cjs/Nullable/orElseAsync': COMPAT_DESCRIPTOR,
     'cjs/Nullable/tap': COMPAT_DESCRIPTOR,
+    'cjs/Nullable/toUndefinable': COMPAT_DESCRIPTOR,
     'cjs/Nullable/unwrap': COMPAT_DESCRIPTOR,
     'cjs/Nullable/unwrapOr': COMPAT_DESCRIPTOR,
     'cjs/Nullable/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -951,6 +974,7 @@ export const legacyApiTable = Object.freeze({
     'cjs/Undefinable/orElse': COMPAT_DESCRIPTOR,
     'cjs/Undefinable/orElseAsync': COMPAT_DESCRIPTOR,
     'cjs/Undefinable/tap': COMPAT_DESCRIPTOR,
+    'cjs/Undefinable/toNullable': COMPAT_DESCRIPTOR,
     'cjs/Undefinable/unwrap': COMPAT_DESCRIPTOR,
     'cjs/Undefinable/unwrapOr': COMPAT_DESCRIPTOR,
     'cjs/Undefinable/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -976,6 +1000,8 @@ export const legacyApiTable = Object.freeze({
     'esm/Maybe/orElse': COMPAT_DESCRIPTOR,
     'esm/Maybe/orElseAsync': COMPAT_DESCRIPTOR,
     'esm/Maybe/tap': COMPAT_DESCRIPTOR,
+    'esm/Maybe/toNullable': COMPAT_DESCRIPTOR,
+    'esm/Maybe/toUndefinable': COMPAT_DESCRIPTOR,
     'esm/Maybe/unwrap': COMPAT_DESCRIPTOR,
     'esm/Maybe/unwrapOr': COMPAT_DESCRIPTOR,
     'esm/Maybe/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -1000,6 +1026,7 @@ export const legacyApiTable = Object.freeze({
     'esm/Nullable/orElse': COMPAT_DESCRIPTOR,
     'esm/Nullable/orElseAsync': COMPAT_DESCRIPTOR,
     'esm/Nullable/tap': COMPAT_DESCRIPTOR,
+    'esm/Nullable/toUndefinable': COMPAT_DESCRIPTOR,
     'esm/Nullable/unwrap': COMPAT_DESCRIPTOR,
     'esm/Nullable/unwrapOr': COMPAT_DESCRIPTOR,
     'esm/Nullable/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -1089,6 +1116,7 @@ export const legacyApiTable = Object.freeze({
     'esm/Undefinable/orElse': COMPAT_DESCRIPTOR,
     'esm/Undefinable/orElseAsync': COMPAT_DESCRIPTOR,
     'esm/Undefinable/tap': COMPAT_DESCRIPTOR,
+    'esm/Undefinable/toNullable': COMPAT_DESCRIPTOR,
     'esm/Undefinable/unwrap': COMPAT_DESCRIPTOR,
     'esm/Undefinable/unwrapOr': COMPAT_DESCRIPTOR,
     'esm/Undefinable/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -1114,6 +1142,8 @@ export const legacyApiTable = Object.freeze({
     'lib/Maybe/orElse': COMPAT_DESCRIPTOR,
     'lib/Maybe/orElseAsync': COMPAT_DESCRIPTOR,
     'lib/Maybe/tap': COMPAT_DESCRIPTOR,
+    'lib/Maybe/toNullable': COMPAT_DESCRIPTOR,
+    'lib/Maybe/toUndefinable': COMPAT_DESCRIPTOR,
     'lib/Maybe/unwrap': COMPAT_DESCRIPTOR,
     'lib/Maybe/unwrapOr': COMPAT_DESCRIPTOR,
     'lib/Maybe/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -1138,6 +1168,7 @@ export const legacyApiTable = Object.freeze({
     'lib/Nullable/orElse': COMPAT_DESCRIPTOR,
     'lib/Nullable/orElseAsync': COMPAT_DESCRIPTOR,
     'lib/Nullable/tap': COMPAT_DESCRIPTOR,
+    'lib/Nullable/toUndefinable': COMPAT_DESCRIPTOR,
     'lib/Nullable/unwrap': COMPAT_DESCRIPTOR,
     'lib/Nullable/unwrapOr': COMPAT_DESCRIPTOR,
     'lib/Nullable/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -1227,6 +1258,7 @@ export const legacyApiTable = Object.freeze({
     'lib/Undefinable/orElse': COMPAT_DESCRIPTOR,
     'lib/Undefinable/orElseAsync': COMPAT_DESCRIPTOR,
     'lib/Undefinable/tap': COMPAT_DESCRIPTOR,
+    'lib/Undefinable/toNullable': COMPAT_DESCRIPTOR,
     'lib/Undefinable/unwrap': COMPAT_DESCRIPTOR,
     'lib/Undefinable/unwrapOr': COMPAT_DESCRIPTOR,
     'lib/Undefinable/unwrapOrElse': COMPAT_DESCRIPTOR,


Test patch:
diff --git a/__tests__/Maybe/to_nullable.test.mjs b/__tests__/Maybe/to_nullable.test.mjs
new file mode 100644
index 00000000..73140020
--- /dev/null
+++ b/__tests__/Maybe/to_nullable.test.mjs
@@ -0,0 +1,18 @@
+import test from 'ava';
+
+import { toNullableFromMaybe } from '../../__dist/esm/Maybe/toNullable.mjs';
+import { nonNullableValueCaseListForSync } from '../utils.mjs';
+
+for (const [INPUT, , EXPECTED] of nonNullableValueCaseListForSync) {
+    test('pass the value' + String(INPUT), (t) => {
+        const actual = toNullableFromMaybe(INPUT);
+        t.is(actual, EXPECTED, 'should the expected result');
+    });
+}
+
+for (const NULL_VALUE of [undefined, null]) {
+    test(`pass ${NULL_VALUE}`, (t) => {
+        const actual = toNullableFromMaybe(NULL_VALUE);
+        t.is(actual, null);
+    });
+}
diff --git a/__tests__/Maybe/to_undefinable.test.mjs b/__tests__/Maybe/to_undefinable.test.mjs
new file mode 100644
index 00000000..a6431740
--- /dev/null
+++ b/__tests__/Maybe/to_undefinable.test.mjs
@@ -0,0 +1,18 @@
+import test from 'ava';
+
+import { toUndefinableFromMaybe } from '../../__dist/esm/Maybe/toUndefinable.mjs';
+import { nonNullableValueCaseListForSync } from '../utils.mjs';
+
+for (const [INPUT, , EXPECTED] of nonNullableValueCaseListForSync) {
+    test('pass the value' + String(INPUT), (t) => {
+        const actual = toUndefinableFromMaybe(INPUT);
+        t.is(actual, EXPECTED, 'should the expected result');
+    });
+}
+
+for (const NULL_VALUE of [undefined, null]) {
+    test(`pass ${NULL_VALUE}`, (t) => {
+        const actual = toUndefinableFromMaybe(NULL_VALUE);
+        t.is(actual, undefined);
+    });
+}
diff --git a/__tests__/Nullable/to_undefinable.test.mjs b/__tests__/Nullable/to_undefinable.test.mjs
new file mode 100644
index 00000000..5462a5e4
--- /dev/null
+++ b/__tests__/Nullable/to_undefinable.test.mjs
@@ -0,0 +1,18 @@
+import test from 'ava';
+
+import { toUndefinableFromNullable } from '../../__dist/esm/Nullable/toUndefinable.mjs';
+import { nonNullableValueCaseListForSync } from '../utils.mjs';
+
+for (const [INPUT, , EXPECTED] of nonNullableValueCaseListForSync) {
+    test('pass the value' + String(INPUT), (t) => {
+        const actual = toUndefinableFromNullable(INPUT);
+        t.is(actual, EXPECTED, 'should the expected result');
+    });
+}
+
+for (const NULL_VALUE of [undefined, null]) {
+    test(`pass ${NULL_VALUE}`, (t) => {
+        const actual = toUndefinableFromNullable(NULL_VALUE);
+        t.is(actual, undefined);
+    });
+}
diff --git a/__tests__/Undefinable/to_nullable.test.mjs b/__tests__/Undefinable/to_nullable.test.mjs
new file mode 100644
index 00000000..dd397e61
--- /dev/null
+++ b/__tests__/Undefinable/to_nullable.test.mjs
@@ -0,0 +1,18 @@
+import test from 'ava';
+
+import { toNullableFromUndefinable } from '../../__dist/esm/Undefinable/toNullable.mjs';
+import { nonNullableValueCaseListForSync } from '../utils.mjs';
+
+for (const [INPUT, , EXPECTED] of nonNullableValueCaseListForSync) {
+    test('pass the value' + String(INPUT), (t) => {
+        const actual = toNullableFromUndefinable(INPUT);
+        t.is(actual, EXPECTED, 'should the expected result');
+    });
+}
+
+for (const NULL_VALUE of [undefined, null]) {
+    test(`pass ${NULL_VALUE}`, (t) => {
+        const actual = toNullableFromUndefinable(NULL_VALUE);
+        t.is(actual, null);
+    });
+}