You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
910 B
JavaScript
43 lines
910 B
JavaScript
'use strict';
|
|
|
|
const promiseTry = require("es6-promise-try");
|
|
|
|
function evaluateValue(value) {
|
|
if (typeof value === "function") {
|
|
return value();
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
function maybeEvaluateValue(value, evaluate) {
|
|
if (evaluate === true) {
|
|
return evaluateValue(value);
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
function defaultValue(value, fallbackValue, options = {}) {
|
|
value = maybeEvaluateValue(value, options.evaluate);
|
|
|
|
if (value != null) {
|
|
return value;
|
|
} else {
|
|
return maybeEvaluateValue(fallbackValue, options.evaluate);
|
|
}
|
|
}
|
|
|
|
defaultValue.async = function defaultAsyncValue(value, fallbackValue, options = {}) {
|
|
return promiseTry(() => {
|
|
return maybeEvaluateValue(value, options.evaluate);
|
|
}).then((resultValue) => {
|
|
if (resultValue != null) {
|
|
return resultValue;
|
|
} else {
|
|
return maybeEvaluateValue(fallbackValue, options.evaluate);
|
|
}
|
|
})
|
|
}
|
|
|
|
module.exports = defaultValue; |