option-t
Version:
A toolkit of Nullable/Option/Result type implementation in ECMAScript. Their APIs are inspired by Rust's `Option<T>` and `Result<T, E>`.
25 lines (24 loc) • 807 B
JavaScript
import { ERR_MSG_UNWRAP_NO_VAL_FOR_MAYBE } from '../internal/error_message.js';
export function isNotNullOrUndefined(input) {
return input !== undefined && input !== null;
}
export function isNullOrUndefined(input) {
return input === undefined || input === null;
}
/**
* Return _input_ as `T` if the passed _input_ is not `null` and `undefined`.
* Otherwise, throw `TypeError` with the passed `msg`.
*/
export function expectNotNullOrUndefined(input, msg) {
if (isNotNullOrUndefined(input)) {
return input;
}
throw new TypeError(msg);
}
/**
* Return _value_ as `T` if the passed _value_ is not `null` and `undefined`.
* Otherwise, throw `TypeError`.
*/
export function unwrapMaybe(value) {
return expectNotNullOrUndefined(value, ERR_MSG_UNWRAP_NO_VAL_FOR_MAYBE);
}