port Sum operation

feature-extract-files
d98762625 2018-05-15 10:07:49 +01:00
parent a7d763287e
commit 4fe34a4839
4 changed files with 2217 additions and 2007 deletions

4009
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,48 @@
/**
* @author bwhitn [brian.m.whitney@outlook.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
import Arithmetic from "./baseClasses/Arithmetic";
import BigNumber from "bignumber.js";
/**
* Sum operation
*/
class Sum extends Operation {
/**
* Sum constructor
*/
constructor() {
super();
this.name = "Sum";
this.module = "Default";
this.description = "Adds together a list of numbers. If an item in the string is not a number it is excluded from the list.<br><br>e.g. <code>0x0a 8 .5</code> becomes <code>18.5</code>";
this.inputType = "string";
this.outputType = "BigNumber";
this.args = [
{
"name": "Delimiter",
"type": "option",
"value": Arithmetic.DELIM_OPTIONS,
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {BigNumber}
*/
run(input, args) {
const val = Arithmetic._sum(Arithmetic._createNumArray(input, args[0]));
return val instanceof BigNumber ? val : new BigNumber(NaN);
}
}
export default Sum;

View File

@ -0,0 +1,161 @@
/**
* @author d98762625 [d98762625@gmailcom]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Utils from "../../Utils";
import BigNumber from "bignumber.js";
/**
*
*/
class Arithmetic {
/**
* @constant
* @default
*/
static get DELIM_OPTIONS() {
return ["Line feed", "Space", "Comma", "Semi-colon", "Colon", "CRLF"];
}
/**
* Converts a string array to a number array.
*
* @private
* @param {string[]} input
* @param {string} delim
* @returns {BigNumber[]}
*/
static _createNumArray(input, delim) {
delim = Utils.charRep(delim || "Space");
const splitNumbers = input.split(delim);
const numbers = [];
let num;
splitNumbers.map((number) => {
try {
num = BigNumber(number.trim());
if (!num.isNaN()) {
numbers.push(num);
}
} catch (err) {
// This line is not a valid number
}
});
return numbers;
}
/**
* Adds an array of numbers and returns the value.
*
* @private
* @param {BigNumber[]} data
* @returns {BigNumber}
*/
static _sum(data) {
if (data.length > 0) {
return data.reduce((acc, curr) => acc.plus(curr));
}
}
/**
* Subtracts an array of numbers and returns the value.
*
* @private
* @param {BigNumber[]} data
* @returns {BigNumber}
*/
static _sub(data) {
if (data.length > 0) {
return data.reduce((acc, curr) => acc.minus(curr));
}
}
/**
* Multiplies an array of numbers and returns the value.
*
* @private
* @param {BigNumber[]} data
* @returns {BigNumber}
*/
static _multi(data) {
if (data.length > 0) {
return data.reduce((acc, curr) => acc.times(curr));
}
}
/**
* Divides an array of numbers and returns the value.
*
* @private
* @param {BigNumber[]} data
* @returns {BigNumber}
*/
static _div(data) {
if (data.length > 0) {
return data.reduce((acc, curr) => acc.div(curr));
}
}
/**
* Computes mean of a number array and returns the value.
*
* @private
* @param {BigNumber[]} data
* @returns {BigNumber}
*/
static _mean(data) {
if (data.length > 0) {
return Arithmetic._sum(data).div(data.length);
}
}
/**
* Computes median of a number array and returns the value.
*
* @private
* @param {BigNumber[]} data
* @returns {BigNumber}
*/
static _median(data) {
if ((data.length % 2) === 0 && data.length > 0) {
data.sort(function(a, b){
return a.minus(b);
});
const first = data[Math.floor(data.length / 2)];
const second = data[Math.floor(data.length / 2) - 1];
return Arithmetic._mean([first, second]);
} else {
return data[Math.floor(data.length / 2)];
}
}
/**
* Computes standard deviation of a number array and returns the value.
*
* @private
* @param {BigNumber[]} data
* @returns {BigNumber}
*/
static _stdDev(data) {
if (data.length > 0) {
const avg = Arithmetic._mean(data);
let devSum = new BigNumber(0);
data.map((datum) => {
devSum = devSum.plus(datum.minus(avg).pow(2));
});
return devSum.div(data.length).sqrt();
}
}
}
export default Arithmetic;

View File

@ -1,3 +1,9 @@
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Utils from "../Utils.js";
import BigNumber from "bignumber.js";