+ Adding Basic Arithmetic Operation

This commit is contained in:
Dermott, Scott J 2021-11-25 17:57:03 +00:00
parent ae1b12c120
commit e4607e97fc
2 changed files with 47 additions and 0 deletions

View file

@ -160,6 +160,7 @@
"AND",
"ADD",
"SUB",
"Basic Arithmetic",
"Sum",
"Subtract",
"Multiply",

View file

@ -0,0 +1,46 @@
/**
* @author scottdermott [scottdermott@outlook.com]
* @copyright Crown Copyright 2021
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
/**
* Basic Arithmetic operation
*/
class BasicArithmetic extends Operation {
/**
* BasicArithmetic constructor
*/
constructor() {
super();
this.name = "BasicArithmetic";
this.module = "Default";
this.description = "Evalutes Basic Arithmetic. <br><br>e.g. <code>1+2-1</code> becomes <code>2</code>";
this.infoURL = "";
this.inputType = "string";
this.outputType = "number";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
run(input, args) {
if (parseInt(input, 10).toString().length === input.length) {
return parseInt(input, 10);
} else {
return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || [])
.reduce(function (sum, value) {
return parseFloat(sum) + parseFloat(value);
});
}
}
}
export default BasicArithmetic;