import { ds100Name } from './app_functions.js'; import { padZeros } from './helpers.js'; import { languages } from './languages.js'; export const formatName = (point) => { switch (point.type) { case 'stop': case 'station': let nameHTML = point.name; const ds100 = ds100Name(point.id); if (ds100 !== null) nameHTML += ` (${ds100})`; return nameHTML; case 'location': if (point.address) return point.address; return point.name; default: return ''; }; }; export const formatDateTime = (date, format) => { if (format != null) { switch (format) { case 'full': return padZeros(date.getHours()) + ':' + padZeros(date.getMinutes()) + ', ' + date.getDate() + '.' + (date.getMonth() + 1) + '.' + date.getFullYear(); break; case 'date': return date.getDate() + '.' + (date.getMonth() + 1) + '.' + date.getFullYear(); break; case 'time': return `${padZeros(date.getHours())}:${padZeros(date.getMinutes())}`; break; default: return false; break; } } if (date.toLocaleDateString() !== new Date().toLocaleDateString()) { return padZeros(date.getHours()) + ':' + padZeros(date.getMinutes()) + ', ' + date.getDate() + '.' + (date.getMonth() + 1) + '.'; } else { return padZeros(date.getHours()) + ':' + padZeros(date.getMinutes()); } }; export const formatDuration = (duration) => { const mins = duration / 60000; const h = Math.floor(mins / 60); const m = mins % 60; if (h > 0) return h+'h '+m+'min'; return m+'min'; }; export const formatFromTo = obj => { if (obj.type === 'stop' || obj.type === 'station') return obj.id; else if (obj.address) return { latitude: obj.latitude, longitude: obj.longitude, address: obj.address, }; else return { id: obj.id, latitude: obj.latitude, longitude: obj.longitude, }; }; export const formatPrice = price => { if (!price) return '-'; const currencies = { USD: '$', EUR: '€', GBP: '£' }; let ret = currencies[price.currency] || price.currency; ret += `${Math.floor(price.amount)}.${padZeros(price.amount * 100 % 100, 2)}`; return ret; }; export const formatTrainTypes = info => { const counts = {}; for (let group of info.sequence?.groups) { const name = group.baureihe?.name; if (!name) continue; counts[name] = (counts[name] ? counts[name] : 0) + 1; } return Object.entries(counts).map(([name, count]) => { let text = ""; if (count > 1) text += `${count} x `; text += name; while (text.length < 12) { text = ' ' + text + ' '; } return text; }).join(" + "); }; export const formatLineAdditionalName = (line) => { if (!line.name) return null; const splitName = line.name.split(' '); if (splitName.length === 2 && line.fahrtNr && line.fahrtNr != splitName[1]) { return `${splitName[0]} ${line.fahrtNr}`; } else { return null; } }; export const formatLineDisplayName = (line) => line?.name || line?.operator?.name || "???";