2018-04-11 19:29:02 +02:00
|
|
|
/**
|
|
|
|
* @author d98762625 [d98762625@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2018
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
2018-04-04 18:37:19 +02:00
|
|
|
|
|
|
|
/**
|
2018-04-06 17:52:59 +02:00
|
|
|
* Set Intersection operation
|
2018-04-04 18:37:19 +02:00
|
|
|
*/
|
2018-04-06 17:52:59 +02:00
|
|
|
class SetIntersection extends Operation {
|
2018-04-04 18:37:19 +02:00
|
|
|
|
|
|
|
/**
|
2018-04-06 17:52:59 +02:00
|
|
|
* Set Intersection constructor
|
2018-04-04 18:37:19 +02:00
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Set Intersection";
|
2018-04-06 17:52:59 +02:00
|
|
|
this.module = "Default";
|
2018-04-11 19:29:02 +02:00
|
|
|
this.description = "Calculates the intersection of two sets.";
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Intersection_(set_theory)";
|
2018-04-06 17:52:59 +02:00
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
name: "Sample delimiter",
|
|
|
|
type: "binaryString",
|
2018-04-11 19:29:02 +02:00
|
|
|
value: "\\n\\n"
|
2018-04-06 17:52:59 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Item delimiter",
|
|
|
|
type: "binaryString",
|
|
|
|
value: ","
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate input length
|
2018-04-11 19:29:02 +02:00
|
|
|
*
|
2018-04-06 17:52:59 +02:00
|
|
|
* @param {Object[]} sets
|
|
|
|
* @throws {Error} if not two sets
|
|
|
|
*/
|
|
|
|
validateSampleNumbers(sets) {
|
|
|
|
if (!sets || (sets.length !== 2)) {
|
2018-04-27 10:59:10 +02:00
|
|
|
throw new OperationError("Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?");
|
2018-04-06 17:52:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the intersection operation
|
2018-04-11 19:29:02 +02:00
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
2018-04-27 10:59:10 +02:00
|
|
|
* @throws {OperationError}
|
2018-04-06 17:52:59 +02:00
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
[this.sampleDelim, this.itemDelimiter] = args;
|
|
|
|
const sets = input.split(this.sampleDelim);
|
|
|
|
|
2018-04-27 10:59:10 +02:00
|
|
|
this.validateSampleNumbers(sets);
|
2018-04-06 17:52:59 +02:00
|
|
|
|
2018-04-11 19:29:02 +02:00
|
|
|
return this.runIntersect(...sets.map(s => s.split(this.itemDelimiter)));
|
2018-04-04 18:37:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the intersection of the two sets.
|
|
|
|
*
|
|
|
|
* @param {Object[]} a
|
|
|
|
* @param {Object[]} b
|
|
|
|
* @returns {Object[]}
|
|
|
|
*/
|
2018-04-06 17:52:59 +02:00
|
|
|
runIntersect(a, b) {
|
|
|
|
return a
|
|
|
|
.filter((item) => {
|
|
|
|
return b.indexOf(item) > -1;
|
|
|
|
})
|
|
|
|
.join(this.itemDelimiter);
|
2018-04-04 18:37:19 +02:00
|
|
|
}
|
2018-04-06 17:52:59 +02:00
|
|
|
|
2018-04-04 18:37:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default SetIntersection;
|