Conditional with nullish coalescing operator
Using the nullish coalescing operator to compose functions that classify a result.
by Peer Reynders
JavaScript
const STATUS = {
reject: 'nope',
accept: 'hire',
reevaluate: 'maybe',
};
const results = {
noShow: false,
immediateAvailability: true,
experience: {
angular: false,
react: false,
vue: true,
},
};
const status = interviewStatus(results);
console.log(status);
function interviewStatus(results) {
return maybeReject(results) ?? maybeAccept(results) ?? STATUS.reevaluate;
}
// "maybe" prefix - returns a value or `undefiend`
function maybeReject({ noShow }) {
return noShow ? STATUS.reject : undefined;
}
function maybeAccept({
experience: { angular, react, vue },
immediateAvailability,
}) {
return angular || react || (vue && immediateAvailability)
? STATUS.accept
: undefined;
}