Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add departures()/arrivals() #40

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move common helper functions into lib/util.js
  • Loading branch information
derhuerst committed Jun 24, 2023
commit 273a51b1b58299cc83dd006d9d3473084101f490
17 changes: 6 additions & 11 deletions lib/journeys.js
Original file line number Diff line number Diff line change
@@ -10,12 +10,14 @@ const uniq = require('lodash/uniq')
const sortBy = require('lodash/sortBy')
const slug = require('slugg')

const {
formatDateTime: formatInputDate,
parseDateTime: formatOutputDate,
normalizeEmpty: m,
parseSparseStation: createStation,
} = require('./util')
const fetch = require('./fetch')

const m = (a) => ((a === undefined || a === '') ? null : a)
const formatInputDate = (date) => moment(date).tz('Europe/Berlin').format('DD.MM.YYYY')
const formatOutputDate = (date) => moment.unix(+date.timestamp).utcOffset(date.tz).format()

const createOperator = (o) => ({
type: 'operator',
id: o.key,
@@ -24,13 +26,6 @@ const createOperator = (o) => ({
address: o.address,
})

const createStation = (s) => ({
type: 'station',
id: s.id + '',
name: s.name,
importance: Number.isInteger(s.importance_order) ? s.importance_order : null,
})

const extractLegs = (rawOrigin, rawDestination, rawJourney) => {
let {
departure,
5 changes: 3 additions & 2 deletions lib/regions.js
Original file line number Diff line number Diff line change
@@ -3,8 +3,9 @@
const fetch = require('./fetch')
const merge = require('lodash/merge')
const intoStream = require('into-stream').object

const m = (a) => ((a === undefined || a === '') ? null : a)
const {
normalizeEmpty: m,
} = require('./util')

const defaults = {
}
40 changes: 19 additions & 21 deletions lib/stations.js
Original file line number Diff line number Diff line change
@@ -4,33 +4,31 @@ const csv = require('tiny-csv')
const merge = require('lodash/merge')
const fetch = require('./fetch')
const intoStream = require('into-stream').object

const m = (a) => ((a === undefined || a === '') ? null : a)
const {
normalizeEmpty: m,
parseStation,
} = require('./util')

const defaults = {
}

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

const createStation = (station) => ({
type: 'station',
id: station.id + '',
name: m(station.name),
location: {
type: 'location',
longitude: m(station.coordinates.longitude),
latitude: m(station.coordinates.latitude),
address: m(station.full_address),
country: formatCountry(station.country),
zip: station.zip,
street: m(station.address),
},
slug: m(station.slugs),
aliases: (station.aliases ? csv(station.aliases) : null),
regions: [m(station.city_id + '')].filter((x) => !!x),
connections: m(station.pairs),
importance: m(+station.importance_order),
})
const createStation = (station) => {
const res = parseStation(station)
return {
...res,
location: {
...res.location,
country: formatCountry(station.country),
zip: station.zip,
},
slug: m(station.slugs),
aliases: (station.aliases ? csv(station.aliases) : null),
regions: [m(station.city_id + '')].filter((x) => !!x),
connections: m(station.pairs),
}
}

const allAsync = async (opt) => {
const options = merge({}, defaults, opt)
41 changes: 41 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const moment = require('moment-timezone')

const formatDateTime = (date) => {
return moment(date).tz('Europe/Berlin').format('DD.MM.YYYY')
}

const parseDateTime = (date) => {
return moment.unix(+date.timestamp).utcOffset(date.tz).format()
}

const normalizeEmpty = (val) => {
return (val === undefined || val === '') ? null : val
}

const parseSparseStation = (s) => ({
type: 'station',
id: s.id + '',
name: normalizeEmpty(s.name),
})

const parseStation = (s) => ({
...parseSparseStation(s),
location: {
type: 'location',
longitude: normalizeEmpty(s.coordinates.longitude),
latitude: normalizeEmpty(s.coordinates.latitude),
address: normalizeEmpty(s.full_address),
street: normalizeEmpty(s.address),
},
importance: Number.isInteger(s.importance_order) ? normalizeEmpty(+s.importance_order) : null,
})

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