jubilee/server/reducers/directions.js
2018-04-11 16:14:43 +01:00

50 lines
1.4 KiB
JavaScript

const logger = require('log4js').getLogger('Directions 🔧');
const { get, isEmpty, has } = require('lodash');
const humanizeDuration = require('humanize-duration');
logger.level = 'debug';
const htmlTidy = /<(\/*?)(?!(em|p|br\s*\/|strong|h1|h2|h3))\w+?.+?>/gim;
function reduceEstDirections(body = '') {
if (body === '') return {};
console.log(body);
const jBody = JSON.parse(body);
const obj = {};
const { ResultSet } = jBody;
if (has(ResultSet, 'Result')) {
const directions = get(ResultSet, 'Result.yahoo_driving_directions');
obj.totalTime = parseFloat(get(directions, 'total_time'));
obj.totalTimeWithTraffic = parseFloat(get(directions, 'total_time_with_traffic'));
obj.readable = humanizeDuration(obj.totalTimeWithTraffic * 60 * 1000);
obj.timePercentage = ((obj.totalTime * ( obj.totalTimeWithTraffic / 100 )) + obj.totalTime) - 100;
if ( obj.totalTimeWithTraffic > (obj.totalTime * 1.75)) {
obj.traffic = 'heavy traffic';
obj.className = 'trafficHeavy';
}
else if ( obj.totalTimeWithTraffic > (obj.totalTime * 1.5)) {
obj.traffic = 'some traffic';
obj.className = 'trafficMedium';
}
else if ( obj.totalTimeWithTraffic > (obj.totalTime * 1.25)) {
obj.traffic = 'light traffic';
obj.className = 'trafficLight';
}
else {
obj.traffic = 'no traffic';
obj.className = 'trafficNone';
}
}
return obj;
}
module.exports = { reduceEstDirections };