bitburner-src/src/Terminal/commands/expr.ts
2022-09-27 15:35:40 -04:00

21 lines
570 B
TypeScript

import { Terminal } from "../../Terminal";
export function expr(args: (string | number | boolean)[]): void {
if (args.length === 0) {
Terminal.error("Incorrect usage of expr command. Usage: expr [math expression]");
return;
}
const expr = args.join("");
// Sanitize the math expression
const sanitizedExpr = expr.replace(/s+/g, "").replace(/[^-()\d/*+.%]/g, "");
let result;
try {
result = eval(sanitizedExpr);
} catch (e) {
Terminal.error(`Could not evaluate expression: ${sanitizedExpr}`);
return;
}
Terminal.print(result);
}