CyberChef/src/core/operations/SetIntersection.mjs

88 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-04-11 19:29:02 +02:00
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
2018-04-04 18:37:19 +02:00
/**
* Set Intersection operation
2018-04-04 18:37:19 +02:00
*/
class SetIntersection extends Operation {
2018-04-04 18:37:19 +02:00
/**
* Set Intersection constructor
2018-04-04 18:37:19 +02:00
*/
constructor() {
super();
this.name = "Set Intersection";
this.module = "Default";
2018-04-11 19:29:02 +02:00
this.description = "Calculates the intersection of two sets.";
this.infoURL = "https://wikipedia.org/wiki/Intersection_(set_theory)";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Sample delimiter",
type: "binaryString",
2018-04-11 19:29:02 +02:00
value: "\\n\\n"
},
{
name: "Item delimiter",
type: "binaryString",
value: ","
},
];
}
/**
* Validate input length
2018-04-11 19:29:02 +02:00
*
* @param {Object[]} sets
* @throws {Error} if not two sets
*/
validateSampleNumbers(sets) {
if (!sets || (sets.length !== 2)) {
throw new OperationError("Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?");
}
}
/**
* Run the intersection operation
2018-04-11 19:29:02 +02:00
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
* @throws {OperationError}
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
const sets = input.split(this.sampleDelim);
this.validateSampleNumbers(sets);
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[]}
*/
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-04 18:37:19 +02:00
}
export default SetIntersection;