From e4607e97fc04b6cccbeb0be94254fe7a9eed6668 Mon Sep 17 00:00:00 2001 From: "Dermott, Scott J" Date: Thu, 25 Nov 2021 17:57:03 +0000 Subject: [PATCH 1/7] + Adding Basic Arithmetic Operation --- src/core/config/Categories.json | 1 + src/core/operations/BasicArithmetic.mjs | 46 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/core/operations/BasicArithmetic.mjs 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 From 5f40ee80769f6f72fc9637cd1b399fe9e65ce418 Mon Sep 17 00:00:00 2001 From: "Dermott, Scott J" Date: Thu, 25 Nov 2021 18:32:45 +0000 Subject: [PATCH 2/7] * Fix lint issues --- src/core/operations/BasicArithmetic.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/operations/BasicArithmetic.mjs b/src/core/operations/BasicArithmetic.mjs index d127ef26..6af45dd1 100644 --- a/src/core/operations/BasicArithmetic.mjs +++ b/src/core/operations/BasicArithmetic.mjs @@ -43,4 +43,4 @@ class BasicArithmetic extends Operation { } -export default BasicArithmetic; \ No newline at end of file +export default BasicArithmetic; From 1b9f0cda980b0b84af886c15d3c0380cb8bda45f Mon Sep 17 00:00:00 2001 From: "Dermott, Scott J" Date: Thu, 25 Nov 2021 20:17:54 +0000 Subject: [PATCH 3/7] + Adding unit tests and fix for empty value --- src/core/operations/BasicArithmetic.mjs | 19 ++++-- tests/operations/tests/BasicArithmetic.mjs | 68 ++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 tests/operations/tests/BasicArithmetic.mjs diff --git a/src/core/operations/BasicArithmetic.mjs b/src/core/operations/BasicArithmetic.mjs index 6af45dd1..afaafb3c 100644 --- a/src/core/operations/BasicArithmetic.mjs +++ b/src/core/operations/BasicArithmetic.mjs @@ -4,6 +4,7 @@ * @license Apache-2.0 */ +import BigNumber from "bignumber.js"; import Operation from "../Operation.mjs"; /** @@ -31,13 +32,19 @@ class BasicArithmetic extends Operation { * @returns {number} */ run(input, args) { - if (parseInt(input, 10).toString().length === input.length) { - return parseInt(input, 10); + if (input.length >= 1) { + if (parseInt(input, 10).toString().length === input.length) { + const val = parseInt(input, 10); + return BigNumber.isBigNumber(val) ? val : new BigNumber(NaN); + } else { + return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || []) + .reduce(function (sum, value) { + const val = parseFloat(sum) + parseFloat(value); + return BigNumber.isBigNumber(val) ? val : new BigNumber(NaN); + }); + } } else { - return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || []) - .reduce(function (sum, value) { - return parseFloat(sum) + parseFloat(value); - }); + return new BigNumber(NaN); } } diff --git a/tests/operations/tests/BasicArithmetic.mjs b/tests/operations/tests/BasicArithmetic.mjs new file mode 100644 index 00000000..964a5d30 --- /dev/null +++ b/tests/operations/tests/BasicArithmetic.mjs @@ -0,0 +1,68 @@ +/** + * @author scottdermott [scottdermott@outlook.com] + * @copyright Crown Copyright 2021 + * @license Apache-2.0 + */ + +/** + * Basic Arithmetic Tests + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "BasicArithmetic: nothing", + input: "", + expectedOutput: "", + recipeConfig: [ + { + op: "BasicArithmetic", + args: [], + }, + ], + }, + { + name: "BasicArithmetic: Addition", + input: "1+2+3+4+5+6+7+8+9+0", + expectedOutput: 45, + recipeConfig: [ + { + op: "BasicArithmetic", + args: [], + }, + ], + }, + { + name: "BasicArithmetic: Subtraction", + input: "100-9-8-7-6-5-4-3-2-1-0", + expectedOutput: 55, + recipeConfig: [ + { + op: "BasicArithmetic", + args: [], + }, + ], + }, + { + name: "BasicArithmetic: Add + Sub", + input: "1+2+3+4+5+6+7+8+9-9-8-7-6-5-4-3-2-1", + expectedOutput: 0, + recipeConfig: [ + { + op: "BasicArithmetic", + args: [], + }, + ], + }, + { + name: "BasicArithmetic: Large number", + input: "999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999", + expectedOutput: 22977, + recipeConfig: [ + { + op: "BasicArithmetic", + args: [], + }, + ], + }, +]); From 3239d5599fb8549d741c275ef6ed7fdbcb90c151 Mon Sep 17 00:00:00 2001 From: "Dermott, Scott J" Date: Thu, 25 Nov 2021 20:26:29 +0000 Subject: [PATCH 4/7] * Fix logic for empty or invalid values --- src/core/operations/BasicArithmetic.mjs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/core/operations/BasicArithmetic.mjs b/src/core/operations/BasicArithmetic.mjs index afaafb3c..3a417c46 100644 --- a/src/core/operations/BasicArithmetic.mjs +++ b/src/core/operations/BasicArithmetic.mjs @@ -4,7 +4,6 @@ * @license Apache-2.0 */ -import BigNumber from "bignumber.js"; import Operation from "../Operation.mjs"; /** @@ -32,19 +31,17 @@ class BasicArithmetic extends Operation { * @returns {number} */ run(input, args) { - if (input.length >= 1) { - if (parseInt(input, 10).toString().length === input.length) { - const val = parseInt(input, 10); - return BigNumber.isBigNumber(val) ? val : new BigNumber(NaN); - } else { - return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || []) - .reduce(function (sum, value) { - const val = parseFloat(sum) + parseFloat(value); - return BigNumber.isBigNumber(val) ? val : new BigNumber(NaN); - }); - } + if (parseInt(input, 10).toString().length === input.length) { + const val = parseInt(input, 10); + return val; + } else if (input.match(/[+-]?([0-9.]+)/g)) { + return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || []) + .reduce(function (sum, value) { + const val = parseFloat(sum) + parseFloat(value); + return val; + }); } else { - return new BigNumber(NaN); + return NaN; } } From 7f260ec86bed0347db68edcb6dd80c24ca6dc255 Mon Sep 17 00:00:00 2001 From: "Dermott, Scott J" Date: Thu, 25 Nov 2021 20:27:12 +0000 Subject: [PATCH 5/7] * Fix logic for empty or invalid values --- src/core/operations/BasicArithmetic.mjs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/core/operations/BasicArithmetic.mjs b/src/core/operations/BasicArithmetic.mjs index 3a417c46..dde83448 100644 --- a/src/core/operations/BasicArithmetic.mjs +++ b/src/core/operations/BasicArithmetic.mjs @@ -32,13 +32,11 @@ class BasicArithmetic extends Operation { */ run(input, args) { if (parseInt(input, 10).toString().length === input.length) { - const val = parseInt(input, 10); - return val; + return parseInt(input, 10); } else if (input.match(/[+-]?([0-9.]+)/g)) { return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || []) .reduce(function (sum, value) { - const val = parseFloat(sum) + parseFloat(value); - return val; + return parseFloat(sum) + parseFloat(value); }); } else { return NaN; From 2374a2cbf08837236f36947ba15271891eef1c73 Mon Sep 17 00:00:00 2001 From: "Dermott, Scott J" Date: Thu, 25 Nov 2021 20:28:38 +0000 Subject: [PATCH 6/7] * Fix test naming --- tests/operations/tests/BasicArithmetic.mjs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/operations/tests/BasicArithmetic.mjs b/tests/operations/tests/BasicArithmetic.mjs index 964a5d30..8ff504e8 100644 --- a/tests/operations/tests/BasicArithmetic.mjs +++ b/tests/operations/tests/BasicArithmetic.mjs @@ -16,7 +16,7 @@ TestRegister.addTests([ expectedOutput: "", recipeConfig: [ { - op: "BasicArithmetic", + op: "Basic Arithmetic", args: [], }, ], @@ -27,7 +27,7 @@ TestRegister.addTests([ expectedOutput: 45, recipeConfig: [ { - op: "BasicArithmetic", + op: "Basic Arithmetic", args: [], }, ], @@ -38,7 +38,7 @@ TestRegister.addTests([ expectedOutput: 55, recipeConfig: [ { - op: "BasicArithmetic", + op: "Basic Arithmetic", args: [], }, ], @@ -49,7 +49,7 @@ TestRegister.addTests([ expectedOutput: 0, recipeConfig: [ { - op: "BasicArithmetic", + op: "Basic Arithmetic", args: [], }, ], @@ -60,7 +60,7 @@ TestRegister.addTests([ expectedOutput: 22977, recipeConfig: [ { - op: "BasicArithmetic", + op: "Basic Arithmetic", args: [], }, ], From 82ed84fc76df61da4c58e80028fcff3c1d88f9f9 Mon Sep 17 00:00:00 2001 From: "Dermott, Scott J" Date: Thu, 25 Nov 2021 20:37:54 +0000 Subject: [PATCH 7/7] * Fix naming of OP with space --- src/core/operations/BasicArithmetic.mjs | 2 +- tests/operations/tests/BasicArithmetic.mjs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/operations/BasicArithmetic.mjs b/src/core/operations/BasicArithmetic.mjs index dde83448..0891cda6 100644 --- a/src/core/operations/BasicArithmetic.mjs +++ b/src/core/operations/BasicArithmetic.mjs @@ -17,7 +17,7 @@ class BasicArithmetic extends Operation { constructor() { super(); - this.name = "BasicArithmetic"; + this.name = "Basic Arithmetic"; this.module = "Default"; this.description = "Evalutes Basic Arithmetic.

e.g. 1+2-1 becomes 2"; this.infoURL = ""; diff --git a/tests/operations/tests/BasicArithmetic.mjs b/tests/operations/tests/BasicArithmetic.mjs index 8ff504e8..d8a42be5 100644 --- a/tests/operations/tests/BasicArithmetic.mjs +++ b/tests/operations/tests/BasicArithmetic.mjs @@ -11,7 +11,7 @@ import TestRegister from "../../lib/TestRegister.mjs"; TestRegister.addTests([ { - name: "BasicArithmetic: nothing", + name: "Basic Arithmetic: nothing", input: "", expectedOutput: "", recipeConfig: [ @@ -22,7 +22,7 @@ TestRegister.addTests([ ], }, { - name: "BasicArithmetic: Addition", + name: "Basic Arithmetic: Addition", input: "1+2+3+4+5+6+7+8+9+0", expectedOutput: 45, recipeConfig: [ @@ -33,7 +33,7 @@ TestRegister.addTests([ ], }, { - name: "BasicArithmetic: Subtraction", + name: "Basic Arithmetic: Subtraction", input: "100-9-8-7-6-5-4-3-2-1-0", expectedOutput: 55, recipeConfig: [ @@ -44,7 +44,7 @@ TestRegister.addTests([ ], }, { - name: "BasicArithmetic: Add + Sub", + name: "Basic Arithmetic: Add + Sub", input: "1+2+3+4+5+6+7+8+9-9-8-7-6-5-4-3-2-1", expectedOutput: 0, recipeConfig: [ @@ -55,7 +55,7 @@ TestRegister.addTests([ ], }, { - name: "BasicArithmetic: Large number", + name: "Basic Arithmetic: Large number", input: "999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999", expectedOutput: 22977, recipeConfig: [