@neth4ck/neth4ck
Version:
NetHack WASM shim — version-agnostic callback bridge and module initializer
35 lines (30 loc) • 901 B
JavaScript
const optionMap = new Map([
["autoquiver", "boolean"],
["name", "string"],
]);
export function createNethackOptions(opt) {
const optStr = [];
for (const key in opt) {
if (!optionMap.has(key)) {
throw new TypeError(`NetHack option not recognized: ${key}`);
}
const type = optionMap.get(key);
checkType(key, opt[key], type);
switch (type) {
case "string":
optStr.push(`${key}:${opt[key]}`);
break;
case "boolean":
optStr.push(`${opt[key] ? "" : "!"}${key}`);
break;
default:
throw new Error(`unknown type: ${type}`);
}
}
return optStr.join(",");
}
function checkType(key, val, type) {
if (typeof val !== type) {
throw new Error(`Expected NetHack option '${key}' to be ${type}`);
}
}