simple-hafas/test.js
2019-11-01 18:59:48 +01:00

52 lines
1.3 KiB
JavaScript

const createClient = require('hafas-client')
const insaProfile = require('hafas-client/p/insa')
const client = createClient(insaProfile, 'my-awesome-program')
var queryPromise = client.nearby({
type: 'location',
latitude: 51.33157,
longitude: 12.40679
}, {
distance: 400
})
async function getNearestStationDepartures() {
var result = await queryPromise
printResult(client.departures(result[0]['id']))
}
function getTime(stringTime) {
let d = new Date(stringTime)
return String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0")
}
function cutPadString(string, length) {
let outstring = ""
if (string.length > length) {
outstring = string.substring(0, length - 3) + "..."
} else {
if (string.length < length) {
outstring = string.padEnd(length)
} else {
outstring = string
}
}
return outstring
}
async function printResult(departures) {
result = await departures
result.forEach(element => {
let delay = ""
if (element['delay'] != 0) {
delay = "(+" + element['delay'] / 60 + ")"
}
console.log(cutPadString(element['line']['id'], 4) + " " + cutPadString(element['direction'], 20) + " " + cutPadString(getTime(element['when']), 6) + delay)
});
}
getNearestStationDepartures()