2018-04-09 11:23:05 +02:00
|
|
|
/**
|
|
|
|
* @author d98762625 [d98762625@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2018
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation";
|
2018-04-27 10:59:10 +02:00
|
|
|
import OperationError from "../errors/OperationError";
|
2018-04-09 11:23:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Difference operation
|
|
|
|
*/
|
|
|
|
class SetDifference extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Difference constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Set Difference";
|
|
|
|
this.module = "Default";
|
2018-08-21 20:07:13 +02:00
|
|
|
this.description = "Calculates the difference, or relative complement, of two sets.";
|
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Complement_(set_theory)#Relative_complement";
|
2018-04-09 11:23:05 +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-09 11:23:05 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Item delimiter",
|
|
|
|
type: "binaryString",
|
|
|
|
value: ","
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate input length
|
2018-04-11 19:29:02 +02:00
|
|
|
*
|
2018-04-09 11:23:05 +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-09 11:23:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the difference 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-09 11:23:05 +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-09 11:23:05 +02:00
|
|
|
|
2018-04-11 19:29:02 +02:00
|
|
|
return this.runSetDifference(...sets.map(s => s.split(this.itemDelimiter)));
|
2018-04-09 11:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get elements in set a that are not in set b
|
|
|
|
*
|
|
|
|
* @param {Object[]} a
|
|
|
|
* @param {Object[]} b
|
|
|
|
* @returns {Object[]}
|
|
|
|
*/
|
|
|
|
runSetDifference(a, b) {
|
|
|
|
return a
|
|
|
|
.filter((item) => {
|
|
|
|
return b.indexOf(item) === -1;
|
|
|
|
})
|
|
|
|
.join(this.itemDelimiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SetDifference;
|