Adding basic arithmetic operation

This commit is contained in:
BigYellowHammer 2021-10-05 17:17:30 +02:00
parent ae1b12c120
commit f9cbb6246b
6 changed files with 157 additions and 1 deletions

View file

@ -171,7 +171,8 @@
"Bit shift right", "Bit shift right",
"Rotate left", "Rotate left",
"Rotate right", "Rotate right",
"ROT13" "ROT13",
"Add/Subtract/Multiply/Divide by value"
] ]
}, },
{ {

View file

@ -35,6 +35,31 @@ export function createNumArray(input, delim) {
return numbers; 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. * Adds an array of numbers and returns the value.

View file

@ -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"]; export const IP_DELIM_OPTIONS = ["Line feed", "CRLF", "Space", "Comma", "Semi-colon"];
/**
* Math operations
*/
export const ARITMETIC_OPTIONS = ["Add", "Subtract", "Multiply", "Divide"];
/** /**
* Split delimiters. * Split delimiters.
*/ */

View file

@ -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;

View file

@ -107,6 +107,7 @@ import "./tests/CBORDecode.mjs";
import "./tests/JA3Fingerprint.mjs"; import "./tests/JA3Fingerprint.mjs";
import "./tests/JA3SFingerprint.mjs"; import "./tests/JA3SFingerprint.mjs";
import "./tests/HASSH.mjs"; import "./tests/HASSH.mjs";
import "./tests/AddSubMulDivValue.mjs";
// Cannot test operations that use the File type yet // Cannot test operations that use the File type yet

View file

@ -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"] },
]
}
]);