瀏覽代碼

start of math operations

bwhitn 7 年之前
父節點
當前提交
b9b4147c2f
共有 2 個文件被更改,包括 149 次插入0 次删除
  1. 19 0
      src/core/config/OperationConfig.js
  2. 130 0
      src/core/operations/Arithmetic.js

+ 19 - 0
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.",

+ 130 - 0
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;