CyberChef/src/core/operations/Arithmetic.js

253 lines
6.1 KiB
JavaScript
Raw Normal View History

2017-12-17 21:19:10 +01:00
import Utils from "../Utils.js";
2017-12-21 16:12:06 +01:00
2017-12-17 21:19:10 +01:00
/**
* Math operations on numbers.
*
* @author bwhitn [brian.m.whitney@outlook.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*/
const Arithmetic = {
/**
* @constant
* @default
*/
2017-12-21 16:12:06 +01:00
DELIM_OPTIONS: ["Line feed", "Space", "Comma", "Semi-colon", "Colon", "CRLF"],
2017-12-17 21:19:10 +01:00
2017-12-21 06:19:47 +01:00
2017-12-17 21:19:10 +01:00
/**
* Splits a string based on a delimiter and calculates the sum of numbers.
2017-12-17 21:19:10 +01:00
*
* @param {string} input
* @param {Object[]} args
2017-12-21 16:12:06 +01:00
* @returns {number}
2017-12-17 21:19:10 +01:00
*/
runSum: function(input, args) {
2017-12-21 06:19:47 +01:00
const val = Arithmetic._sum(Arithmetic._createNumArray(input, args[0]));
2017-12-21 16:12:06 +01:00
return typeof(val) === "number" ? val : NaN;
2017-12-21 06:19:47 +01:00
},
/**
* Splits a string based on a delimiter and subtracts all the numbers.
*
* @param {string} input
* @param {Object[]} args
2017-12-21 16:12:06 +01:00
* @returns {number}
*/
runSub: function(input, args) {
2017-12-21 06:19:47 +01:00
let val = Arithmetic._sub(Arithmetic._createNumArray(input, args[0]));
2017-12-21 16:12:06 +01:00
return typeof(val) === "number" ? val : NaN;
2017-12-21 06:19:47 +01:00
},
/**
* Splits a string based on a delimiter and multiplies the numbers.
*
* @param {string} input
* @param {Object[]} args
2017-12-21 16:12:06 +01:00
* @returns {number}
*/
runMulti: function(input, args) {
2017-12-21 06:19:47 +01:00
let val = Arithmetic._multi(Arithmetic._createNumArray(input, args[0]));
2017-12-21 16:12:06 +01:00
return typeof(val) === "number" ? val : NaN;
2017-12-21 06:19:47 +01:00
},
/**
* Splits a string based on a delimiter and divides the numbers.
*
* @param {string} input
* @param {Object[]} args
2017-12-21 16:12:06 +01:00
* @returns {number}
*/
runDiv: function(input, args) {
2017-12-21 06:19:47 +01:00
let val = Arithmetic._div(Arithmetic._createNumArray(input, args[0]));
2017-12-21 16:12:06 +01:00
return typeof(val) === "number" ? val : NaN;
2017-12-21 06:19:47 +01:00
},
/**
* Splits a string based on a delimiter and computes the mean (average).
*
* @param {string} input
* @param {Object[]} args
2017-12-21 16:12:06 +01:00
* @returns {number}
*/
runMean: function(input, args) {
2017-12-21 06:19:47 +01:00
let val = Arithmetic._mean(Arithmetic._createNumArray(input, args[0]));
2017-12-21 16:12:06 +01:00
return typeof(val) === "number" ? val : NaN;
2017-12-21 06:19:47 +01:00
},
/**
* Splits a string based on a delimiter and finds the median.
*
* @param {string} input
* @param {Object[]} args
2017-12-21 16:12:06 +01:00
* @returns {number}
*/
runMedian: function(input, args) {
2017-12-21 06:19:47 +01:00
let val = Arithmetic._median(Arithmetic._createNumArray(input, args[0]));
2017-12-21 16:12:06 +01:00
return typeof(val) === "number" ? val : NaN;
2017-12-21 06:19:47 +01:00
},
/**
* splits a string based on a delimiter and computes the standard deviation.
*
* @param {string} input
* @param {Object[]} args
2017-12-21 16:12:06 +01:00
* @returns {number}
*/
runStdDev: function(input, args) {
2017-12-21 06:19:47 +01:00
let val = Arithmetic._stdDev(Arithmetic._createNumArray(input, args[0]));
2017-12-21 16:12:06 +01:00
return typeof(val) === "number" ? val : NaN;
2017-12-21 06:19:47 +01:00
},
/**
* Converts a string array to a number array.
*
2017-12-21 16:12:06 +01:00
* @private
* @param {string[]} input
* @param {string} delim
* @returns {number[]}
*/
2017-12-21 06:19:47 +01:00
_createNumArray: function(input, delim) {
delim = Utils.charRep[delim || "Space"];
2017-12-17 21:19:10 +01:00
let splitNumbers = input.split(delim),
numbers = [],
2017-12-17 21:20:58 +01:00
num;
2017-12-21 16:12:06 +01:00
2017-12-17 21:20:58 +01:00
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
}
}
2017-12-21 06:19:47 +01:00
return numbers;
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);
2017-12-17 21:19:10 +01:00
}
},
2017-12-21 16:12:06 +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
}
},
2017-12-21 16:12:06 +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-21 06:19:47 +01:00
_multi: 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
}
},
2017-12-21 16:12:06 +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-21 06:19:47 +01:00
_div: 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
}
},
2017-12-21 16:12:06 +01:00
2017-12-17 21:20:58 +01:00
/**
* Computes mean of a number array and returns the value.
2017-12-17 21:20:58 +01:00
*
* @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;
}
},
2017-12-21 16:12:06 +01:00
2017-12-18 03:57:09 +01:00
/**
* Computes median of a number array and returns the value.
2017-12-18 03:57:09 +01:00
*
* @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)];
}
},
2017-12-21 16:12:06 +01:00
2017-12-18 03:57:09 +01:00
/**
* Computes standard deviation of a number array and returns the value.
2017-12-18 03:57:09 +01:00
*
* @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);
2017-12-17 21:19:10 +01:00
}
},
};
export default Arithmetic;