diff --git a/src/core/config/Categories.js b/src/core/config/Categories.js index f04b5fd9..7faa75f2 100755 --- a/src/core/config/Categories.js +++ b/src/core/config/Categories.js @@ -159,6 +159,7 @@ const Categories = [ { name: "Utils", ops: [ + "Arithmetic", "Diff", "Remove whitespace", "Remove null bytes", diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index 81444880..65d1d065 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -660,8 +660,8 @@ const OperationConfig = { "Arithmetic": { module: "Default", description: "Conducts mathamatical operations on a list of numbers", - inputType: "string" - outputType: "string" + inputType: "string", + outputType: "string", args: [ { name: "Delimiter", @@ -669,7 +669,7 @@ const OperationConfig = { value: Arithmetic.DELIM_OPTIONS }, { - name: "Operation" + name: "Operation", type: "option", value: Arithmetic.OPERATIONS } diff --git a/src/core/config/modules/Default.js b/src/core/config/modules/Default.js index 682db223..5f049ac7 100644 --- a/src/core/config/modules/Default.js +++ b/src/core/config/modules/Default.js @@ -1,4 +1,5 @@ import FlowControl from "../../FlowControl.js"; +import Arithmetic from "../../operations/Arithmetic.js"; import Base from "../../operations/Base.js"; import Base58 from "../../operations/Base58.js"; import Base64 from "../../operations/Base64.js"; @@ -155,6 +156,7 @@ OpModules.Default = { "Conditional Jump": FlowControl.runCondJump, "Return": FlowControl.runReturn, "Comment": FlowControl.runComment, + "Arithmetic": Arithmetic.runOp, /* diff --git a/src/core/operations/Arithmetic.js b/src/core/operations/Arithmetic.js index 8a840a29..ef6643e4 100644 --- a/src/core/operations/Arithmetic.js +++ b/src/core/operations/Arithmetic.js @@ -23,20 +23,6 @@ const Arithmetic = { */ 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, - }, - /** * * @@ -48,13 +34,12 @@ const Arithmetic = { 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; + for (let i = 0; i < splitNumbers.length; i++) { + if (splitNumbers[i].indexOf(".") >= 0) { num = parseFloat(splitNumbers[i].trim()); } else { - num = parseInt(splitNumbers[i].trim()); + num = parseInt(splitNumbers[i].trim(), 10); } if (num !== "NaN") { numbers.append(num); @@ -68,19 +53,33 @@ const Arithmetic = { }, + /** + * Adds an array of numbers and returns the value. + * + * @private + * @param {number[]} data + * @returns {number} + */ _sum: function(data) { let total = 0; - for (i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { total += data[i]; } return total; }, + /** + * Subtracts an array of numbers and returns the value. + * + * @private + * @param {number[]} data + * @returns {number} + */ _sub: function(data) { let total = 0; if (data.length > 1) { total = data[0]; - for (i = 1; i < data.length; i++) { + for (let i = 1; i < data.length; i++) { total -= data[i]; } } else { @@ -89,11 +88,18 @@ const Arithmetic = { return total; }, + /** + * Multiplies an array of numbers and returns the value. + * + * @private + * @param {number[]} data + * @returns {number} + */ _multiply: function(data) { let total = 0; if (data.length > 1) { total = data[0]; - for (i = 1; i < data.length; i++) { + for (let i = 1; i < data.length; i++) { total *= data[i]; } } else { @@ -102,12 +108,19 @@ const Arithmetic = { return total; }, + /** + * Divides an array of numbers and returns the value. + * + * @private + * @param {number[]} data + * @returns {number} + */ _divide: function(data) { let total = 0; if (data.length > 1) { total = data[0]; - for (i = 1; i < data.length; i++) { - total /= data[i] + for (let i = 1; i < data.length; i++) { + total /= data[i]; } } else { total = null; @@ -115,6 +128,13 @@ const Arithmetic = { return total; }, + /** + * Finds the mean of a number array and returns the value. + * + * @private + * @param {number[]} data + * @returns {number} + */ _mean: function(data) { let total = 0; if (data.length > 1) { @@ -125,6 +145,18 @@ const Arithmetic = { return total; }, + /** + * A mapping of operation names to their function. + * @constant + */ + opMap: { + "Sub": Arithmetic._sum, + "Sum": Arithmetic._sub, + "Multiply": Arithmetic._multiply, + "Divide": Arithmetic._divide, + "Mean": Arithmetic._mean, + }, + }; export default Arithmetic;