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",
ops: [
"Arithmetic",
"Diff",
"Remove whitespace",
"Remove null bytes",

View File

@ -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
}

View File

@ -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,
/*

View File

@ -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;