From 03ecaa81f78e868752ebd6c6a13e25c7c0e7e1a5 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Fri, 6 Apr 2018 16:52:59 +0100 Subject: [PATCH] move Set Intersection into its own operation class --- src/core/config/OperationConfig.json | 4 +- src/core/operations/SetIntersection.mjs | 65 +++++++++++++++++++---- test/index.mjs | 2 + test/tests/operations/SetIntersection.mjs | 56 +++++++++++++++++++ 4 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 test/tests/operations/SetIntersection.mjs diff --git a/src/core/config/OperationConfig.json b/src/core/config/OperationConfig.json index 6e0e27a9..024e5952 100644 --- a/src/core/config/OperationConfig.json +++ b/src/core/config/OperationConfig.json @@ -220,7 +220,7 @@ { "name": "Sample delimiter", "type": "binaryString", - "value": "\n\n" + "value": "\\n\\n" }, { "name": "Item delimiter", @@ -256,7 +256,7 @@ "flowControl": false, "args": [ { - "name": "Samples delimiter", + "name": "Sample delimiter", "type": "binaryString", "value": "\\n\\n" }, diff --git a/src/core/operations/SetIntersection.mjs b/src/core/operations/SetIntersection.mjs index efc3503f..fd91c55b 100644 --- a/src/core/operations/SetIntersection.mjs +++ b/src/core/operations/SetIntersection.mjs @@ -1,19 +1,63 @@ -import SetOp from "./SetOps"; +import Utils from "../Utils"; +import Operation from "../Operation"; /** - * + * Set Intersection operation */ -class SetIntersection extends SetOp { +class SetIntersection extends Operation { /** - * + * Set Intersection constructor */ constructor() { super(); - this.setOp = this.runIntersection; this.name = "Set Intersection"; + this.module = "Default"; this.description = "Get the intersection 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 intersection 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.runIntersect(...sets.map(s => s.split(this.itemDelimiter)))); } /** @@ -23,11 +67,14 @@ class SetIntersection extends SetOp { * @param {Object[]} b * @returns {Object[]} */ - runIntersection(a, b) { - return a.filter((item) => { - return b.indexOf(item) > -1; - }).join(this.itemDelimiter); + runIntersect(a, b) { + return a + .filter((item) => { + return b.indexOf(item) > -1; + }) + .join(this.itemDelimiter); } + } export default SetIntersection; diff --git a/test/index.mjs b/test/index.mjs index 08942219..73638537 100644 --- a/test/index.mjs +++ b/test/index.mjs @@ -48,6 +48,8 @@ import "./tests/operations/Base64"; // import "./tests/operations/StrUtils.js"; // import "./tests/operations/SeqUtils.js"; import "./tests/operations/SetUnion"; +import "./tests/operations/SetIntersection"; + let allTestsPassing = true; diff --git a/test/tests/operations/SetIntersection.mjs b/test/tests/operations/SetIntersection.mjs new file mode 100644 index 00000000..b6121893 --- /dev/null +++ b/test/tests/operations/SetIntersection.mjs @@ -0,0 +1,56 @@ +/** + * Set Operations tests. + * + * @author d98762625 + * + * @copyright Crown Copyright 2017 + * @license Apache-2.0 + */ +import TestRegister from "../../TestRegister"; + +TestRegister.addTests([ + { + name: "Set Intersection", + input: "1 2 3 4 5\n\n3 4 5 6 7", + expectedOutput: "3 4 5", + recipeConfig: [ + { + op: "Set Intersection", + args: ["\n\n", " "], + }, + ], + }, + { + name: "Set 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 Intersection", + args: ["\n\n", " "], + }, + ], + }, + { + name: "Set Intersection: item delimiter", + input: "1-2-3-4-5\n\n3-4-5-6-7", + expectedOutput: "3-4-5", + recipeConfig: [ + { + op: "Set Intersection", + args: ["\n\n", "-"], + }, + ], + }, + { + name: "Set Intersection: sample delimiter", + input: "1-2-3-4-5z3-4-5-6-7", + expectedOutput: "3-4-5", + recipeConfig: [ + { + op: "Set Intersection", + args: ["z", "-"], + }, + ], + } +]);