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>`.
22 lines (21 loc) • 638 B
JavaScript
import { isNotNullOrUndefined } from './maybe.js';
/**
* Return one of this pattern:
*
* 1. a=others, b=null|undefined => _a_
* 2. a=null|undefined, b=others => _b_
* 3. Others => `undefined`
*/
export function xorForMaybe(a, b) {
const aIsSome = isNotNullOrUndefined(a);
const bIsSome = isNotNullOrUndefined(b);
if (aIsSome && !bIsSome) {
return a;
}
if (!aIsSome && bIsSome) {
return b;
}
// XXX: We can choose both `null` and `undefined`.
// But we return `undefined` to sort with [Optional Chaining](https://github.com/TC39/proposal-optional-chaining)
return undefined;
}