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) • 737 B
JavaScript
import { ERR_MSG_UNWRAP_NO_VAL_FOR_UNDEFINABLE } from '../internal/error_message.js';
export function isNotUndefined(input) {
return input !== undefined;
}
export function isUndefined(input) {
return input === undefined;
}
/**
* Return _input_ as `T` if the passed _input_ is not `undefined`.
* Otherwise, throw `TypeError` with the passed `msg`.
*/
export function expectNotUndefined(input, msg) {
if (isNotUndefined(input)) {
return input;
}
throw new TypeError(msg);
}
/**
* Return _input_ as `T` if the passed _input_ is not `undefined`.
* Otherwise, throw `TypeError`.
*/
export function unwrapUndefinable(input) {
return expectNotUndefined(input, ERR_MSG_UNWRAP_NO_VAL_FOR_UNDEFINABLE);
}