import { type None } from "https://deno.land/x/eitherway@0.10.0/lib/core/mod.ts";
None
None
represents the absence of a value and is the opinionated, composable
equivalent of undefined
.
It support coercion to the falsy representation of primitive types
Furthermore, it implements the iterator protocol and returns undefined
when it gets JSON encoded via JSON.stringify()
Please checkout Some
for the opposite case.
Examples
Example 1
Example 1
import { assert } from "./assert.ts";
import { Option, None, Some } from "./option.ts";
const none = None;
const rec = { a: 1, b: none };
const arr = [ ...none ];
const encode = JSON.stringify;
//TS Bug: https://github.com/microsoft/TypeScript/issues/39064
assert(none instanceof (None as any) === true);
assert(none.isNone() === true);
assert(none.isSome() === false);
assert(none.unwrap() === undefined);
assert(String(none) === "");
assert(Number(none) === 0);
assert(none[Symbol.toPrimitive]() === false);
assert(Boolean(none) === true); //object always evaluate to true
assert(encode(rec) === encode({ a: 1 }));
assert(arr.length === 0);