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.
33 lines
924 B
JavaScript
33 lines
924 B
JavaScript
"use strict";
|
|
|
|
// FIXME: Switch to `match-value` package
|
|
|
|
function getValue(value, functionsAreLiterals) {
|
|
if (typeof value === "function" && !functionsAreLiterals) {
|
|
return value();
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
function doMatchValue(value, arms, functionsAreLiterals) {
|
|
if (value == null) {
|
|
return value;
|
|
} else if (arms[value] !== undefined) {
|
|
// NOTE: We intentionally only check for `undefined` here (and below), since we want to allow the mapped-to value to be an explicit `null`.
|
|
return getValue(arms[value], functionsAreLiterals);
|
|
} else if (arms._ !== undefined) {
|
|
return getValue(arms._, functionsAreLiterals);
|
|
} else {
|
|
throw new Error(`No match arm found for value '${value}'`);
|
|
}
|
|
}
|
|
|
|
module.exports = function matchValue(value, arms) {
|
|
return doMatchValue(value, arms, true);
|
|
};
|
|
|
|
module.exports.literal = function matchValueLiteral(value, arms) {
|
|
return doMatchValue(value, arms, false);
|
|
};
|