forked from TeamCodeStream/codestream-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_utilities.js
More file actions
46 lines (38 loc) · 1.21 KB
/
Copy patharray_utilities.js
File metadata and controls
46 lines (38 loc) · 1.21 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
// provides some utility array functions ... if this gets too big we can just start
// using lodash or something, but so far we don't really need all that much
'use strict';
module.exports = {
// does array1 and array2 have at least element in common?
hasCommonElement: function(array1, array2) {
return array1.find(elem => {
return array2.includes(elem);
});
},
// does array1 have all the elements in array2 (is it a superset)?
hasAllElements: function(array1, array2) {
return !array2.find(elem => {
return !array1.includes(elem);
});
},
// get all the elements in array1 that are not in array2
difference: function(array1, array2) {
return array1.filter(elem => !array2.includes(elem));
},
// get all the elements in array1 that are also in array2
intersection: function(array1, array2) {
return array1.filter(elem => array2.includes(elem));
},
// get all the elements in array1 and all the elements in array2,
// but avoid redundancies
union: function(array1, array2) {
return array1.concat(array2.filter(elem => !array1.includes(elem)));
},
unique: function(arr) {
return arr.reduce((a, elem) => {
if (a.indexOf(elem) === -1) {
a.push(elem);
}
return a;
}, []);
}
};