ctucx.git: trainsearch

web based trip-planner, fork of https://cyberchaos.dev/yuka/trainsearch

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
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 || "???";