Added additional arithmetic source

This commit is contained in:
bwhitn 2017-12-17 15:20:58 -05:00
parent ae8d1f2178
commit 772f9a806e
4 changed files with 62 additions and 27 deletions

View File

@ -159,6 +159,7 @@ const Categories = [
{ {
name: "Utils", name: "Utils",
ops: [ ops: [
"Arithmetic",
"Diff", "Diff",
"Remove whitespace", "Remove whitespace",
"Remove null bytes", "Remove null bytes",

View File

@ -660,8 +660,8 @@ const OperationConfig = {
"Arithmetic": { "Arithmetic": {
module: "Default", module: "Default",
description: "Conducts mathamatical operations on a list of numbers", description: "Conducts mathamatical operations on a list of numbers",
inputType: "string" inputType: "string",
outputType: "string" outputType: "string",
args: [ args: [
{ {
name: "Delimiter", name: "Delimiter",
@ -669,7 +669,7 @@ const OperationConfig = {
value: Arithmetic.DELIM_OPTIONS value: Arithmetic.DELIM_OPTIONS
}, },
{ {
name: "Operation" name: "Operation",
type: "option", type: "option",
value: Arithmetic.OPERATIONS value: Arithmetic.OPERATIONS
} }

View File

@ -1,4 +1,5 @@
import FlowControl from "../../FlowControl.js"; import FlowControl from "../../FlowControl.js";
import Arithmetic from "../../operations/Arithmetic.js";
import Base from "../../operations/Base.js"; import Base from "../../operations/Base.js";
import Base58 from "../../operations/Base58.js"; import Base58 from "../../operations/Base58.js";
import Base64 from "../../operations/Base64.js"; import Base64 from "../../operations/Base64.js";
@ -155,6 +156,7 @@ OpModules.Default = {
"Conditional Jump": FlowControl.runCondJump, "Conditional Jump": FlowControl.runCondJump,
"Return": FlowControl.runReturn, "Return": FlowControl.runReturn,
"Comment": FlowControl.runComment, "Comment": FlowControl.runComment,
"Arithmetic": Arithmetic.runOp,
/* /*

View File

@ -23,20 +23,6 @@ const Arithmetic = {
*/ */
OPERATIONS: ["Sum", "Sub", "Multiply", "Divide", "Mean", "Median", "Mode"], 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"]; const delim = Utils.charRep[args[0] || "Space"];
let splitNumbers = input.split(delim), let splitNumbers = input.split(delim),
numbers = [], numbers = [],
num, num;
retVal; for (let i = 0; i < splitNumbers.length; i++) {
for (i = 0; i < splitNumbers.length; i++) { if (splitNumbers[i].indexOf(".") >= 0) {
if splitNumbers[i].indexOf(".") {
num = parseFloat(splitNumbers[i].trim()); num = parseFloat(splitNumbers[i].trim());
} else { } else {
num = parseInt(splitNumbers[i].trim()); num = parseInt(splitNumbers[i].trim(), 10);
} }
if (num !== "NaN") { if (num !== "NaN") {
numbers.append(num); 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) { _sum: function(data) {
let total = 0; let total = 0;
for (i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
total += data[i]; total += data[i];
} }
return total; return total;
}, },
/**
* Subtracts an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_sub: function(data) { _sub: function(data) {
let total = 0; let total = 0;
if (data.length > 1) { if (data.length > 1) {
total = data[0]; total = data[0];
for (i = 1; i < data.length; i++) { for (let i = 1; i < data.length; i++) {
total -= data[i]; total -= data[i];
} }
} else { } else {
@ -89,11 +88,18 @@ const Arithmetic = {
return total; return total;
}, },
/**
* Multiplies an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_multiply: function(data) { _multiply: function(data) {
let total = 0; let total = 0;
if (data.length > 1) { if (data.length > 1) {
total = data[0]; total = data[0];
for (i = 1; i < data.length; i++) { for (let i = 1; i < data.length; i++) {
total *= data[i]; total *= data[i];
} }
} else { } else {
@ -102,12 +108,19 @@ const Arithmetic = {
return total; return total;
}, },
/**
* Divides an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_divide: function(data) { _divide: function(data) {
let total = 0; let total = 0;
if (data.length > 1) { if (data.length > 1) {
total = data[0]; total = data[0];
for (i = 1; i < data.length; i++) { for (let i = 1; i < data.length; i++) {
total /= data[i] total /= data[i];
} }
} else { } else {
total = null; total = null;
@ -115,6 +128,13 @@ const Arithmetic = {
return total; return total;
}, },
/**
* Finds the mean of a number array and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_mean: function(data) { _mean: function(data) {
let total = 0; let total = 0;
if (data.length > 1) { if (data.length > 1) {
@ -125,6 +145,18 @@ const Arithmetic = {
return total; 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; export default Arithmetic;