ESM: Tidied up Set operations

This commit is contained in:
n1474335 2018-04-11 17:29:02 +00:00
parent 955a082614
commit e99331f305
12 changed files with 81 additions and 487 deletions

View file

@ -4,7 +4,6 @@
* @license Apache-2.0
*/
import Utils from "../Utils";
import Operation from "../Operation";
/**
@ -20,14 +19,14 @@ class CartesianProduct extends Operation {
this.name = "Cartesian Product";
this.module = "Default";
this.description = "Get the cartesian product of two sets";
this.description = "Calculates the cartesian product of multiple sets of data, returning all possible combinations.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Sample delimiter",
type: "binaryString",
value: Utils.escapeHtml("\\n\\n")
value: "\\n\\n"
},
{
name: "Item delimiter",
@ -39,19 +38,22 @@ class CartesianProduct extends Operation {
/**
* Validate input length
*
* @param {Object[]} sets
* @throws {Error} if not two sets
* @throws {Error} if fewer than 2 sets
*/
validateSampleNumbers(sets) {
if (!sets || (sets.length !== 2)) {
if (!sets || sets.length < 2) {
throw "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
}
}
/**
* Run the product operation
* @param input
* @param args
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
@ -63,7 +65,7 @@ class CartesianProduct extends Operation {
return e;
}
return Utils.escapeHtml(this.runCartesianProduct(...sets.map(s => s.split(this.itemDelimiter))));
return this.runCartesianProduct(...sets.map(s => s.split(this.itemDelimiter)));
}
/**
@ -71,12 +73,23 @@ class CartesianProduct extends Operation {
*
* @param {Object[]} a
* @param {Object[]} b
* @returns {String[]}
* @param {Object[]} c
* @returns {string}
*/
runCartesianProduct(a, b) {
return Array(Math.max(a.length, b.length))
.fill(null)
.map((item, index) => `(${a[index] || undefined},${b[index] || undefined})`)
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(",")})`)
.join(this.itemDelimiter);
}
}

View file

@ -4,7 +4,6 @@
* @license Apache-2.0
*/
import Utils from "../Utils";
import Operation from "../Operation";
/**
@ -20,7 +19,7 @@ class PowerSet extends Operation {
this.name = "Power Set";
this.module = "Default";
this.description = "Generate the power set of a set";
this.description = "Calculates all the subsets of a set.";
this.inputType = "string";
this.outputType = "string";
this.args = [
@ -34,8 +33,10 @@ class PowerSet extends Operation {
/**
* Generate the power set
* @param input
* @param args
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
[this.itemDelimiter] = args;
@ -43,7 +44,7 @@ class PowerSet extends Operation {
const inputArray = input.split(this.itemDelimiter).filter(a => a);
if (inputArray.length) {
return Utils.escapeHtml(this.runPowerSet(inputArray));
return this.runPowerSet(inputArray);
}
return "";

View file

@ -4,7 +4,6 @@
* @license Apache-2.0
*/
import Utils from "../Utils";
import Operation from "../Operation";
/**
@ -20,14 +19,14 @@ class SetDifference extends Operation {
this.name = "Set Difference";
this.module = "Default";
this.description = "Get the Difference of two sets";
this.description = "Calculates the difference of two sets.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Sample delimiter",
type: "binaryString",
value: Utils.escapeHtml("\\n\\n")
value: "\\n\\n"
},
{
name: "Item delimiter",
@ -39,6 +38,7 @@ class SetDifference extends Operation {
/**
* Validate input length
*
* @param {Object[]} sets
* @throws {Error} if not two sets
*/
@ -50,8 +50,10 @@ class SetDifference extends Operation {
/**
* Run the difference operation
* @param input
* @param args
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
@ -63,7 +65,7 @@ class SetDifference extends Operation {
return e;
}
return Utils.escapeHtml(this.runSetDifference(...sets.map(s => s.split(this.itemDelimiter))));
return this.runSetDifference(...sets.map(s => s.split(this.itemDelimiter)));
}
/**

View file

@ -1,4 +1,9 @@
import Utils from "../Utils";
/**
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
/**
@ -14,14 +19,14 @@ class SetIntersection extends Operation {
this.name = "Set Intersection";
this.module = "Default";
this.description = "Get the intersection of two sets";
this.description = "Calculates the intersection of two sets.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Sample delimiter",
type: "binaryString",
value: Utils.escapeHtml("\\n\\n")
value: "\\n\\n"
},
{
name: "Item delimiter",
@ -33,6 +38,7 @@ class SetIntersection extends Operation {
/**
* Validate input length
*
* @param {Object[]} sets
* @throws {Error} if not two sets
*/
@ -44,8 +50,10 @@ class SetIntersection extends Operation {
/**
* Run the intersection operation
* @param input
* @param args
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
@ -57,7 +65,7 @@ class SetIntersection extends Operation {
return e;
}
return Utils.escapeHtml(this.runIntersect(...sets.map(s => s.split(this.itemDelimiter))));
return this.runIntersect(...sets.map(s => s.split(this.itemDelimiter)));
}
/**

View file

@ -4,7 +4,6 @@
* @license Apache-2.0
*/
import Utils from "../Utils";
import Operation from "../Operation";
/**
@ -20,14 +19,14 @@ class SetUnion extends Operation {
this.name = "Set Union";
this.module = "Default";
this.description = "Get the union of two sets";
this.description = "Calculates the union of two sets.";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Sample delimiter",
type: "binaryString",
value: Utils.escapeHtml("\\n\\n")
value: "\\n\\n"
},
{
name: "Item delimiter",
@ -39,6 +38,7 @@ class SetUnion extends Operation {
/**
* Validate input length
*
* @param {Object[]} sets
* @throws {Error} if not two sets
*/
@ -50,8 +50,10 @@ class SetUnion extends Operation {
/**
* Run the union operation
* @param input
* @param args
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
@ -63,7 +65,7 @@ class SetUnion extends Operation {
return e;
}
return Utils.escapeHtml(this.runUnion(...sets.map(s => s.split(this.itemDelimiter))));
return this.runUnion(...sets.map(s => s.split(this.itemDelimiter)));
}
/**

View file

@ -20,7 +20,7 @@ class SymmetricDifference extends Operation {
this.name = "Symmetric Difference";
this.module = "Default";
this.description = "Get the symmetric difference of two sets";
this.description = "Calculates the symmetric difference of two sets.";
this.inputType = "string";
this.outputType = "string";
this.args = [
@ -39,6 +39,7 @@ class SymmetricDifference extends Operation {
/**
* Validate input length
*
* @param {Object[]} sets
* @throws {Error} if not two sets
*/
@ -50,8 +51,10 @@ class SymmetricDifference extends Operation {
/**
* Run the difference operation
* @param input
* @param args
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
[this.sampleDelim, this.itemDelimiter] = args;
@ -63,7 +66,7 @@ class SymmetricDifference extends Operation {
return e;
}
return Utils.escapeHtml(this.runSymmetricDifference(...sets.map(s => s.split(this.itemDelimiter))));
return this.runSymmetricDifference(...sets.map(s => s.split(this.itemDelimiter)));
}
/**