41 lines
768 B
TypeScript
41 lines
768 B
TypeScript
import { evaluate, simplify } from 'mathjs';
|
|
|
|
function done(data: string): void {
|
|
if (process && process.send) {
|
|
process.send(data);
|
|
}
|
|
}
|
|
|
|
process.once('message', (msg) => {
|
|
msg = msg.toString();
|
|
|
|
const dat = msg.split(' ');
|
|
if (!(dat.length > 1)) {
|
|
return;
|
|
}
|
|
|
|
let expr = dat.slice(1).join(' ');
|
|
|
|
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 {
|
|
if (dat[0] === 'eval') {
|
|
result = evaluate(expr);
|
|
} else if (dat[0] === 'simplify') {
|
|
result = simplify(expr).toString();
|
|
}
|
|
} catch (e) {
|
|
return done(e.message);
|
|
}
|
|
|
|
return done(result.toString());
|
|
});
|