-
Notifications
You must be signed in to change notification settings - Fork 23
/
test.js
72 lines (68 loc) · 1.69 KB
/
test.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
async function selectIPforG(ips, app) {
// choose the ip address whose sum of digits is the lowest
if (ips && ips.length) {
let chosenIp = ips[0];
let chosenIpSum = ips[0].split(':')[0].split('.').reduce((a, b) => parseInt(a, 10) + parseInt(b, 10), 0);
for (const ip of ips) {
const sum = ip.split(':')[0].split('.').reduce((a, b) => parseInt(a, 10) + parseInt(b, 10), 0);
if (sum < chosenIpSum) {
chosenIp = ip;
chosenIpSum = sum;
}
}
const isOk = false;
if (isOk) {
return chosenIp;
}
console.log(ips);
const newIps = ips.filter((ip) => ip !== chosenIp);
if (newIps.length) {
return selectIPforG(newIps, app);
}
}
return null;
}
async function test() {
const b = await selectIPforG(['123.234','2345.342','456.34234','342.434'], 'test');
console.log(b);
}
test();
const recentlyConfiguredApps = [{
appName: 'test1',
ips: ['1'],
},
{
appName: 'test2',
ips: ['1'],
},
{
appName: 'test',
ips: ['1'],
},
{
appName: 'test3',
ips: ['1'],
}];
let configuredApps = [{
appName: 'test',
ips: ['2'],
},
{
appName: 'test3',
ips: ['2'],
}];
const updatingConfig = JSON.parse(JSON.stringify(recentlyConfiguredApps));
// merge recentlyConfiguredApps with currently configuredApps
for (const app of configuredApps) {
let appExists = updatingConfig.find((a) => a.appName === app.appName);
if (!appExists) {
updatingConfig.push(app);
} else {
updatingConfig.splice(updatingConfig.indexOf(appExists), 1, app);
// console.log(app);
// appExists = app; // this is also updating element in updatingConfig
}
}
console.log(updatingConfig);
configuredApps = updatingConfig;
console.log(configuredApps);