From 5f93c667a2c27d0a0280431d2316da65690815c5 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Fri, 6 Apr 2018 16:27:24 +0100 Subject: [PATCH] Pull SetUnion into its own operation --- src/core/config/OperationConfig.json | 4 +- src/core/config/modules/Default.js | 1 - src/core/operations/SetUnion.mjs | 54 ++++- test/index.mjs | 1 + test/tests/operations/SetOperations.js | 298 ------------------------- test/tests/operations/SetUnion.mjs | 298 +++++++++++++++++++++++++ 6 files changed, 350 insertions(+), 306 deletions(-) delete mode 100644 test/tests/operations/SetOperations.js create mode 100644 test/tests/operations/SetUnion.mjs diff --git a/src/core/config/OperationConfig.json b/src/core/config/OperationConfig.json index dfb0cc81..6e0e27a9 100644 --- a/src/core/config/OperationConfig.json +++ b/src/core/config/OperationConfig.json @@ -256,9 +256,9 @@ "flowControl": false, "args": [ { - "name": "Sample delimiter", + "name": "Samples delimiter", "type": "binaryString", - "value": "\n\n" + "value": "\\n\\n" }, { "name": "Item delimiter", diff --git a/src/core/config/modules/Default.js b/src/core/config/modules/Default.js index 9b5762cf..d59b7b21 100644 --- a/src/core/config/modules/Default.js +++ b/src/core/config/modules/Default.js @@ -164,7 +164,6 @@ OpModules.Default = { "Windows Filetime to UNIX Timestamp": Filetime.runFromFiletimeToUnix, "UNIX Timestamp to Windows Filetime": Filetime.runToFiletimeFromUnix, "XKCD Random Number": XKCD.runRandomNumber, - "Set Operations": SetOps.runSetOperation.bind(SetOps), /* diff --git a/src/core/operations/SetUnion.mjs b/src/core/operations/SetUnion.mjs index 99aabc0a..bde0cae7 100644 --- a/src/core/operations/SetUnion.mjs +++ b/src/core/operations/SetUnion.mjs @@ -1,19 +1,63 @@ -import SetOp from "./SetOps"; +import Utils from "../Utils"; +import Operation from "../Operation"; /** - * + * Set Union operation */ -class SetUnion extends SetOp { +class SetUnion extends Operation { /** - * + * Set Union constructor */ constructor() { super(); - this.setOp = this.runUnion; this.name = "Set Union"; + this.module = "Default"; this.description = "Get the union of two sets"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + name: "Sample delimiter", + type: "binaryString", + value: Utils.escapeHtml("\\n\\n") + }, + { + name: "Item delimiter", + type: "binaryString", + value: "," + }, + ]; + } + + /** + * Validate input length + * @param {Object[]} sets + * @throws {Error} if not two sets + */ + validateSampleNumbers(sets) { + if (!sets || (sets.length !== 2)) { + throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?"; + } + } + + /** + * Run the union operation + * @param input + * @param args + */ + run(input, args) { + [this.sampleDelim, this.itemDelimiter] = args; + const sets = input.split(this.sampleDelim); + + try { + this.validateSampleNumbers(sets); + } catch (e) { + return e; + } + + return Utils.escapeHtml(this.runUnion(...sets.map(s => s.split(this.itemDelimiter)))); } /** diff --git a/test/index.mjs b/test/index.mjs index bd3f27ba..08942219 100644 --- a/test/index.mjs +++ b/test/index.mjs @@ -47,6 +47,7 @@ import "./tests/operations/Base64"; // import "./tests/operations/Regex.js"; // import "./tests/operations/StrUtils.js"; // import "./tests/operations/SeqUtils.js"; +import "./tests/operations/SetUnion"; let allTestsPassing = true; diff --git a/test/tests/operations/SetOperations.js b/test/tests/operations/SetOperations.js deleted file mode 100644 index 35e46f9e..00000000 --- a/test/tests/operations/SetOperations.js +++ /dev/null @@ -1,298 +0,0 @@ -/** - * Set Operations tests. - * - * @author d98762625 - * - * @copyright Crown Copyright 2017 - * @license Apache-2.0 - */ -import TestRegister from "../../TestRegister.js"; - -TestRegister.addTests([ - { - name: "Set Operations: Nothing", - input: "\n\n", - expectedOutput: "", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Union"], - }, - ], - }, - { - name: "Set Operations: Union", - input: "1 2 3 4 5\n\n3 4 5 6 7", - expectedOutput: "1 2 3 4 5 6 7", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Union"], - }, - ], - }, - { - name: "Set Operations: Union: invalid sample number", - input: "1 2 3 4 5\n\n3 4 5 6 7\n\n1", - expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Union"], - }, - ], - }, - { - name: "Set Operations: Union: item delimiter", - input: "1,2,3,4,5\n\n3,4,5,6,7", - expectedOutput: "1,2,3,4,5,6,7", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", ",", "Union"], - }, - ], - }, - { - name: "Set Operations: Union: sample delimiter", - input: "1 2 3 4 5whatever3 4 5 6 7", - expectedOutput: "1 2 3 4 5 6 7", - recipeConfig: [ - { - op: "Set Operations", - args: ["whatever", " ", "Union"], - }, - ], - }, - { - name: "Set Operations: Intersection", - input: "1 2 3 4 5\n\n3 4 5 6 7", - expectedOutput: "3 4 5", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Intersection"], - }, - ], - }, - { - name: "Set Operations: Intersection: only one set", - input: "1 2 3 4 5 6 7 8", - expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Intersection"], - }, - ], - }, - { - name: "Set Operations: Intersection: item delimiter", - input: "1-2-3-4-5\n\n3-4-5-6-7", - expectedOutput: "3-4-5", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", "-", "Intersection"], - }, - ], - }, - { - name: "Set Operations: Intersection: sample delimiter", - input: "1-2-3-4-5z3-4-5-6-7", - expectedOutput: "3-4-5", - recipeConfig: [ - { - op: "Set Operations", - args: ["z", "-", "Intersection"], - }, - ], - }, - { - name: "Set Operations: Set Difference", - input: "1 2 3 4 5\n\n3 4 5 6 7", - expectedOutput: "1 2", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Set Difference"], - }, - ], - }, - { - name: "Set Operations: Set Difference: wrong sample count", - input: "1 2 3 4 5_3_4 5 6 7", - expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", - recipeConfig: [ - { - op: "Set Operations", - args: [" ", "_", "Set Difference"], - }, - ], - }, - { - name: "Set Operations: Set Difference: item delimiter", - input: "1;2;3;4;5\n\n3;4;5;6;7", - expectedOutput: "1;2", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", ";", "Set Difference"], - }, - ], - }, - { - name: "Set Operations: Set Difference: sample delimiter", - input: "1;2;3;4;5===3;4;5;6;7", - expectedOutput: "1;2", - recipeConfig: [ - { - op: "Set Operations", - args: ["===", ";", "Set Difference"], - }, - ], - }, - { - name: "Set Operations: Symmetric Difference", - input: "1 2 3 4 5\n\n3 4 5 6 7", - expectedOutput: "1 2 6 7", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Symmetric Difference"], - }, - ], - }, - { - name: "Set Operations: Symmetric Difference: wrong sample count", - input: "1 2\n\n3 4 5\n\n3 4 5 6 7", - expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Symmetric Difference"], - }, - ], - }, - { - name: "Set Operations: Symmetric Difference: item delimiter", - input: "a_b_c_d_e\n\nc_d_e_f_g", - expectedOutput: "a_b_f_g", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", "_", "Symmetric Difference"], - }, - ], - }, - { - name: "Set Operations: Symmetric Difference: sample delimiter", - input: "a_b_c_d_eAAAAAc_d_e_f_g", - expectedOutput: "a_b_f_g", - recipeConfig: [ - { - op: "Set Operations", - args: ["AAAAA", "_", "Symmetric Difference"], - }, - ], - }, - { - name: "Set Operations: Cartesian Product", - input: "1 2 3 4 5\n\na b c d e", - expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e)", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Cartesian Product"], - }, - ], - }, - { - name: "Set Operations: Cartesian Product: wrong sample count", - input: "1 2\n\n3 4 5\n\na b c d e", - expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Cartesian Product"], - }, - ], - }, - { - name: "Set Operations: Cartesian Product: too many on left", - input: "1 2 3 4 5 6\n\na b c d e", - expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e) (6,undefined)", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Cartesian Product"], - }, - ], - }, - { - name: "Set Operations: Cartesian Product: too many on right", - input: "1 2 3 4 5\n\na b c d e f", - expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e) (undefined,f)", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Cartesian Product"], - }, - ], - }, - { - name: "Set Operations: Cartesian Product: item delimiter", - input: "1-2-3-4-5\n\na-b-c-d-e", - expectedOutput: "(1,a)-(2,b)-(3,c)-(4,d)-(5,e)", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", "-", "Cartesian Product"], - }, - ], - }, - { - name: "Set Operations: Cartesian Product: sample delimiter", - input: "1 2 3 4 5_a b c d e", - expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e)", - recipeConfig: [ - { - op: "Set Operations", - args: ["_", " ", "Cartesian Product"], - }, - ], - }, - { - name: "Set Operations: Power set: nothing", - input: "", - expectedOutput: "", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Power Set"], - }, - ], - }, - { - name: "Set Operations: Power set: Too many samples", - input: "1 2 3\n\n4", - expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Power Set"], - }, - ], - }, - { - name: "Set Operations: Power set", - input: "1 2 4", - expectedOutput: "\n4\n2\n1\n2 4\n1 4\n1 2\n1 2 4\n", - recipeConfig: [ - { - op: "Set Operations", - args: ["\n\n", " ", "Power Set"], - }, - ], - }, -]); diff --git a/test/tests/operations/SetUnion.mjs b/test/tests/operations/SetUnion.mjs new file mode 100644 index 00000000..f214549a --- /dev/null +++ b/test/tests/operations/SetUnion.mjs @@ -0,0 +1,298 @@ +/** + * Set Operations tests. + * + * @author d98762625 + * + * @copyright Crown Copyright 2017 + * @license Apache-2.0 + */ +import TestRegister from "../../TestRegister"; + +TestRegister.addTests([ + { + name: "Set Union: Nothing", + input: "\n\n", + expectedOutput: "", + recipeConfig: [ + { + op: "Set Union", + args: ["\n\n", " "], + }, + ], + }, + { + name: "Set Union", + input: "1 2 3 4 5\n\n3 4 5 6 7", + expectedOutput: "1 2 3 4 5 6 7", + recipeConfig: [ + { + op: "Set Union", + args: ["\n\n", " "], + }, + ], + }, + { + name: "Set Union: invalid sample number", + input: "1 2 3 4 5\n\n3 4 5 6 7\n\n1", + expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + recipeConfig: [ + { + op: "Set Union", + args: ["\n\n", " "], + }, + ], + }, + { + name: "Set Union: item delimiter", + input: "1,2,3,4,5\n\n3,4,5,6,7", + expectedOutput: "1,2,3,4,5,6,7", + recipeConfig: [ + { + op: "Set Union", + args: ["\n\n", ","], + }, + ], + }, + { + name: "Set Union: sample delimiter", + input: "1 2 3 4 5whatever3 4 5 6 7", + expectedOutput: "1 2 3 4 5 6 7", + recipeConfig: [ + { + op: "Set Union", + args: ["whatever", " "], + }, + ], + }, + // { + // name: "Set Operations: Intersection", + // input: "1 2 3 4 5\n\n3 4 5 6 7", + // expectedOutput: "3 4 5", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Intersection"], + // }, + // ], + // }, + // { + // name: "Set Operations: Intersection: only one set", + // input: "1 2 3 4 5 6 7 8", + // expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Intersection"], + // }, + // ], + // }, + // { + // name: "Set Operations: Intersection: item delimiter", + // input: "1-2-3-4-5\n\n3-4-5-6-7", + // expectedOutput: "3-4-5", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", "-", "Intersection"], + // }, + // ], + // }, + // { + // name: "Set Operations: Intersection: sample delimiter", + // input: "1-2-3-4-5z3-4-5-6-7", + // expectedOutput: "3-4-5", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["z", "-", "Intersection"], + // }, + // ], + // }, + // { + // name: "Set Operations: Set Difference", + // input: "1 2 3 4 5\n\n3 4 5 6 7", + // expectedOutput: "1 2", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Set Difference"], + // }, + // ], + // }, + // { + // name: "Set Operations: Set Difference: wrong sample count", + // input: "1 2 3 4 5_3_4 5 6 7", + // expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: [" ", "_", "Set Difference"], + // }, + // ], + // }, + // { + // name: "Set Operations: Set Difference: item delimiter", + // input: "1;2;3;4;5\n\n3;4;5;6;7", + // expectedOutput: "1;2", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", ";", "Set Difference"], + // }, + // ], + // }, + // { + // name: "Set Operations: Set Difference: sample delimiter", + // input: "1;2;3;4;5===3;4;5;6;7", + // expectedOutput: "1;2", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["===", ";", "Set Difference"], + // }, + // ], + // }, + // { + // name: "Set Operations: Symmetric Difference", + // input: "1 2 3 4 5\n\n3 4 5 6 7", + // expectedOutput: "1 2 6 7", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Symmetric Difference"], + // }, + // ], + // }, + // { + // name: "Set Operations: Symmetric Difference: wrong sample count", + // input: "1 2\n\n3 4 5\n\n3 4 5 6 7", + // expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Symmetric Difference"], + // }, + // ], + // }, + // { + // name: "Set Operations: Symmetric Difference: item delimiter", + // input: "a_b_c_d_e\n\nc_d_e_f_g", + // expectedOutput: "a_b_f_g", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", "_", "Symmetric Difference"], + // }, + // ], + // }, + // { + // name: "Set Operations: Symmetric Difference: sample delimiter", + // input: "a_b_c_d_eAAAAAc_d_e_f_g", + // expectedOutput: "a_b_f_g", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["AAAAA", "_", "Symmetric Difference"], + // }, + // ], + // }, + // { + // name: "Set Operations: Cartesian Product", + // input: "1 2 3 4 5\n\na b c d e", + // expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e)", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Cartesian Product"], + // }, + // ], + // }, + // { + // name: "Set Operations: Cartesian Product: wrong sample count", + // input: "1 2\n\n3 4 5\n\na b c d e", + // expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Cartesian Product"], + // }, + // ], + // }, + // { + // name: "Set Operations: Cartesian Product: too many on left", + // input: "1 2 3 4 5 6\n\na b c d e", + // expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e) (6,undefined)", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Cartesian Product"], + // }, + // ], + // }, + // { + // name: "Set Operations: Cartesian Product: too many on right", + // input: "1 2 3 4 5\n\na b c d e f", + // expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e) (undefined,f)", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Cartesian Product"], + // }, + // ], + // }, + // { + // name: "Set Operations: Cartesian Product: item delimiter", + // input: "1-2-3-4-5\n\na-b-c-d-e", + // expectedOutput: "(1,a)-(2,b)-(3,c)-(4,d)-(5,e)", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", "-", "Cartesian Product"], + // }, + // ], + // }, + // { + // name: "Set Operations: Cartesian Product: sample delimiter", + // input: "1 2 3 4 5_a b c d e", + // expectedOutput: "(1,a) (2,b) (3,c) (4,d) (5,e)", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["_", " ", "Cartesian Product"], + // }, + // ], + // }, + // { + // name: "Set Operations: Power set: nothing", + // input: "", + // expectedOutput: "", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Power Set"], + // }, + // ], + // }, + // { + // name: "Set Operations: Power set: Too many samples", + // input: "1 2 3\n\n4", + // expectedOutput: "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Power Set"], + // }, + // ], + // }, + // { + // name: "Set Operations: Power set", + // input: "1 2 4", + // expectedOutput: "\n4\n2\n1\n2 4\n1 4\n1 2\n1 2 4\n", + // recipeConfig: [ + // { + // op: "Set Operations", + // args: ["\n\n", " ", "Power Set"], + // }, + // ], + // }, +]);