diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 09ee8d15..95f14f85 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -160,6 +160,7 @@ "AND", "ADD", "SUB", + "Basic Arithmetic", "Sum", "Subtract", "Multiply", diff --git a/src/core/operations/BasicArithmetic.mjs b/src/core/operations/BasicArithmetic.mjs new file mode 100644 index 00000000..d127ef26 --- /dev/null +++ b/src/core/operations/BasicArithmetic.mjs @@ -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.

e.g. 1+2-1 becomes 2"; + 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; \ No newline at end of file