const logger = require('log4js').getLogger('Directions 🔧'); const { get, isEmpty, has, uniq } = 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 {}; const jBody = JSON.parse(body); const obj = {}; const { ResultSet } = jBody; const streets = []; if (has(ResultSet, 'Result')) { const directions = get(ResultSet, 'Result.yahoo_driving_directions'); const route = get(directions, 'directions.route_leg'); 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'; } for (const item of route) { const street = item.street.split(','); if (street[0] !== '') streets.push(street[0]); } obj.streets = uniq(streets); } return obj; } function reduceIncidents(body = '') { if (body === '') return []; let workObj = Object.assign({}, body); let incidents = []; const items = get(workObj, 'items'); for (let item of items) { let title = item.title.split(' '); incidents.push({ title : item.title, description: item.description, road : title[0] }) } return incidents; } module.exports = { reduceEstDirections, reduceIncidents };