some changes to utilities

This commit is contained in:
Evert Prants 2020-12-06 15:41:18 +02:00
parent 761157e693
commit 4a3b22f257
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 22 additions and 53 deletions

View File

@ -1,4 +1,4 @@
import { evaluate, simplify } from 'mathjs';
import { evaluate } from 'mathjs';
function done(data: string): void {
if (process && process.send) {
@ -7,14 +7,7 @@ function done(data: string): void {
}
process.once('message', (msg) => {
msg = msg.toString();
const dat = msg.split(' ');
if (!(dat.length > 1)) {
return;
}
let expr = dat.slice(1).join(' ');
let expr = msg.toString();
if (!expr) {
return done('null');
@ -27,11 +20,7 @@ process.once('message', (msg) => {
let result = 'null';
try {
if (dat[0] === 'eval') {
result = evaluate(expr);
} else if (dat[0] === 'simplify') {
result = simplify(expr).toString();
}
result = evaluate(expr);
} catch (e) {
return done(e.message);
}

View File

@ -59,7 +59,7 @@ const urlRegex = /(((ftp|https?):\/\/)[-\w@:%_+.~#?,&//=]+)/g;
const fork = cprog.fork;
// Run mathjs in a separate thread to avoid the killing of the main process
function opMath(expression: string, method = 'eval'): Promise<string> {
function opMath(expression: string): Promise<string> {
return new Promise((resolve, reject) => {
// Fork the script
const mathThread = fork(path.join(__dirname, 'math.js'));
@ -74,7 +74,7 @@ function opMath(expression: string, method = 'eval'): Promise<string> {
}, 8000);
// Send data to the thread to process
mathThread.send(method + ' ' + expression);
mathThread.send(expression);
// Recieve data
mathThread.on('message', (chunk) => {
@ -131,19 +131,13 @@ function RGBToHSL(r: number, g: number, b: number): {[key: string]: number} | nu
// No difference
if (delta === 0) {
h = 0;
}
// Red is max
} // Red is max
else if (cmax === r) {
h = ((g - b) / delta) % 6;
}
// Green is max
} // Green is max
else if (cmax === g) {
h = (b - r) / delta + 2;
}
// Blue is max
} // Blue is max
else {
h = (r - g) / delta + 4;
}
@ -341,6 +335,11 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
cmds.push({
name: 'numsys',
execute: async (msg: IMessage, spec: any, prefix: string, ...simplified: any[]): Promise<boolean> => {
if (simplified.length < 3) {
msg.resolve('Too few arguments!');
return true;
}
const input = simplified[0];
const src = simplified[1].toLowerCase();
const dst = simplified[2].toLowerCase();
@ -429,32 +428,9 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
return true;
},
aliases: ['evaluate', 'math', 'equation', 'calc'],
aliases: ['math', 'calc'],
usage: '<expression>',
description: 'Evaluate a math expression'
});
cmds.push({
name: 'simplify',
execute: async (msg: IMessage, spec: any, prefix: string, ...simplified: any[]): Promise<boolean> => {
if (!simplified[0]) {
return true;
}
const wholeRow = msg.text.split(' ').slice(1).join(' ');
try {
const repl = await opMath(wholeRow, 'simplify');
msg.resolve(repl);
} catch (e) {
msg.resolve('Could not evaluate expression:', e.message);
}
return true;
},
aliases: ['mathsimple', 'algebra'],
usage: '<expression>',
description: 'Simplify a math expression'
description: 'Evaluate a math expression (See https://mathjs.org/)'
});
cmds.push({
@ -516,9 +492,8 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
msg.resolve('Invalid colors');
return true;
}
const hex = rgbToHex(r, g, b);
msg.resolve(hex);
msg.resolve(rgbToHex(r, g, b));
return true;
},
description: 'Convert RGB to HEX colors',
@ -762,6 +737,11 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
cmds.push({
name: 'randomnumber',
execute: async (msg: IMessage, spec: any, prefix: string, ...simplified: any[]): Promise<boolean> => {
if (simplified.length < 2) {
msg.resolve('Too few arguments!');
return true;
}
let min = parseInt(simplified[0], 10);
let max = parseInt(simplified[1], 10);
let count = parseInt(simplified[2], 10);
@ -793,7 +773,7 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
},
description: 'Generate a random number between <min> and <max>.',
usage: '<min> <max> [<count>]',
aliases: ['rnum', 'rand']
aliases: ['rnum', 'rand', 'rng']
});
commands.registerCommand(cmds.map((x: any) => {