-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
51 lines (45 loc) · 1.65 KB
/
utils.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
const get = require('lodash/get');
const filter = require('lodash/filter');
const find = require('lodash/find');
const includes = require('lodash/includes');
const moment = require('moment');
const allStops = require('./static/stops.json');
const getArrivalDepartureTime = (schedules, trip_id, stop_id) => {
const trip = find(schedules, {trip_id});
return find(trip.schedule, {stop_id: parseInt(stop_id, 10)});
};
const toTimeStamp = (thisTime) => {
return parseInt(moment(thisTime, 'HH:mm:ss').format('X'), 10);
};
const mergeScheduleData = (trips, schedules) => {
return trips.map((trip) => {
const trip_id = get(trip, ['trip_update', 'trip', 'trip_id']);
const stops = trip.trip_update.stop_time_update.map((stop) => {
const times = getArrivalDepartureTime(schedules, trip_id, stop.stop_id);
if (times === undefined) {
console.log(`No schedule information for trip id: ${trip_id}`);
}
const arrival = stop.arrival ? {
delay: stop.arrival.delay,
time: times ? toTimeStamp(times.arrival) + stop.arrival.delay : 0
} : null;
const departure = stop.departure ? {
delay: stop.departure.delay,
time: times ? toTimeStamp(times.departure) + stop.departure.delay : 0
} : null;
return {...stop, arrival, departure};
});
const trip_update = {...trip.trip_update, stop_time_update: stops};
return {...trip, trip_update};
});
};
const getStopsByRouteId = (routeId) => {
const routeIdInt = parseInt(routeId, 10);
return filter(allStops, (stop) => {
return (includes(stop.route_ids, routeIdInt));
});
};
module.exports = {
mergeScheduleData,
getStopsByRouteId
};