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) • 431 B
JavaScript
import { isNull } from '../core/nullable.js';
/**
* Return one of this pattern:
*
* 1. a=others, b=null => _a_
* 2. a=null, b=others => _b_
* 3. Others => `null`
*/
export function xorForNullable(a, b) {
const aIsNull = isNull(a);
const bIsNull = isNull(b);
if (!aIsNull && bIsNull) {
return a;
}
else if (aIsNull && !bIsNull) {
return b;
}
else {
return null;
}
}