export Arithmetic funcs individually. Use existing Delim
This commit is contained in:
parent
f79dd29ed3
commit
6ddc1b1c9c
@ -8,29 +8,15 @@
|
||||
import Utils from "../Utils";
|
||||
import BigNumber from "bignumber.js";
|
||||
|
||||
/**
|
||||
* Arithmetic functions used by Sum, Multiply, Divide, Subtract (etc)
|
||||
* functions.
|
||||
*/
|
||||
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) {
|
||||
export function createNumArray(input, delim) {
|
||||
delim = Utils.charRep(delim || "Space");
|
||||
const splitNumbers = input.split(delim);
|
||||
const numbers = [];
|
||||
@ -53,11 +39,10 @@ class Arithmetic {
|
||||
/**
|
||||
* Adds an array of numbers and returns the value.
|
||||
*
|
||||
* @private
|
||||
* @param {BigNumber[]} data
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
static _sum(data) {
|
||||
export function sum(data) {
|
||||
if (data.length > 0) {
|
||||
return data.reduce((acc, curr) => acc.plus(curr));
|
||||
}
|
||||
@ -67,11 +52,10 @@ class Arithmetic {
|
||||
/**
|
||||
* Subtracts an array of numbers and returns the value.
|
||||
*
|
||||
* @private
|
||||
* @param {BigNumber[]} data
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
static _sub(data) {
|
||||
export function sub(data) {
|
||||
if (data.length > 0) {
|
||||
return data.reduce((acc, curr) => acc.minus(curr));
|
||||
}
|
||||
@ -81,11 +65,10 @@ class Arithmetic {
|
||||
/**
|
||||
* Multiplies an array of numbers and returns the value.
|
||||
*
|
||||
* @private
|
||||
* @param {BigNumber[]} data
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
static _multi(data) {
|
||||
export function multi(data) {
|
||||
if (data.length > 0) {
|
||||
return data.reduce((acc, curr) => acc.times(curr));
|
||||
}
|
||||
@ -95,11 +78,10 @@ class Arithmetic {
|
||||
/**
|
||||
* Divides an array of numbers and returns the value.
|
||||
*
|
||||
* @private
|
||||
* @param {BigNumber[]} data
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
static _div(data) {
|
||||
export function div(data) {
|
||||
if (data.length > 0) {
|
||||
return data.reduce((acc, curr) => acc.div(curr));
|
||||
}
|
||||
@ -109,13 +91,12 @@ class Arithmetic {
|
||||
/**
|
||||
* Computes mean of a number array and returns the value.
|
||||
*
|
||||
* @private
|
||||
* @param {BigNumber[]} data
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
static _mean(data) {
|
||||
export function mean(data) {
|
||||
if (data.length > 0) {
|
||||
return Arithmetic._sum(data).div(data.length);
|
||||
return sum(data).div(data.length);
|
||||
}
|
||||
}
|
||||
|
||||
@ -123,18 +104,17 @@ class Arithmetic {
|
||||
/**
|
||||
* Computes median of a number array and returns the value.
|
||||
*
|
||||
* @private
|
||||
* @param {BigNumber[]} data
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
static _median(data) {
|
||||
export function 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]);
|
||||
return mean([first, second]);
|
||||
} else {
|
||||
return data[Math.floor(data.length / 2)];
|
||||
}
|
||||
@ -144,13 +124,12 @@ class Arithmetic {
|
||||
/**
|
||||
* Computes standard deviation of a number array and returns the value.
|
||||
*
|
||||
* @private
|
||||
* @param {BigNumber[]} data
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
static _stdDev(data) {
|
||||
export function stdDev(data) {
|
||||
if (data.length > 0) {
|
||||
const avg = Arithmetic._mean(data);
|
||||
const avg = mean(data);
|
||||
let devSum = new BigNumber(0);
|
||||
data.map((datum) => {
|
||||
devSum = devSum.plus(datum.minus(avg).pow(2));
|
||||
@ -158,6 +137,3 @@ class Arithmetic {
|
||||
return devSum.div(data.length).sqrt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Arithmetic;
|
||||
|
@ -5,9 +5,11 @@
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Arithmetic from "../lib/Arithmetic";
|
||||
import BigNumber from "bignumber.js";
|
||||
import Operation from "../Operation";
|
||||
import { div, createNumArray } from "../lib/Arithmetic";
|
||||
import { DELIM_OPTIONS } from "../lib/Delim";
|
||||
|
||||
|
||||
/**
|
||||
* Divide operation
|
||||
@ -29,7 +31,7 @@ class Divide extends Operation {
|
||||
{
|
||||
"name": "Delimiter",
|
||||
"type": "option",
|
||||
"value": Arithmetic.DELIM_OPTIONS,
|
||||
"value": DELIM_OPTIONS,
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -40,7 +42,7 @@ class Divide extends Operation {
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
run(input, args) {
|
||||
const val = Arithmetic._div(Arithmetic._createNumArray(input, args[0]));
|
||||
const val = div(createNumArray(input, args[0]));
|
||||
return val instanceof BigNumber ? val : new BigNumber(NaN);
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,8 @@
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Arithmetic from "../lib/Arithmetic";
|
||||
import { mean, createNumArray } from "../lib/Arithmetic";
|
||||
import { DELIM_OPTIONS } from "../lib/Delim";
|
||||
import BigNumber from "bignumber.js";
|
||||
|
||||
/**
|
||||
@ -29,7 +30,7 @@ class Mean extends Operation {
|
||||
{
|
||||
"name": "Delimiter",
|
||||
"type": "option",
|
||||
"value": Arithmetic.DELIM_OPTIONS,
|
||||
"value": DELIM_OPTIONS,
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -40,7 +41,7 @@ class Mean extends Operation {
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
run(input, args) {
|
||||
const val = Arithmetic._mean(Arithmetic._createNumArray(input, args[0]));
|
||||
const val = mean(createNumArray(input, args[0]));
|
||||
return val instanceof BigNumber ? val : new BigNumber(NaN);
|
||||
}
|
||||
|
||||
|
@ -5,9 +5,10 @@
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Arithmetic from "../lib/Arithmetic";
|
||||
import BigNumber from "bignumber.js";
|
||||
import Operation from "../Operation";
|
||||
import { median, createNumArray } from "../lib/Arithmetic";
|
||||
import { DELIM_OPTIONS } from "../lib/Delim";
|
||||
|
||||
/**
|
||||
* Median operation
|
||||
@ -29,7 +30,7 @@ class Median extends Operation {
|
||||
{
|
||||
"name": "Delimiter",
|
||||
"type": "option",
|
||||
"value": Arithmetic.DELIM_OPTIONS,
|
||||
"value": DELIM_OPTIONS,
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -40,7 +41,7 @@ class Median extends Operation {
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
run(input, args) {
|
||||
const val = Arithmetic._median(Arithmetic._createNumArray(input, args[0]));
|
||||
const val = median(createNumArray(input, args[0]));
|
||||
return val instanceof BigNumber ? val : new BigNumber(NaN);
|
||||
}
|
||||
|
||||
|
@ -5,9 +5,11 @@
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Arithmetic from "../lib/Arithmetic";
|
||||
import BigNumber from "bignumber.js";
|
||||
import Operation from "../Operation";
|
||||
import { multi, createNumArray } from "../lib/Arithmetic";
|
||||
import { DELIM_OPTIONS } from "../lib/Delim";
|
||||
|
||||
|
||||
/**
|
||||
* Multiply operation
|
||||
@ -29,7 +31,7 @@ class Multiply extends Operation {
|
||||
{
|
||||
"name": "Delimiter",
|
||||
"type": "option",
|
||||
"value": Arithmetic.DELIM_OPTIONS,
|
||||
"value": DELIM_OPTIONS,
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -40,7 +42,7 @@ class Multiply extends Operation {
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
run(input, args) {
|
||||
const val = Arithmetic._multi(Arithmetic._createNumArray(input, args[0]));
|
||||
const val = multi(createNumArray(input, args[0]));
|
||||
return val instanceof BigNumber ? val : new BigNumber(NaN);
|
||||
}
|
||||
|
||||
|
@ -5,9 +5,11 @@
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Arithmetic from "../lib/Arithmetic";
|
||||
import BigNumber from "bignumber.js";
|
||||
import Operation from "../Operation";
|
||||
import { stdDev, createNumArray } from "../lib/Arithmetic";
|
||||
import { DELIM_OPTIONS } from "../lib/Delim";
|
||||
|
||||
|
||||
/**
|
||||
* Standard Deviation operation
|
||||
@ -29,7 +31,7 @@ class StandardDeviation extends Operation {
|
||||
{
|
||||
"name": "Delimiter",
|
||||
"type": "option",
|
||||
"value": Arithmetic.DELIM_OPTIONS,
|
||||
"value": DELIM_OPTIONS,
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -40,7 +42,7 @@ class StandardDeviation extends Operation {
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
run(input, args) {
|
||||
const val = Arithmetic._stdDev(Arithmetic._createNumArray(input, args[0]));
|
||||
const val = stdDev(createNumArray(input, args[0]));
|
||||
return val instanceof BigNumber ? val : new BigNumber(NaN);
|
||||
|
||||
}
|
||||
|
@ -5,9 +5,11 @@
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Arithmetic from "../lib/Arithmetic";
|
||||
import BigNumber from "bignumber.js";
|
||||
import Operation from "../Operation";
|
||||
import { sub, createNumArray } from "../lib/Arithmetic";
|
||||
import { DELIM_OPTIONS } from "../lib/Delim";
|
||||
|
||||
|
||||
/**
|
||||
* Subtract operation
|
||||
@ -29,7 +31,7 @@ class Subtract extends Operation {
|
||||
{
|
||||
"name": "Delimiter",
|
||||
"type": "option",
|
||||
"value": Arithmetic.DELIM_OPTIONS,
|
||||
"value": DELIM_OPTIONS,
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -40,7 +42,7 @@ class Subtract extends Operation {
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
run(input, args) {
|
||||
const val = Arithmetic._sub(Arithmetic._createNumArray(input, args[0]));
|
||||
const val = sub(createNumArray(input, args[0]));
|
||||
return val instanceof BigNumber ? val : new BigNumber(NaN);
|
||||
}
|
||||
|
||||
|
@ -4,9 +4,11 @@
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Arithmetic from "../lib/Arithmetic";
|
||||
import BigNumber from "bignumber.js";
|
||||
import Operation from "../Operation";
|
||||
import { sum, createNumArray } from "../lib/Arithmetic";
|
||||
import { DELIM_OPTIONS } from "../lib/Delim";
|
||||
|
||||
|
||||
/**
|
||||
* Sum operation
|
||||
@ -28,7 +30,7 @@ class Sum extends Operation {
|
||||
{
|
||||
"name": "Delimiter",
|
||||
"type": "option",
|
||||
"value": Arithmetic.DELIM_OPTIONS,
|
||||
"value": DELIM_OPTIONS,
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -39,7 +41,7 @@ class Sum extends Operation {
|
||||
* @returns {BigNumber}
|
||||
*/
|
||||
run(input, args) {
|
||||
const val = Arithmetic._sum(Arithmetic._createNumArray(input, args[0]));
|
||||
const val = sum(createNumArray(input, args[0]));
|
||||
return val instanceof BigNumber ? val : new BigNumber(NaN);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user