plugins-evert/utility/math.ts

30 lines
528 B
TypeScript

import { evaluate } from 'mathjs';
function done(data: string): void {
if (process && process.send) {
process.send(data);
}
}
process.once('message', (msg) => {
let expr = msg.toString();
if (!expr) {
return done('null');
}
if (expr.indexOf('=') === expr.length - 1) {
expr = expr.substring(0, expr.length - 1);
}
expr = expr.replace(/π/g, 'pi');
let result = 'null';
try {
result = evaluate(expr);
} catch (e) {
return done(e.message);
}
return done(result.toString());
});