-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutils.js
More file actions
38 lines (31 loc) · 743 Bytes
/
utils.js
File metadata and controls
38 lines (31 loc) · 743 Bytes
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
const intersection = (...arrs) => {
const counts = new Map();
for (let arr of arrs) {
const unique = [...new Set(arr)];
for (let val of unique) {
if (!counts.has(val)) {
counts.set(val, 0);
}
const current_count = counts.get(val);
counts.set(val, current_count + 1);
}
}
const intersected = [...counts.entries()]
.filter(([key, count]) => count === arrs.length)
.map(([key]) => key);
return intersected;
};
const range = (from, to, inclusive = true) => {
if (to < from) {
// Swap values if `from` is not less than `to`
[from, to] = [to, from];
}
const size = to - from + (inclusive ? 1 : 0);
return Array(size)
.fill()
.map((_, i) => i + from);
};
module.exports = {
intersection,
range,
};