forked from cannawen/metric_units_reddit_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
34 lines (28 loc) · 1.05 KB
/
converter.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
const analytics = require('./analytics');
const rh = require('./regex_helper');
const ch = require('./conversion_helper');
function conversions(comment) {
if (!ch.shouldConvertComment(comment)) {
return {};
}
const potentialConversions = ch.findPotentialConversions(comment);
const filteredConversions = ch.filterConversions(potentialConversions);
const metricConversions = ch.calculateMetric(filteredConversions);
const roundedConversions = ch.roundConversions(metricConversions);
const formattedConversions = ch.formatConversion(roundedConversions);
return formattedConversions.reduce((memo, conversion) => {
const key = conversion['imperial']['number'] + conversion['imperial']['unit'];
const formatted = conversion['formatted'];
let value;
if (Array.isArray(formatted)) {
value = formatted.map(el => el['number'] + el['unit']).join(' or ');
} else {
value = formatted['number'] + formatted['unit'];
}
memo[key] = value ;
return memo;
}, {});
}
module.exports = {
"conversions" : conversions
}