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) • 682 B
JavaScript
import { ERR_MSG_UNWRAP_NO_VAL_FOR_NULLABLE } from './internal/error_message.js';
export function isNotNull(input) {
return input !== null;
}
export function isNull(input) {
return input === null;
}
/**
* Return _input_ as `T` if the passed _input_ is not `null`.
* Otherwise, throw `TypeError` with the passed `msg`.
*/
export function expectNotNull(input, msg) {
if (isNotNull(input)) {
return input;
}
throw new TypeError(msg);
}
/**
* Return _input_ as `T` if the passed _input_ is not `null`.
* Otherwise, throw `TypeError`.
*/
export function unwrapNullable(input) {
return expectNotNull(input, ERR_MSG_UNWRAP_NO_VAL_FOR_NULLABLE);
}