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>`.
20 lines (19 loc) • 455 B
JavaScript
import { isNotUndefined } from '../core/undefinable.js';
/**
* Return one of this pattern:
*
* 1. a=others, b=undefined => _a_
* 2. a=undefined, b=others => _b_
* 3. Others => `undefined`
*/
export function xorForUndefinable(a, b) {
const aIsSome = isNotUndefined(a);
const bIsSome = isNotUndefined(b);
if (aIsSome && !bIsSome) {
return a;
}
if (!aIsSome && bIsSome) {
return b;
}
return undefined;
}