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.

36 lines
1.1 KiB
JavaScript

"use strict";
const unreachable = require("./")("unreachable-demo");
function someFunction(ducksEaten) {
let mapping = {
1: "one",
2: "two",
3: "three"
};
let mappedValue = mapping[ducksEaten];
if (mappedValue == null) {
throw new Error(`Invalid value specified`);
} else if (mappedValue === "one") {
return "Only four little ducks came back";
} else if (mappedValue === "two") {
return "Only three little ducks came back";
} else { // Oops, we forgot to add a case for "three"! Good thing we have a safety check here.
unreachable(`Encountered unexpected mapped value '${mappedValue}'`);
}
}
console.log(someFunction(1)); // Only four little ducks came back
console.log(someFunction(2)); // Only three little ducks came back
console.log(someFunction(3)); /*
Error: Encountered unexpected mapped value 'three' -- this should never happen and it's a bug in 'unreachable-demo', please report it!
at unreachable (/home/sven/projects/unreachable/index.js:5:9)
at someFunction (/home/sven/projects/unreachable/example.js:21:3)
at Object.<anonymous> (/home/sven/projects/unreachable/example.js:29:13)
[ ... ]
*/