From b9b4147c2fdfab8ed2bf12d9720c765be9cdcc8e Mon Sep 17 00:00:00 2001 From: bwhitn Date: Sun, 17 Dec 2017 15:19:10 -0500 Subject: [PATCH] start of math operations --- src/core/config/OperationConfig.js | 19 +++++ src/core/operations/Arithmetic.js | 130 +++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 src/core/operations/Arithmetic.js diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index 43072558..ff609ba2 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -1,3 +1,4 @@ +import Arithmetic from "../operations/Arithmetic.js"; import Base from "../operations/Base.js"; import Base58 from "../operations/Base58.js"; import Base64 from "../operations/Base64.js"; @@ -657,6 +658,24 @@ const OperationConfig = { } ] }, + "Arithmetic": { + module: "Default", + description: "Conducts mathamatical operations on a list of numbers", + inputType: "string" + outputType: "string" + args: [ + { + name: "Delimiter", + type: "option", + value: Arithmetic.DELIM_OPTIONS + }, + { + name: "Operation" + type: "option", + value: Arithmetic.OPERATIONS + } + ] + }, "From Hexdump": { module: "Default", description: "Attempts to convert a hexdump back into raw data. This operation supports many different hexdump variations, but probably not all. Make sure you verify that the data it gives you is correct before continuing analysis.", diff --git a/src/core/operations/Arithmetic.js b/src/core/operations/Arithmetic.js new file mode 100644 index 00000000..8a840a29 --- /dev/null +++ b/src/core/operations/Arithmetic.js @@ -0,0 +1,130 @@ +import Utils from "../Utils.js"; + +/** + * Math operations on numbers. + * + * @author bwhitn [brian.m.whitney@outlook.com] + * @copyright Crown Copyright 2016 + * @license Apache-2.0 + * + * @namespace + */ +const Arithmetic = { + + /** + * @constant + * @default + */ + DELIM_OPTIONS: ["Space", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF"], + + /** + * @constant + * @default + */ + OPERATIONS: ["Sum", "Sub", "Multiply", "Divide", "Mean", "Median", "Mode"], + + /** + * A mapping of operation names to their function. + * @constant + */ + opMap: { + "Sub": _sub, + "Sum": _sum, + "Multiply": _multiply, + "Divide": _divide, + "Mean": _mean, + "Median": _median, + "Mode": _mode, + }, + + /** + * + * + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + runOp: function(input, args) { + const delim = Utils.charRep[args[0] || "Space"]; + let splitNumbers = input.split(delim), + numbers = [], + num, + retVal; + for (i = 0; i < splitNumbers.length; i++) { + if splitNumbers[i].indexOf(".") { + num = parseFloat(splitNumbers[i].trim()); + } else { + num = parseInt(splitNumbers[i].trim()); + } + if (num !== "NaN") { + numbers.append(num); + } + } + num = Arithmetic.opMap[args[1] || "Sum"](numbers); + if (num !== null) { + return "The values " + args[1] + "equal: " + num; + } + throw "Error with Arithmetic Operation: " + args[1]; + }, + + + _sum: function(data) { + let total = 0; + for (i = 0; i < data.length; i++) { + total += data[i]; + } + return total; + }, + + _sub: function(data) { + let total = 0; + if (data.length > 1) { + total = data[0]; + for (i = 1; i < data.length; i++) { + total -= data[i]; + } + } else { + total = null; + } + return total; + }, + + _multiply: function(data) { + let total = 0; + if (data.length > 1) { + total = data[0]; + for (i = 1; i < data.length; i++) { + total *= data[i]; + } + } else { + total = null; + } + return total; + }, + + _divide: function(data) { + let total = 0; + if (data.length > 1) { + total = data[0]; + for (i = 1; i < data.length; i++) { + total /= data[i] + } + } else { + total = null; + } + return total; + }, + + _mean: function(data) { + let total = 0; + if (data.length > 1) { + total = Arithmetic._sum(data) / data.length; + } else { + total = null; + } + return total; + }, + +}; + +export default Arithmetic;