mirror of
https://github.com/Dasemu/skipredictor.git
synced 2026-11-26 07:16:15 +00:00
94 lines
2.1 KiB
TypeScript
94 lines
2.1 KiB
TypeScript
const LONG_DATE_FORMATTER = new Intl.DateTimeFormat('es-ES', {
|
|
weekday: 'long',
|
|
day: 'numeric',
|
|
month: 'long',
|
|
})
|
|
|
|
const SHORT_DATE_FORMATTER = new Intl.DateTimeFormat('es-ES', {
|
|
day: 'numeric',
|
|
month: 'short',
|
|
})
|
|
|
|
const DATE_TIME_FORMATTER = new Intl.DateTimeFormat('es-ES', {
|
|
day: '2-digit',
|
|
month: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
})
|
|
|
|
export function parseLocalDate(dateString: string): Date {
|
|
const [year, month, day] = dateString.split('-').map(Number)
|
|
return new Date(year, month - 1, day, 12, 0, 0, 0)
|
|
}
|
|
|
|
export function toIsoDate(date: Date): string {
|
|
const year = date.getFullYear()
|
|
const month = `${date.getMonth() + 1}`.padStart(2, '0')
|
|
const day = `${date.getDate()}`.padStart(2, '0')
|
|
return `${year}-${month}-${day}`
|
|
}
|
|
|
|
export function formatLongDate(dateString: string): string {
|
|
return LONG_DATE_FORMATTER.format(parseLocalDate(dateString))
|
|
}
|
|
|
|
export function formatShortDate(dateString: string): string {
|
|
return SHORT_DATE_FORMATTER.format(parseLocalDate(dateString))
|
|
}
|
|
|
|
export function formatDateTime(timestamp: string | null): string {
|
|
if (!timestamp) {
|
|
return 'Sin sincronizar'
|
|
}
|
|
|
|
return DATE_TIME_FORMATTER.format(new Date(timestamp))
|
|
}
|
|
|
|
export function formatProbability(probability: number | null): string {
|
|
if (probability === null || Number.isNaN(probability)) {
|
|
return '--'
|
|
}
|
|
|
|
return `${Math.round(probability * 100)}%`
|
|
}
|
|
|
|
export function formatKilometers(km: number | null, maxKm: number | null): string {
|
|
if (km === null || Number.isNaN(km)) {
|
|
return '--'
|
|
}
|
|
|
|
if (maxKm === null || Number.isNaN(maxKm) || maxKm <= 0) {
|
|
return `${km.toFixed(1)} km`
|
|
}
|
|
|
|
return `${km.toFixed(1)} / ${maxKm.toFixed(1)} km`
|
|
}
|
|
|
|
export function crowdLabel(level: string | null): string {
|
|
if (!level) {
|
|
return 'Sin dato'
|
|
}
|
|
|
|
return (
|
|
{
|
|
low: 'Baja',
|
|
medium: 'Media',
|
|
high: 'Alta',
|
|
}[level] ?? level
|
|
)
|
|
}
|
|
|
|
export function weekdayLabel(weekday: number): string {
|
|
return (
|
|
{
|
|
0: 'Domingo',
|
|
1: 'Lunes',
|
|
2: 'Martes',
|
|
3: 'Miércoles',
|
|
4: 'Jueves',
|
|
5: 'Viernes',
|
|
6: 'Sábado',
|
|
}[weekday] ?? 'Desconocido'
|
|
)
|
|
}
|