From 0fea84ed7a63492f6cea6de19b8a5452ac17db18 Mon Sep 17 00:00:00 2001 From: bwhitn Date: Thu, 21 Dec 2017 00:19:47 -0500 Subject: [PATCH] WIP --- src/core/config/Categories.js | 9 ++- src/core/operations/Arithmetic.js | 100 +++++++++++++++--------------- 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/src/core/config/Categories.js b/src/core/config/Categories.js index 2109ff15..0ab32eb1 100755 --- a/src/core/config/Categories.js +++ b/src/core/config/Categories.js @@ -113,7 +113,7 @@ const Categories = [ ] }, { - name: "Logical operations", + name: "Arithmetic / Logic", ops: [ "XOR", "XOR Brute Force", @@ -127,6 +127,13 @@ const Categories = [ "Rotate left", "Rotate right", "ROT13", + "Sum", + "Sub", + "Multiply", + "Divide", + "Mean", + "Median", + "Standard Deviation", ] }, { diff --git a/src/core/operations/Arithmetic.js b/src/core/operations/Arithmetic.js index 1151c28f..6472a7fc 100644 --- a/src/core/operations/Arithmetic.js +++ b/src/core/operations/Arithmetic.js @@ -31,15 +31,58 @@ const Arithmetic = { "Standard Deviation" ], + /** * * * @param {string} input * @param {Object[]} args - * @returns {string} + * @returns {number} */ - runOp: function(input, args) { - const delim = Utils.charRep[args[0] || "Space"]; + computeSum: function(input, args) { + const val = Arithmetic._sum(Arithmetic._createNumArray(input, args[0])); + return typeof(val) === 'number' ? val.toString() : ""; + }, + + + computeSub: function(input, args) { + let val = Arithmetic._sub(Arithmetic._createNumArray(input, args[0])); + return typeof(val) === 'number' ? val.toString() : ""; + }, + + + computeMulti: function(input, args) { + let val = Arithmetic._multi(Arithmetic._createNumArray(input, args[0])); + return typeof(val) === 'number' ? val.toString() : ""; + }, + + + computeDiv: function(input, args) { + let val = Arithmetic._div(Arithmetic._createNumArray(input, args[0])); + return typeof(val) === 'number' ? val.toString() : ""; + }, + + + computeMean: function(input, args) { + let val = Arithmetic._mean(Arithmetic._createNumArray(input, args[0])); + return typeof(val) === 'number' ? val.toString() : ""; + }, + + + computeMedian: function(input, args) { + let val = Arithmetic._median(Arithmetic._createNumArray(input, args[0])); + return typeof(val) === 'number' ? val.toString() : ""; + }, + + + computeStdDev: function(input, args) { + let val = Arithmetic._stdDev(Arithmetic._createNumArray(input, args[0])); + return typeof(val) === 'number' ? val.toString() : ""; + }, + + + _createNumArray: function(input, delim) { + const delim = Utils.charRep[delim || "Space"]; let splitNumbers = input.split(delim), numbers = [], num; @@ -53,11 +96,7 @@ const Arithmetic = { numbers.push(num); } } - num = Arithmetic.opMap[args[1] || "Sum"](numbers); - if (num === null) { - return ""; - } - return num.toString(); + return numbers; }, @@ -71,8 +110,6 @@ const Arithmetic = { _sum: function(data) { if (data.length > 0) { return data.reduce((acc, curr) => acc + curr); - } else { - return null; } }, @@ -86,8 +123,6 @@ const Arithmetic = { _sub: function(data) { if (data.length > 0) { return data.reduce((acc, curr) => acc - curr); - } else { - return null; } }, @@ -98,11 +133,9 @@ const Arithmetic = { * @param {number[]} data * @returns {number} */ - _multiply: function(data) { + _multi: function(data) { if (data.length > 0) { return data.reduce((acc, curr) => acc * curr); - } else { - return null; } }, @@ -113,11 +146,9 @@ const Arithmetic = { * @param {number[]} data * @returns {number} */ - _divide: function(data) { + _div: function(data) { if (data.length > 0) { return data.reduce((acc, curr) => acc / curr); - } else { - return null; } }, @@ -131,8 +162,6 @@ const Arithmetic = { _mean: function(data) { if (data.length > 0) { return Arithmetic._sum(data) / data.length; - } else { - return null; } }, @@ -172,39 +201,8 @@ const Arithmetic = { devSum += (data[i] - avg) ** 2; } return Math.sqrt(devSum / data.length); - } else { - return null; } }, - - /** - * A mapping of operation names to their function. - * - * @constant - */ - opMap: { - "Sum": function(numArray) { - return Arithmetic._sum(numArray); - }, - "Sub": function(numArray) { - return Arithmetic._sub(numArray); - }, - "Multiply": function(numArray) { - return Arithmetic._multiply(numArray); - }, - "Divide": function(numArray) { - return Arithmetic._divide(numArray); - }, - "Mean": function(numArray) { - return Arithmetic._mean(numArray); - }, - "Median": function(numArray) { - return Arithmetic._median(numArray); - }, - "Standard Deviation": function (numArray) { - return Arithmetic._stdDev(numArray); - }, - }, }; export default Arithmetic;