Skip to content

Commit

Permalink
more common parse fns into lib/util.js
Browse files Browse the repository at this point in the history
- move parseOperator
- extend parseStation
  • Loading branch information
derhuerst committed Apr 15, 2020
1 parent 7614d9c commit 8405a17
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 21 deletions.
9 changes: 1 addition & 8 deletions lib/journeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@ const {
parseDateTime: formatOutputDate,
normalizeEmpty: m,
parseSparseStation: createStation,
parseOperator: createOperator,
} = require('./util')
const fetch = require('./fetch')

const createOperator = (o) => ({
type: 'operator',
id: o.key,
name: o.label,
url: o.url,
address: o.address
})

const extractLegs = (rawOrigin, rawDestination, rawJourney) => {
let {
departure,
Expand Down
3 changes: 1 addition & 2 deletions lib/regions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ const merge = require('lodash/merge')
const intoStream = require('into-stream').object
const {
normalizeEmpty: m,
parseCountry: formatCountry,
} = require('./util')

const defaults = {
}

const formatCountry = (country) => (country ? { name: m(country.name), code: m(country.alpha2_code) } : null)

const createRegion = (city) => ({
type: 'region',
id: city.id + '',
Expand Down
10 changes: 2 additions & 8 deletions lib/stations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@ const intoStream = require('into-stream').object
const {
normalizeEmpty: m,
parseStation,
parseCountry: formatCountry,
} = require('./util')

const defaults = {
}

const formatCountry = (country) => (country ? { name: m(country.name), code: m(country.alpha2_code) } : null)

const createStation = (station) => {
const res = parseStation(station)
return {
...res,
location: {
...res.location,
country: formatCountry(station.country),
zip: station.zip,
},
...parseStation(station),
slug: m(station.slugs),
aliases: (station.aliases ? csv(station.aliases) : null),
regions: [m(station.city_id + '')].filter((x) => !!x),
Expand Down
17 changes: 14 additions & 3 deletions lib/timetable.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
'use strict'

const {
parseDateTime,
parseWhen,
parseStation,
normalizeEmpty,
} = require('./util')
const fetch = require('./fetch')

const parseArrivalDeparture = (dep, station) => {
const cancelled = dep.is_cancelled === true
const {
when,
plannedWhen,
prognosedWhen,
delay,
} = parseWhen({
planned: 'datetime',
// @todo does this route provide realtime data?
}, dep, cancelled)

return {
tripId: dep.trip_uid, // @todo: `dep.ride_id`?
when: parseDateTime(dep.datetime),
cancelled: dep.is_cancelled === true,
when, plannedWhen, prognosedWhen, delay,
cancelled,
direction: dep.direction,
lineName: dep.line_code,
stop: station,
Expand Down
50 changes: 50 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const moment = require('moment-timezone')
const slug = require('slugg')

const formatDateTime = (date) => {
return moment(date).tz('Europe/Berlin').format('DD.MM.YYYY')
Expand All @@ -20,6 +21,14 @@ const parseSparseStation = (s) => ({
name: normalizeEmpty(s.name),
})

const parseCountry = (country) => {
if (!country) return null
return {
name: normalizeEmpty(country.name),
code: normalizeEmpty(country.alpha2_code),
}
}

const parseStation = (s) => ({
...parseSparseStation(s),
location: {
Expand All @@ -28,14 +37,55 @@ const parseStation = (s) => ({
latitude: normalizeEmpty(s.coordinates.latitude),
address: normalizeEmpty(s.full_address),
street: normalizeEmpty(s.address),
zip: normalizeEmpty(s.zip),
country: parseCountry(s.country),
notes: normalizeEmpty(s.warnings),
},
importance: Number.isInteger(s.importance_order) ? normalizeEmpty(+s.importance_order) : null,
})

const parseOperator = (o) => ({
type: 'operator',
id: o.key ? o.key : (o.label ? slug(o.label) : null),
name: o.label,
url: normalizeEmpty(o.url),
address: normalizeEmpty(o.address),
})

const parseWhen = (keys, data, cancelled = false) => {
let plannedWhen = keys.planned && data[keys.planned] || null
if (plannedWhen) {
plannedWhen = parseDateTime(plannedWhen)
}
let prognosedWhen = keys.prognosed && data[keys.prognosed] || null
if (prognosedWhen && prognosedWhen.eta) {
prognosedWhen = parseDateTime(prognosedWhen.eta)
}
const delay = plannedWhen && prognosedWhen
? Math.round((new Date(prognosedWhen) - new Date(plannedWhen)) / 1000)
: null

if (cancelled) {
return {
when: null,
plannedWhen,
prognosedWhen,
delay,
}
}
return {
when: prognosedWhen || plannedWhen,
plannedWhen,
delay,
}
}

module.exports = {
formatDateTime,
parseDateTime,
normalizeEmpty,
parseSparseStation,
parseStation,
parseOperator,
parseWhen,
}

0 comments on commit 8405a17

Please sign in to comment.