From 4a3b22f2576048cc1ceae3f1060b94ec33499484 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sun, 6 Dec 2020 15:41:18 +0200 Subject: [PATCH] some changes to utilities --- utility/math.ts | 17 +++----------- utility/plugin.ts | 58 ++++++++++++++++------------------------------- 2 files changed, 22 insertions(+), 53 deletions(-) diff --git a/utility/math.ts b/utility/math.ts index 4ffe58a..c31629b 100644 --- a/utility/math.ts +++ b/utility/math.ts @@ -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); } diff --git a/utility/plugin.ts b/utility/plugin.ts index 9aa8737..fc26c77 100644 --- a/utility/plugin.ts +++ b/utility/plugin.ts @@ -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 { +function opMath(expression: string): Promise { 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 { }, 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 => { + 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: '', - description: 'Evaluate a math expression' - }); - - cmds.push({ - name: 'simplify', - execute: async (msg: IMessage, spec: any, prefix: string, ...simplified: any[]): Promise => { - 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: '', - 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 => { + 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 and .', usage: ' []', - aliases: ['rnum', 'rand'] + aliases: ['rnum', 'rand', 'rng'] }); commands.registerCommand(cmds.map((x: any) => {