Files
Bus-Infotainment--IBus-/lib/utils/NameBeautify.dart
ImBenji c2ebac5bb5 shtuff
2024-05-01 19:28:51 +01:00

74 lines
2.1 KiB
Dart

import 'dart:math';
import 'package:intl/intl.dart';
class NameBeautify {
static final Map<String, String> Longify = {
"ctr": "Centre",
"stn": "Station",
"tn": "Town",
};
static String beautifyStopName(String label) {
String stopName = label.toUpperCase();
// remove <>
stopName = stopName.replaceAll("<>", "");
// remove any parathesese pairs as well as the contents (), [], {}, <>, ><
stopName = stopName.replaceAll(RegExp(r'\(.*\)'), '');
stopName = stopName.replaceAll(RegExp(r'\[.*\]'), '');
stopName = stopName.replaceAll(RegExp(r'\{.*\}'), '');
// stopName = stopName.replaceAll(RegExp(r'\<.*\>'), '');
stopName = stopName.replaceAll(RegExp(r'\>.*\<'), '');
// remove any special characters except & and / and -
stopName = stopName.replaceAll(RegExp(r'[^a-zA-Z0-9&\/\- ]'), '');
// remove any double spaces
stopName = stopName.replaceAll(RegExp(r' '), ' ');
// remove any spaces at the start or end of the string
stopName = stopName.trim();
// replace any short words with their long form
for (String phrase in Longify.keys) {
stopName = stopName.replaceAll(RegExp(phrase, caseSensitive: false), Longify[phrase]!);
}
// remove any spaces at the start or end of the string
stopName = stopName.trim();
stopName = stopName.replaceAll(' ', ' ');
stopName = stopName.toLowerCase();
// Capitalify the first letter of each word
try {
stopName = stopName.split(' ').map((word) => word[0].toUpperCase() + word.substring(1)).join(' ');
} catch (e) {
print("Error capitalifying stop name: $stopName");
}
return stopName;
}
static String getShortTime(){
// return the HH:MM with AM and PM and make sure that the hour is 12 hour format and it always double digits. IE 01, 02 etc
DateTime now = DateTime.now();
String formatted = DateFormat('hh:mm a').format(now);
return formatted;
}
static String getLongTime() {
DateTime now = DateTime.now();
String formattedTime = DateFormat('HH:mm:ss dd.MM.yyyy').format(now);
return formattedTime;
}
}