-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathformatData.js
87 lines (73 loc) · 2.98 KB
/
formatData.js
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
// format intersection data
const formatIntersectionData = (data) => {
// compiling solo set data - how many values per set
const soloSets = [];
// nameStr is for the setName, which makes it easy to compile
// each name would be A, then B, so on..
const nameStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substr(0, data.length);
data.forEach((x, i) => {
soloSets.push({
name: x.name,
setName: nameStr.substr(i, 1),
num: x.values.length,
values: x.values,
});
});
// compiling list of intersection names recursively
// ["A", "AB", "ABC", ...]
const getIntNames = (start, end, nameStr) => {
// eg. BCD
const name = nameStr.substring(start, end);
// when reaching the last letter
if (name.length === 1) {
return [name];
}
const retArr = getIntNames(start + 1, end, nameStr);
// eg. for name = BCD, would return [B] + [BC,BCD,BD] + [C,CD,D]
return [name[0]].concat(retArr.map((x) => name[0] + x), retArr);
};
let intNames = getIntNames(0, nameStr.length, nameStr);
// removing solo names
intNames = intNames.filter((x) => x.length !== 1);
let intersections = [];
// compile intersections of values for each intersection name
intNames.forEach((intName) => {
// collecting all values: [pub1arr, pub2arr, ...]
const values = intName.split('').map((set) => soloSets.find((x) => x.setName === set).values);
// getting intersection
// https://stackoverflow.com/questions/37320296/how-to-calculate-intersection-of-multiple-arrays-in-javascript-and-what-does-e
const result = values.reduce((a, b) => a.filter((c) => b.includes(c)));
intersections.push({
name: intName.split('').map((set) => soloSets.find((x) => x.setName === set).name).join(' + '),
setName: intName,
num: result.length,
values: result,
});
});
// taking out all 0s
intersections = intersections.filter((x) => x.value !== 0);
return { intersections, soloSets };
};
// include solo sets with all its data
const insertSoloDataAll = (intersections, soloSets) => {
soloSets.forEach(x => {
intersections.push(x);
});
return intersections;
};
// include solo sets with only the values that ARE NOT in other sets
const insertSoloDataOutersect = (intersections, soloSets) => {
soloSets.forEach(x => {
// compile all unique values from other sets except current set
const otherSets = [...new Set(soloSets.map(y => y.setName === x.setName ? [] : y.values).flat())];
// subtract otherSets values from current set values
const values = x.values.filter(y => !otherSets.includes(y));
intersections.push({
name: x.name,
setName: x.setName,
num: values.length,
values: values,
})
})
return intersections;
}