CyberChef/src/core/operations/Arithmetic.js

211 lines
4.9 KiB
JavaScript
Raw Normal View History

2017-12-17 21:19:10 +01:00
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
*/
2017-12-18 03:57:09 +01:00
OPERATIONS: [
"Sum",
"Sub",
"Multiply",
"Divide",
"Mean",
"Median",
"Standard Deviation"
],
2017-12-17 21:19:10 +01:00
/**
*
*
* @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 = [],
2017-12-17 21:20:58 +01:00
num;
for (let i = 0; i < splitNumbers.length; i++) {
if (splitNumbers[i].indexOf(".") >= 0) {
2017-12-17 21:19:10 +01:00
num = parseFloat(splitNumbers[i].trim());
} else {
2017-12-18 03:57:09 +01:00
num = parseInt(splitNumbers[i].trim(), 0);
2017-12-17 21:19:10 +01:00
}
2017-12-18 03:57:09 +01:00
if (!isNaN(num)) {
numbers.push(num);
2017-12-17 21:19:10 +01:00
}
}
num = Arithmetic.opMap[args[1] || "Sum"](numbers);
2017-12-18 03:57:09 +01:00
if (num === null) {
return "";
2017-12-17 21:19:10 +01:00
}
2017-12-18 04:15:13 +01:00
return num.toString();
2017-12-17 21:19:10 +01:00
},
2017-12-17 21:20:58 +01:00
/**
* Adds an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
2017-12-17 21:19:10 +01:00
_sum: function(data) {
2017-12-18 03:57:09 +01:00
if (data.length > 0) {
return data.reduce((acc, curr) => acc + curr);
} else {
return null;
2017-12-17 21:19:10 +01:00
}
},
2017-12-17 21:20:58 +01:00
/**
* Subtracts an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
2017-12-17 21:19:10 +01:00
_sub: function(data) {
2017-12-18 03:57:09 +01:00
if (data.length > 0) {
return data.reduce((acc, curr) => acc - curr);
2017-12-17 21:19:10 +01:00
} else {
2017-12-18 03:57:09 +01:00
return null;
2017-12-17 21:19:10 +01:00
}
},
2017-12-17 21:20:58 +01:00
/**
* Multiplies an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
2017-12-17 21:19:10 +01:00
_multiply: function(data) {
2017-12-18 03:57:09 +01:00
if (data.length > 0) {
return data.reduce((acc, curr) => acc * curr);
2017-12-17 21:19:10 +01:00
} else {
2017-12-18 03:57:09 +01:00
return null;
2017-12-17 21:19:10 +01:00
}
},
2017-12-17 21:20:58 +01:00
/**
* Divides an array of numbers and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
2017-12-17 21:19:10 +01:00
_divide: function(data) {
2017-12-18 03:57:09 +01:00
if (data.length > 0) {
return data.reduce((acc, curr) => acc / curr);
2017-12-17 21:19:10 +01:00
} else {
2017-12-18 03:57:09 +01:00
return null;
2017-12-17 21:19:10 +01:00
}
},
2017-12-17 21:20:58 +01:00
/**
* Finds the mean of a number array and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
2017-12-17 21:19:10 +01:00
_mean: function(data) {
2017-12-18 03:57:09 +01:00
if (data.length > 0) {
return Arithmetic._sum(data) / data.length;
} else {
return null;
}
},
/**
* Finds the median of a number array and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_median: function (data) {
if ((data.length % 2) === 0) {
let first, second;
data.sort(function(a, b){
return a - b;
});
first = data[Math.floor(data.length / 2)];
second = data[Math.floor(data.length / 2) - 1];
return Arithmetic._mean([first, second]);
2017-12-17 21:19:10 +01:00
} else {
2017-12-18 03:57:09 +01:00
return data[Math.floor(data.length / 2)];
}
},
/**
* Finds the standard deviation of a number array and returns the value.
*
* @private
* @param {number[]} data
* @returns {number}
*/
_stdDev: function (data) {
if (data.length > 0) {
let avg = Arithmetic._mean(data);
let devSum = 0;
for (let i = 0; i < data.length; i++) {
devSum += (data[i] - avg) ** 2;
}
return Math.sqrt(devSum / data.length);
} else {
return null;
2017-12-17 21:19:10 +01:00
}
},
2017-12-17 21:20:58 +01:00
/**
* A mapping of operation names to their function.
2017-12-18 03:57:09 +01:00
*
2017-12-17 21:20:58 +01:00
* @constant
*/
opMap: {
2017-12-18 03:57:09 +01:00
"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);
},
2017-12-17 21:20:58 +01:00
},
2017-12-17 21:19:10 +01:00
};
export default Arithmetic;