Skip to content

Commit 431ea77

Browse files
committed
add sortNetworksToAttach
1 parent 937bb01 commit 431ea77

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lib/tools.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,36 @@ module.exports = {
55

66
while (order.length !== serviceNames.length) {
77
for (var serviceName of serviceNames) {
8-
if(order.indexOf(serviceName) === -1) {
8+
if (order.indexOf(serviceName) === -1) {
99
insertService(serviceName, recipe.services[serviceName].depends_on || [], order)
1010
}
1111
}
1212
}
1313
return order;
14+
},
15+
'sortNetworksToAttach': function (networksToAttach) {
16+
var networksToAttachSorted = [];
17+
for (let i = 0; i < networksToAttach.length; i++) {
18+
let networkName = Object.keys(networksToAttach[i]);
19+
if (i === 0) {
20+
networksToAttachSorted.push(networksToAttach[i])
21+
} else {
22+
let aux = 0;
23+
for (let j = 0; j < networksToAttachSorted.length; j++) {
24+
let networkNameSorted = Object.keys(networksToAttachSorted[j]);
25+
if (networksToAttachSorted[j][networkNameSorted].priority > networksToAttach[i][networkName].priority) {
26+
aux += j + 1;
27+
} else if (networksToAttachSorted[j][networkNameSorted].priority < networksToAttach[i][networkName].priority) {
28+
aux += j - 1;
29+
} else {
30+
aux += j + 1;
31+
}
32+
}
33+
if (aux < 0) aux = 0;
34+
networksToAttachSorted.splice(aux, 0, networksToAttach[i]);
35+
}
36+
}
37+
return networksToAttachSorted
1438
}
1539
}
1640

0 commit comments

Comments
 (0)