forked from gchq/CyberChef
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetOperations.js
More file actions
202 lines (176 loc) · 5.25 KB
/
SetOperations.js
File metadata and controls
202 lines (176 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Utils from "../Utils.js";
/**
* Set operations.
*
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license APache-2.0
*
* @namespace
*/
class SetOps {
/**
* Set default options for operation
*/
constructor() {
this._sampleDelimiter = "\\n\\n";
this._operation = ["Union", "Intersection", "Set Difference", "Symmetric Difference", "Cartesian Product", "Power Set"];
this._itemDelimiter = ",";
}
/**
* Get operations array
* @returns {String[]}
*/
get OPERATION() {
return this._operation;
}
/**
* Get sample delimiter
* @returns {String}
*/
get SAMPLE_DELIMITER() {
return this._sampleDelimiter;
}
/**
* Get item delimiter
* @returns {String}
*/
get ITEM_DELIMITER() {
return this._itemDelimiter;
}
/**
* Run the configured set operation.
*
* @param {String} input
* @param {String[]} args
* @returns {html}
*/
runSetOperation(input, args) {
const [sampleDelim, itemDelimiter, operation] = args;
const sets = input.split(sampleDelim);
if (!sets || (sets.length !== 2 && operation !== "Power Set") || (sets.length !== 1 && operation === "Power Set")) {
return "Incorrect number of sets, perhaps you need to modify the sample delimiter or add more samples?";
}
if (this._operation.indexOf(operation) === -1) {
return "Invalid 'Operation' option.";
}
let result = {
"Union": this.runUnion,
"Intersection": this.runIntersect,
"Set Difference": this.runSetDifference,
"Symmetric Difference": this.runSymmetricDifference,
"Cartesian Product": this.runCartesianProduct,
"Power Set": this.runPowerSet.bind(undefined, itemDelimiter),
}[operation]
.apply(this, sets.map(s => s.split(itemDelimiter)));
// Formatting issues due to the nested characteristics of power set.
if (operation === "Power Set") {
result = result.map(i => `${i}\n`).join("");
} else {
result = result.join(itemDelimiter);
}
return Utils.escapeHtml(result);
}
/**
* Get the union of the two sets.
*
* @param {Object[]} a
* @param {Object[]} b
* @returns {Object[]}
*/
runUnion(a, b) {
const result = {};
/**
* Only add non-existing items
* @param {Object} hash
*/
const addUnique = (hash) => (item) => {
if (!hash[item]) {
hash[item] = true;
}
};
a.map(addUnique(result));
b.map(addUnique(result));
return Object.keys(result);
}
/**
* 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;
});
}
/**
* 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;
});
}
/**
* Get elements of each set that aren't in the other set.
*
* @param {Object[]} a
* @param {Object[]} b
* @return {Object[]}
*/
runSymmetricDifference(a, b) {
return this.runSetDifference(a, b)
.concat(this.runSetDifference(b, a));
}
/**
* Return the cartesian product of the two inputted sets.
*
* @param {Object[]} a
* @param {Object[]} b
* @returns {String[]}
*/
runCartesianProduct(a, b) {
return Array(Math.max(a.length, b.length))
.fill(null)
.map((item, index) => `(${a[index] || undefined},${b[index] || undefined})`);
}
/**
* Return the power set of the inputted set.
*
* @param {Object[]} a
* @returns {Object[]}
*/
runPowerSet(delimiter, a) {
// empty array items getting picked up
a = a.filter(i => i.length);
if (!a.length) {
return [];
}
/**
* Decimal to binary function
* @param {*} dec
*/
const toBinary = (dec) => (dec >>> 0).toString(2);
const result = new Set();
// Get the decimal number to make a binary as long as the input
const maxBinaryValue = parseInt(Number(a.map(i => "1").reduce((p, c) => p + c)), 2);
// Make an array of each binary number from 0 to maximum
const binaries = [...Array(maxBinaryValue + 1).keys()]
.map(toBinary)
.map(i => i.padStart(toBinary(maxBinaryValue).length, "0"));
// XOR the input with each binary to get each unique permutation
binaries.forEach((binary) => {
const split = binary.split("");
result.add(a.filter((item, index) => split[index] === "1"));
});
// map for formatting & put in length order.
return [...result].map(r => r.join(delimiter)).sort((a, b) => a.length - b.length);
}
}
export default new SetOps();