35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
import type { Stop } from '../../../lib/ridebus';
|
|
|
|
export type StopGroup = { key: string; name: string; stops: Stop[] };
|
|
|
|
export function groupStops(stops: Stop[]): StopGroup[] {
|
|
const groups = new Map<string, StopGroup>();
|
|
const seen = new Set<number>();
|
|
for (const stop of stops) {
|
|
if (seen.has(stop.id)) continue;
|
|
seen.add(stop.id);
|
|
const name = stop.name.trim().replace(/\s+/g, ' ');
|
|
const key = name.normalize('NFKC').toLocaleLowerCase('ru');
|
|
const group = groups.get(key) ?? { key, name, stops: [] };
|
|
group.stops.push(stop);
|
|
groups.set(key, group);
|
|
}
|
|
return Array.from(groups.values());
|
|
}
|
|
|
|
export function formatRemainingMinutes(
|
|
value: number,
|
|
labels: { now: string; hours: string; minutes: string }
|
|
): string {
|
|
const minutes = Math.max(0, Math.ceil(value));
|
|
if (!minutes) return labels.now;
|
|
const hours = Math.floor(minutes / 60);
|
|
const remainder = minutes % 60;
|
|
return [
|
|
hours ? `${hours} ${labels.hours}` : '',
|
|
remainder ? `${remainder} ${labels.minutes}` : '',
|
|
]
|
|
.filter(Boolean)
|
|
.join(' ');
|
|
}
|