Skip to main content
@spa-tools/utilities

tern()

The native conditional ternary operator ? is awesome, but have you ever needed to execute a separate logic path for null/undefined values versus actual boolean values?

This is what the tern<T, F, U>() function is for. It's a simple utility that allows you to pass in a condition with three separate execution paths for true, false, and null/undefined.

  • The generic T type is used to define the value type returned when true.
  • The generic F type is used to define the value type returned when false.
  • The generic U type is used to define the value type returned when null or undefined.

Usage

import { tern } from '@spa-tools/utilities';

// contrived function to get a random
// vote of either true, false, or null
function getRandomVote(): boolean | null {
const randomNum = Math.random();
if (randomNum < 0.33) {
return true;
} else if (randomNum < 0.66) {
return false;
} else {
return null;
}
}

const vote = getRandomVote();

console.log(
'And the vote is:',
// here the tern function decides which path to take
// with the three possible branches based on vote value
tern(vote, 'Yea!', 'Nay!', 'Abstained')
);

Arguments

NameTypeDescription
valueboolean | null | undefinedThe expression/value to evaluate
whenTrueTThe value to return when the expression evaluates to true
whenFalseFThe value to return when the expression evaluates to false
whenNullUThe value to return when the expression evaluates to null or undefined

Returns

Returns either T, F, U, accordingly.