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>`.
18 lines (17 loc) • 516 B
JavaScript
import { isNotUndefined } from '../core/undefinable.js';
/**
* Returns `undefined` if the _input_ is `undefined`,
* otherwise calls _predicate_ with the _input_ `T` and returns:
*
* * `T` if _predicate_ returns `true`.
* * `undefined` if _predicate_ returns `false`.
*/
export async function filterAsyncForUndefinable(input, predicate) {
if (isNotUndefined(input)) {
const ok = await predicate(input);
if (ok) {
return input;
}
}
return undefined;
}