mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-25 02:32:55 +02:00
23 lines
734 B
TypeScript
23 lines
734 B
TypeScript
import { Terminal } from "../../Terminal";
|
|
import { StdIO } from "../StdIO/StdIO";
|
|
import { BaseServer } from "../../Server/BaseServer";
|
|
|
|
export function expr(args: (string | number | boolean)[], server: BaseServer, stdIO: StdIO): void {
|
|
if (args.length === 0) {
|
|
Terminal.error("Incorrect usage of expr command. Usage: expr [math expression]", stdIO);
|
|
return;
|
|
}
|
|
const expr = args.join("");
|
|
|
|
// Sanitize the math expression
|
|
const sanitizedExpr = expr.replace(/[^-()\deE/*+.%]/g, "");
|
|
let result: string;
|
|
try {
|
|
result = String(eval?.(sanitizedExpr));
|
|
} catch (e) {
|
|
Terminal.error(`Could not evaluate expression: ${sanitizedExpr}. Error: ${e}.`, stdIO);
|
|
return;
|
|
}
|
|
Terminal.print(result, stdIO);
|
|
}
|