Repository: karen-irc/option-t
Pull request #1333 (2022-06-18 18:56:01)

Implement `toNullable()` & `toUndefinable()` for `PlainOption`


Patch:
diff --git a/src/PlainOption/toNullable.ts b/src/PlainOption/toNullable.ts
new file mode 100644
index 00000000..7623cbbb
--- /dev/null
+++ b/src/PlainOption/toNullable.ts
@@ -0,0 +1,10 @@
+import type { Nullable } from '../Nullable/Nullable';
+import { type Option } from './Option';
+import { mapOrForOption } from './mapOr';
+
+export function toNullableFromOption<T>(input: Option<T>): Nullable<T> {
+    const rv = mapOrForOption<T, Nullable<T>>(input, null, (inner: T) => {
+        return inner;
+    });
+    return rv;
+}
diff --git a/src/PlainOption/toUndefinable.ts b/src/PlainOption/toUndefinable.ts
new file mode 100644
index 00000000..4490b23a
--- /dev/null
+++ b/src/PlainOption/toUndefinable.ts
@@ -0,0 +1,10 @@
+import type { Undefinable } from '../Undefinable/Undefinable';
+import { type Option } from './Option';
+import { mapOrForOption } from './mapOr';
+
+export function toUndefinableFromOption<T>(input: Option<T>): Undefinable<T> {
+    const rv = mapOrForOption<T, Undefinable<T>>(input, undefined, (inner) => {
+        return inner;
+    });
+    return rv;
+}
diff --git a/tools/pkg_files.json b/tools/pkg_files.json
index 3a2aea48..c6d86688 100644
--- a/tools/pkg_files.json
+++ b/tools/pkg_files.json
@@ -169,6 +169,10 @@
     "cjs/PlainOption/transpose.js": null,
     "cjs/PlainOption/tap.d.ts": null,
     "cjs/PlainOption/tap.js": null,
+    "cjs/PlainOption/toNullable.d.ts": null,
+    "cjs/PlainOption/toNullable.js": null,
+    "cjs/PlainOption/toUndefinable.d.ts": null,
+    "cjs/PlainOption/toUndefinable.js": null,
     "cjs/PlainOption/unwrap.d.ts": null,
     "cjs/PlainOption/unwrap.js": null,
     "cjs/PlainOption/unwrapOr.d.ts": null,
@@ -466,6 +470,10 @@
     "esm/PlainOption/transpose.mjs": null,
     "esm/PlainOption/tap.d.ts": null,
     "esm/PlainOption/tap.mjs": null,
+    "esm/PlainOption/toNullable.d.ts": null,
+    "esm/PlainOption/toNullable.mjs": null,
+    "esm/PlainOption/toUndefinable.d.ts": null,
+    "esm/PlainOption/toUndefinable.mjs": null,
     "esm/PlainOption/unwrap.d.ts": null,
     "esm/PlainOption/unwrap.mjs": null,
     "esm/PlainOption/unwrapOr.d.ts": null,
@@ -673,6 +681,8 @@
     "lib/PlainOption/orElseAsync.d.ts": null,
     "lib/PlainOption/transpose.d.ts": null,
     "lib/PlainOption/tap.d.ts": null,
+    "lib/PlainOption/toNullable.d.ts": null,
+    "lib/PlainOption/toUndefinable.d.ts": null,
     "lib/PlainOption/unwrap.d.ts": null,
     "lib/PlainOption/unwrapOr.d.ts": null,
     "lib/PlainOption/unwrapOrElse.d.ts": null,
diff --git a/tools/public_api/table.mjs b/tools/public_api/table.mjs
index e401f83d..6a651d88 100644
--- a/tools/public_api/table.mjs
+++ b/tools/public_api/table.mjs
@@ -440,6 +440,16 @@ export const apiTable = Object.freeze({
             'tapOption'
         ]
     },
+    'PlainOption/toNullable': {
+        'exports': [
+            'toNullableFromOption'
+        ]
+    },
+    'PlainOption/toUndefinable': {
+        'exports': [
+            'toUndefinableFromOption'
+        ]
+    },
     'PlainOption/unwrap': {
         'exports': [
             'unwrapOption',
@@ -917,6 +927,8 @@ export const legacyApiTable = Object.freeze({
     'cjs/PlainOption/orElseAsync': COMPAT_DESCRIPTOR,
     'cjs/PlainOption/transpose': COMPAT_DESCRIPTOR,
     'cjs/PlainOption/tap': COMPAT_DESCRIPTOR,
+    'cjs/PlainOption/toNullable': COMPAT_DESCRIPTOR,
+    'cjs/PlainOption/toUndefinable': COMPAT_DESCRIPTOR,
     'cjs/PlainOption/unwrap': COMPAT_DESCRIPTOR,
     'cjs/PlainOption/unwrapOr': COMPAT_DESCRIPTOR,
     'cjs/PlainOption/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -1059,6 +1071,8 @@ export const legacyApiTable = Object.freeze({
     'esm/PlainOption/orElseAsync': COMPAT_DESCRIPTOR,
     'esm/PlainOption/transpose': COMPAT_DESCRIPTOR,
     'esm/PlainOption/tap': COMPAT_DESCRIPTOR,
+    'esm/PlainOption/toNullable': COMPAT_DESCRIPTOR,
+    'esm/PlainOption/toUndefinable': COMPAT_DESCRIPTOR,
     'esm/PlainOption/unwrap': COMPAT_DESCRIPTOR,
     'esm/PlainOption/unwrapOr': COMPAT_DESCRIPTOR,
     'esm/PlainOption/unwrapOrElse': COMPAT_DESCRIPTOR,
@@ -1201,6 +1215,8 @@ export const legacyApiTable = Object.freeze({
     'lib/PlainOption/orElseAsync': COMPAT_DESCRIPTOR,
     'lib/PlainOption/transpose': COMPAT_DESCRIPTOR,
     'lib/PlainOption/tap': COMPAT_DESCRIPTOR,
+    'lib/PlainOption/toNullable': COMPAT_DESCRIPTOR,
+    'lib/PlainOption/toUndefinable': COMPAT_DESCRIPTOR,
     'lib/PlainOption/unwrap': COMPAT_DESCRIPTOR,
     'lib/PlainOption/unwrapOr': COMPAT_DESCRIPTOR,
     'lib/PlainOption/unwrapOrElse': COMPAT_DESCRIPTOR,


Test patch:
diff --git a/__tests__/PlainOption/to_nullable.test.mjs b/__tests__/PlainOption/to_nullable.test.mjs
new file mode 100644
index 00000000..3d0e63ef
--- /dev/null
+++ b/__tests__/PlainOption/to_nullable.test.mjs
@@ -0,0 +1,20 @@
+import test from 'ava';
+
+import { createSome, createNone } from '../../__dist/esm/PlainOption/Option.mjs';
+import { toNullableFromOption } from '../../__dist/esm/PlainOption/toNullable.mjs';
+
+const VALUE_T = Math.random();
+
+test('input is Some(T)', (t) => {
+    const input = createSome(VALUE_T);
+    const actual = toNullableFromOption(input);
+
+    t.is(actual, VALUE_T);
+});
+
+test('input is None', (t) => {
+    const input = createNone();
+    const actual = toNullableFromOption(input);
+
+    t.is(actual, null);
+});
diff --git a/__tests__/PlainOption/to_undefinable.test.mjs b/__tests__/PlainOption/to_undefinable.test.mjs
new file mode 100644
index 00000000..3172dcfe
--- /dev/null
+++ b/__tests__/PlainOption/to_undefinable.test.mjs
@@ -0,0 +1,20 @@
+import test from 'ava';
+
+import { createSome, createNone } from '../../__dist/esm/PlainOption/Option.mjs';
+import { toUndefinableFromOption } from '../../__dist/esm/PlainOption/toUndefinable.mjs';
+
+const VALUE_T = Math.random();
+
+test('input is Some(T)', (t) => {
+    const input = createSome(VALUE_T);
+    const actual = toUndefinableFromOption(input);
+
+    t.is(actual, VALUE_T);
+});
+
+test('input is None', (t) => {
+    const input = createNone();
+    const actual = toUndefinableFromOption(input);
+
+    t.is(actual, undefined);
+});