move Set Intersection into its own operation class

This commit is contained in:
d98762625 2018-04-06 16:52:59 +01:00
parent 5f93c667a2
commit 03ecaa81f7
4 changed files with 116 additions and 11 deletions

View file

@ -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;