Browse Source

Make value evaluation opt-in

master
Sven Slootweg 5 years ago
parent
commit
64cbd78cc7
  1. 20
      src/index.js

20
src/index.js

@ -10,24 +10,32 @@ function evaluateValue(value) {
}
}
function defaultValue(value, fallbackValue) {
value = evaluateValue(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 evaluateValue(fallbackValue);
return maybeEvaluateValue(fallbackValue, options.evaluate);
}
}
defaultValue.async = function defaultAsyncValue(value, fallbackValue) {
defaultValue.async = function defaultAsyncValue(value, fallbackValue, options = {}) {
return promiseTry(() => {
return evaluateValue(value);
return maybeEvaluateValue(value, options.evaluate);
}).then((resultValue) => {
if (resultValue != null) {
return resultValue;
} else {
return evaluateValue(fallbackValue);
return maybeEvaluateValue(fallbackValue, options.evaluate);
}
})
}

Loading…
Cancel
Save