The _.withDefault function would accept a function as its argument, and returns a new function that transparently replaces null/undefined return values with a default value.
Example:
function getArrLength (arr) {
if (_.isArray(arr)) return arr.length;
}
getArrLengthOrZero = _.withDefault(getArrLength, 0);
getArrLengthOrZero({});
// => 0
May need a better name for the function, though.
The
_.withDefaultfunction would accept a function as its argument, and returns a new function that transparently replacesnull/undefinedreturn values with a default value.Example:
May need a better name for the function, though.