From f9cbb6246b56e38e3dbcad7262a7315f9e5f37a1 Mon Sep 17 00:00:00 2001 From: BigYellowHammer <6392942+BigYellowHammer@users.noreply.github.com> Date: Tue, 5 Oct 2021 17:17:30 +0200 Subject: [PATCH] Adding basic arithmetic operation --- src/core/config/Categories.json | 3 +- src/core/lib/Arithmetic.mjs | 25 +++++++ src/core/lib/Delim.mjs | 5 ++ src/core/operations/AddSubMulDivValue.mjs | 74 ++++++++++++++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/AddSubMulDivValue.mjs | 50 +++++++++++++ 6 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/AddSubMulDivValue.mjs create mode 100644 tests/operations/tests/AddSubMulDivValue.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 09ee8d15..8d913aca 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -171,7 +171,8 @@ "Bit shift right", "Rotate left", "Rotate right", - "ROT13" + "ROT13", + "Add/Subtract/Multiply/Divide by value" ] }, { diff --git a/src/core/lib/Arithmetic.mjs b/src/core/lib/Arithmetic.mjs index 7c10855f..d5235df5 100644 --- a/src/core/lib/Arithmetic.mjs +++ b/src/core/lib/Arithmetic.mjs @@ -35,6 +35,31 @@ export function createNumArray(input, delim) { return numbers; } +/** + * Converts two strings to a number array. + * + * @param {string} input + * @param {string} userValue + * @returns {BigNumber[]} + */ +export function createNumArrayFromTwoStrings(input, userValue) { + const numbers = []; + let num; + try { + num = BigNumber(input.trim()); + if (!num.isNaN()) { + numbers.push(num); + } + num = BigNumber(userValue.trim()); + if (!num.isNaN()) { + numbers.push(num); + } + } catch (err) { + // This line is not a valid number + } + return numbers; +} + /** * Adds an array of numbers and returns the value. diff --git a/src/core/lib/Delim.mjs b/src/core/lib/Delim.mjs index c8cc637b..1480083b 100644 --- a/src/core/lib/Delim.mjs +++ b/src/core/lib/Delim.mjs @@ -46,6 +46,11 @@ export const HASH_DELIM_OPTIONS = ["Line feed", "CRLF", "Space", "Comma"]; */ export const IP_DELIM_OPTIONS = ["Line feed", "CRLF", "Space", "Comma", "Semi-colon"]; +/** + * Math operations + */ + export const ARITMETIC_OPTIONS = ["Add", "Subtract", "Multiply", "Divide"]; + /** * Split delimiters. */ diff --git a/src/core/operations/AddSubMulDivValue.mjs b/src/core/operations/AddSubMulDivValue.mjs new file mode 100644 index 00000000..ea95db0c --- /dev/null +++ b/src/core/operations/AddSubMulDivValue.mjs @@ -0,0 +1,74 @@ +/** + * @author BigYellowHammer + * @license Apache-2.0 + */ + +import BigNumber from "bignumber.js"; +import Operation from "../Operation.mjs"; +import { sum, sub, multi, div, createNumArrayFromTwoStrings } from "../lib/Arithmetic.mjs"; +import { ARITMETIC_OPTIONS } from "../lib/Delim.mjs"; + + +/** + * Sum operation + */ +class AddSubMulDivValue extends Operation { + + /** + * Sum constructor + */ + constructor() { + super(); + + this.name = "Add/Subtract/Multiply/Divide by value"; + this.module = "Default"; + this.description = "Performs aritmetical operation between the input and argument specified by the user"; + this.inputType = "string"; + this.outputType = "BigNumber"; + this.args = [ + { + "name": "Operation", + "type": "option", + "value": ARITMETIC_OPTIONS, + }, + { + "name": "Value", + "type": "shortString", + "value": "" + }, + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {BigNumber} + */ + run(input, args) { + + const arr = createNumArrayFromTwoStrings(input, args[1]); + let val; + + switch (args[0]) { + case "Add": + val = sum(arr); + break; + case "Subtract": + val = sub(arr); + break; + case "Multiply": + val = multi(arr); + break; + case "Divide": + val = div(arr); + break; + default: + break; + } + + return BigNumber.isBigNumber(val) ? val : new BigNumber(NaN); + } + +} + +export default AddSubMulDivValue; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 9add20b9..7972cbd4 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -107,6 +107,7 @@ import "./tests/CBORDecode.mjs"; import "./tests/JA3Fingerprint.mjs"; import "./tests/JA3SFingerprint.mjs"; import "./tests/HASSH.mjs"; +import "./tests/AddSubMulDivValue.mjs"; // Cannot test operations that use the File type yet diff --git a/tests/operations/tests/AddSubMulDivValue.mjs b/tests/operations/tests/AddSubMulDivValue.mjs new file mode 100644 index 00000000..14cdf569 --- /dev/null +++ b/tests/operations/tests/AddSubMulDivValue.mjs @@ -0,0 +1,50 @@ +/** + * BitwiseOp tests + * + * @author BigYellowHammer + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Add value", + input: "9", + expectedOutput: "11", + recipeConfig: [ + { "op": "Add/Subtract/Multiply/Divide by value", + "args": ["Add", "2"] }, + + ] + }, + { + name: "Subtract value", + input: "9", + expectedOutput: "7", + recipeConfig: [ + { "op": "Add/Subtract/Multiply/Divide by value", + "args": ["Subtract", "2"] }, + + ] + }, + { + name: "Multiply by value", + input: "9", + expectedOutput: "18", + recipeConfig: [ + { "op": "Add/Subtract/Multiply/Divide by value", + "args": ["Multiply", "2"] }, + + ] + }, + { + name: "Multiply by value", + input: "10", + expectedOutput: "5", + recipeConfig: [ + { "op": "Add/Subtract/Multiply/Divide by value", + "args": ["Divide", "2"] }, + + ] + } +]);