Util.coalesce()

Return the first value if it is defined, otherwise the second.

The two values must have compatible types! For example, "foo" is compatible with "bar", but it is not compatible with `{}`. Also note that, in JavaScript, this is equivalent to `typeof a!=="undefined" ? a : b`, not `a||b`. In other words, if the preferred value is false, zero, or the empty string, it will still be used. The alternate value will only be used if the preferred value is **not defined**.

Usage

Hover over inputs/exits for more info.

var Util = require('machinepack-util');


// Return the first value if it is defined, otherwise the second.

Util.coalesce({

b: '===',

a: '===',

}).execSync();

Inputs

b required

The backup/alternate value to use in case the preferred value is not defined.

'==='
a

The preferred value to use, if it's defined. Must be the same type as the alternate.

'==='

Exits

error

An unexpected error occurred.

success

OK.

(varies based on provided inputs)