|
@@ -1,11 +1,18 @@
|
|
import Utils from "../Utils.js";
|
|
import Utils from "../Utils.js";
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * Set operations.
|
|
*
|
|
*
|
|
|
|
+ * @author d98762625 [d98762625@gmail.com]
|
|
|
|
+ * @copyright Crown Copyright 2018
|
|
|
|
+ * @license APache-2.0
|
|
|
|
+ *
|
|
|
|
+ * @namespace
|
|
*/
|
|
*/
|
|
class SetOps {
|
|
class SetOps {
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
|
|
+ * Set default options for operation
|
|
*/
|
|
*/
|
|
constructor() {
|
|
constructor() {
|
|
this._sampleDelimiter = "\\n\\n";
|
|
this._sampleDelimiter = "\\n\\n";
|
|
@@ -14,21 +21,24 @@ class SetOps {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
|
|
+ * Get operations array
|
|
|
|
+ * @returns {String[]}
|
|
*/
|
|
*/
|
|
get OPERATION() {
|
|
get OPERATION() {
|
|
return this._operation;
|
|
return this._operation;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
|
|
+ * Get sample delimiter
|
|
|
|
+ * @returns {String}
|
|
*/
|
|
*/
|
|
get SAMPLE_DELIMITER() {
|
|
get SAMPLE_DELIMITER() {
|
|
return this._sampleDelimiter;
|
|
return this._sampleDelimiter;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
|
|
+ * Get item delimiter
|
|
|
|
+ * @returns {String}
|
|
*/
|
|
*/
|
|
get ITEM_DELIMITER() {
|
|
get ITEM_DELIMITER() {
|
|
return this._itemDelimiter;
|
|
return this._itemDelimiter;
|
|
@@ -36,9 +46,11 @@ class SetOps {
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * Run the configured set operation.
|
|
*
|
|
*
|
|
- * @param {*} input
|
|
|
|
- * @param {*} args
|
|
|
|
|
|
+ * @param {String} input
|
|
|
|
+ * @param {String[]} args
|
|
|
|
+ * @returns {html}
|
|
*/
|
|
*/
|
|
runSetOperation(input, args) {
|
|
runSetOperation(input, args) {
|
|
const [sampleDelim, itemDelimiter, operation] = args;
|
|
const [sampleDelim, itemDelimiter, operation] = args;
|
|
@@ -73,17 +85,19 @@ class SetOps {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * Get the union of the two sets.
|
|
*
|
|
*
|
|
- * @param {*} a
|
|
|
|
- * @param {*} a
|
|
|
|
|
|
+ * @param {Object[]} a
|
|
|
|
+ * @param {Object[]} b
|
|
|
|
+ * @returns {Object[]}
|
|
*/
|
|
*/
|
|
runUnion(a, b) {
|
|
runUnion(a, b) {
|
|
|
|
|
|
const result = {};
|
|
const result = {};
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
- * @param {*} r
|
|
|
|
|
|
+ * Only add non-existing items
|
|
|
|
+ * @param {Object} hash
|
|
*/
|
|
*/
|
|
const addUnique = (hash) => (item) => {
|
|
const addUnique = (hash) => (item) => {
|
|
if (!hash[item]) {
|
|
if (!hash[item]) {
|
|
@@ -98,9 +112,10 @@ class SetOps {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
- * @param {*} a
|
|
|
|
- * @param {*} b
|
|
|
|
|
|
+ * Get the intersection of the two sets.
|
|
|
|
+ * @param {Object[]} a
|
|
|
|
+ * @param {Object[]} b
|
|
|
|
+ * @returns {Object[]}
|
|
*/
|
|
*/
|
|
runIntersect(a, b) {
|
|
runIntersect(a, b) {
|
|
return a.filter((item) => {
|
|
return a.filter((item) => {
|
|
@@ -109,9 +124,10 @@ class SetOps {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
- * @param {*} a
|
|
|
|
- * @param {*} b
|
|
|
|
|
|
+ * Get elements in set a that are not in set b
|
|
|
|
+ * @param {Object[]} a
|
|
|
|
+ * @param {Object[]} b
|
|
|
|
+ * @returns {Object[]}
|
|
*/
|
|
*/
|
|
runSetDifference(a, b) {
|
|
runSetDifference(a, b) {
|
|
return a.filter((item) => {
|
|
return a.filter((item) => {
|
|
@@ -120,14 +136,16 @@ class SetOps {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
- * @param {*} a
|
|
|
|
- * @param {*} b
|
|
|
|
|
|
+ * Get elements of each set that aren't in the other set.
|
|
|
|
+ * @param {Object[]} a
|
|
|
|
+ * @param {Object[]} b
|
|
|
|
+ * @return {Object}
|
|
*/
|
|
*/
|
|
runSymmetricDifference(a, b) {
|
|
runSymmetricDifference(a, b) {
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
- * @param {*} refArray
|
|
|
|
|
|
+ * One-way difference function, formatted for mapping.
|
|
|
|
+ * @param {Object[]} refArray
|
|
*/
|
|
*/
|
|
const getDifference = (refArray) => (item) => {
|
|
const getDifference = (refArray) => (item) => {
|
|
return refArray.indexOf(item) === -1;
|
|
return refArray.indexOf(item) === -1;
|