30 lines
541 B
TypeScript
30 lines
541 B
TypeScript
import { evaluate } from 'mathjs';
|
|
|
|
function done(data: string): void {
|
|
if (process && process.send) {
|
|
process.send(data);
|
|
}
|
|
}
|
|
|
|
process.once('message', (msg: string) => {
|
|
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: any) {
|
|
return done(e.message);
|
|
}
|
|
|
|
return done(result.toString());
|
|
});
|