-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (28 loc) · 901 Bytes
/
index.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
const data = require('./data.json');
function closest(averages) {
return Object.keys(averages).reduce((key, v) =>
averages[v] < averages[key] ? v : key
);
}
function findClosestAWSRegion(to, options) {
options = options || {};
options.data = options.data || data;
options.filters = options.filters || [];
options.default = options.default || options.filters[0] || 'us-east-1';
const current = options.data[to];
if (!current) {
return options.default;
}
if (options.excludeTo) {
delete current[to];
}
const filtered = options.filters.length
? Object.entries(current).reduce((prev, [region, ping]) => {
return options.filters.includes(region)
? { ...prev, [region]: ping }
: prev;
}, {})
: current;
return Object.keys(filtered).length ? closest(filtered) : options.default;
}
module.exports = findClosestAWSRegion;