db-vendo-client/parse/trip.js

31 lines
708 B
JavaScript
Raw Permalink Normal View History

2020-06-11 15:33:45 +02:00
'use strict'
const minBy = require('lodash/minBy')
const maxBy = require('lodash/maxBy')
const last = require('lodash/last')
2020-06-11 15:33:45 +02:00
const parseTrip = (ctx, t) => { // t = raw trip
const {profile} = ctx
// pretend the trip is a leg in a journey
const fakeLeg = {
type: 'JNY',
dep: minBy(t.stopL, 'idx') || t.stopL[0],
arr: maxBy(t.stopL, 'idx') || last(t.stopL),
2020-06-11 15:33:45 +02:00
jny: t,
}
2020-03-09 21:44:08 +01:00
// todo: this breaks if the trip starts on a different day
// how does HAFAS do this?
const today = () => profile.formatDate(profile, Date.now())
const date = t.date || today()
const trip = profile.parseJourneyLeg(ctx, fakeLeg, date)
2020-06-11 15:33:45 +02:00
trip.id = trip.tripId
delete trip.tripId
return trip
}
module.exports = parseTrip