ctucx.git: trainsearch

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

commit 23261fbca1110c0320d0e291cafd71a2245764d4
parent ecfa26d8a354088513c43e5c424a423a51977090
Author: Yureka <yuka@yuka.dev>
Date: Tue, 27 Dec 2022 22:43:09 +0100

format train type
2 files changed, 21 insertions(+), 18 deletions(-)
M
src/canvas.js
|
20
++------------------
M
src/helpers.js
|
19
+++++++++++++++++++
diff --git a/src/canvas.js b/src/canvas.js
@@ -1,6 +1,6 @@
 import { moreJourneys } from './journeysView.js';
 import { go } from './router.js';
-import { padZeros } from './helpers.js';
+import { padZeros, formatTrainTypes } from './helpers.js';
 import { cachedCoachSequence, coachSequenceCache, coachSequenceCacheKey } from './reihung';
 
 const formatTime = (date) => {

@@ -73,23 +73,7 @@ const typeTextsFor = leg => {
 	if (!info || info instanceof Promise) {
 		return [];
 	}
-	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;
-	});
+	return formatTrainTypes(info).split(" + ");
 };
 
 export const setupCanvas = (data, isUpdate) => {
diff --git a/src/helpers.js b/src/helpers.js
@@ -103,3 +103,22 @@ export const formatPrice = price => {
 	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(" + ");
+};