2018-04-09 12:13:23 +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-09 12:13:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set cartesian product operation
|
|
|
|
*/
|
|
|
|
class CartesianProduct extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cartesian Product constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Cartesian Product";
|
|
|
|
this.module = "Default";
|
2018-04-11 19:29:02 +02:00
|
|
|
this.description = "Calculates the cartesian product of multiple sets of data, returning all possible combinations.";
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Cartesian_product";
|
2018-04-09 12:13:23 +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 12:13:23 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Item delimiter",
|
|
|
|
type: "binaryString",
|
|
|
|
value: ","
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate input length
|
2018-04-11 19:29:02 +02:00
|
|
|
*
|
2018-04-09 12:13:23 +02:00
|
|
|
* @param {Object[]} sets
|
2018-05-15 19:01:04 +02:00
|
|
|
* @throws {OperationError} if fewer than 2 sets
|
2018-04-09 12:13:23 +02:00
|
|
|
*/
|
|
|
|
validateSampleNumbers(sets) {
|
2018-04-11 19:29:02 +02:00
|
|
|
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 12:13:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the product 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 12:13:23 +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 12:13:23 +02:00
|
|
|
|
2018-04-11 19:29:02 +02:00
|
|
|
return this.runCartesianProduct(...sets.map(s => s.split(this.itemDelimiter)));
|
2018-04-09 12:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the cartesian product of the two inputted sets.
|
|
|
|
*
|
|
|
|
* @param {Object[]} a
|
|
|
|
* @param {Object[]} b
|
2018-04-11 19:29:02 +02:00
|
|
|
* @param {Object[]} c
|
|
|
|
* @returns {string}
|
2018-04-09 12:13:23 +02:00
|
|
|
*/
|
2018-04-11 19:29:02 +02:00
|
|
|
runCartesianProduct(a, b, ...c) {
|
|
|
|
/**
|
|
|
|
* https://stackoverflow.com/a/43053803/7200497
|
|
|
|
* @returns {Object[]}
|
|
|
|
*/
|
|
|
|
const f = (a, b) => [].concat(...a.map(d => b.map(e => [].concat(d, e))));
|
|
|
|
/**
|
|
|
|
* https://stackoverflow.com/a/43053803/7200497
|
|
|
|
* @returns {Object[][]}
|
|
|
|
*/
|
|
|
|
const cartesian = (a, b, ...c) => (b ? cartesian(f(a, b), ...c) : a);
|
|
|
|
|
|
|
|
return cartesian(a, b, ...c)
|
|
|
|
.map(set => `(${set.join(",")})`)
|
2018-04-09 12:13:23 +02:00
|
|
|
.join(this.itemDelimiter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CartesianProduct;
|