Provides a simple Option type that can be used to represent a value that may or may not be present. It is similar to the Option type in Rust or the Optional type in Java. It's main purpose is to avoid null or undefined values and the need to check for them.
Is done in TypeScript, is tree-shakable and has no dependencies.
see jsr documentation for details on how to install
import { maybe } from '@nnou/option';
function handleMessage(message?: string | null): void {
const messageOption = maybe(message);
if (messageOption.hasValue) {
console.log(messageOption.value);
} else {
console.log('No message');
}
}Creates an Option with a value.
import { some, Option } from '@nnou/option';
const option = some(42); // Some<number>
const option: Option<number> = some(42);Creates an Option without a value.
import { none, Option } from '@nnou/option';
const option = none(); // None
const option: Option<string> = none();Creates an Option with a value if it is not null or undefined.
import { maybe } from '@nnou/option';
const option = maybe(42); // Option<number>
const option = maybe<null | number>(null); // Option<number>
const option = maybe<string>(); // Option<string>Checks if a value is an Option.
import { isOption } from '@nnou/option';
const value: unknown = ...;
if (isOption(value)) {
// value is an Option
}Checks if an Option is None.
import { assertNone, none, some } from '@nnou/option';
const option = none();
assertNone(option); // OK
const option = some(42);
assertNone(option); // ErrorChecks if an Option is Some.
import { assertSome, none, some } from '@nnou/option';
const option = some(42);
assertSome(option); // OK
const option = none();
assertSome(option); // ErrorChecks if an Option is Some with a specific value.
import { assertSomeValue, none, some } from '@nnou/option';
const option = some(42);
assertSomeValue(option, 42); // OK
const option = some(42);
assertSomeValue(option, 43); // Error
const option = none();
assertSomeValue(option, 42); // ErrorUnwraps an Option and returns the value if it is Some or a default value if it is None.
import { unwrap, none, some } from '@nnou/option';
const option = some(42);
const value = unwrap(option, 0); // 42
const option = none();
const value = unwrap(option, 0); // 0
const value = unwrap(option, () => 0); // 0Maps an Option to another Option.
import { map, none, some } from '@nnou/option';
const option = some(42);
const mappedOption = map(option, value => value.toString()); // Option<string>
const option = none();
const mappedOption = map(option, value => value.toString()); // Option<string>Maps an Option to a value.
import { mapOr, none, some } from '@nnou/option';
const option = some(42);
const value = mapOr(option, (v) => v, () => 10); // '42'
const option = none();
const value = mapOr(option, (v) => v, () => 10); // 10Flattens an Option of an Option.
import { flatten, none, some } from '@nnou/option';
const option = some(some(42));
const flattenedOption = flatten(option); // Some<number>
const option = none();
const flattenedOption = flatten(option); // NoneCreates an Option from a Promise.
import { fromPromise } from '@nnou/option';
const promise = Promise.resolve(42);
const option = await fromPromise(promise); // Option<number>
const promise = Promise.reject();
const option = await fromPromise(promise); // None
const promise = Promise.reject();
const option = await fromPromise(promise, false); // ErrorCreates an Option from a Result.
import { fromResult } from '@nnou/option';
const result = { ok: true, value: 42 };
const option = fromResult(result); // Option<number>
const result = { ok: false };
const option = fromResult(result); // NonefromAsyncResult<T>(result: Promise<{ ok: false } | { ok: true; value: T }>, catchError = true): Promise<Option<T>>
Creates an Option from a Promise of a Result.
import { fromAsyncResult } from '@nnou/option';
const result = Promise.resolve({ ok: true, value: 42 });
const option = await fromAsyncResult(result); // Option<number>
const result = Promise.resolve({ ok: false });
const option = await fromAsyncResult(result); // None
const result = Promise.reject();
const option = await fromAsyncResult(result); // None
const result = Promise.reject();
const option = await fromAsyncResult(result, false); // Error