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

View File

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