| import * as chrono from "chrono-node"; |
| import { format } from "date-fns"; |
|
|
| |
| |
| |
| |
| function isValidDate(year: number, month: number, day: number): boolean { |
| const date = new Date(year, month - 1, day); |
| return ( |
| date.getFullYear() === year && |
| date.getMonth() === month - 1 && |
| date.getDate() === day |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function formatDate(dateString: string): string | undefined { |
| if (!dateString?.trim()) return undefined; |
|
|
| const trimmed = dateString.trim(); |
|
|
| |
| |
| const isoMatch = trimmed.match(/^(\d{4})-(\d{2})-(\d{2})/); |
| if (isoMatch) { |
| const [, year, month, day] = isoMatch; |
| const y = Number(year); |
| const m = Number(month); |
| const d = Number(day); |
| |
| if (isValidDate(y, m, d)) { |
| return `${year}-${month}-${day}`; |
| } |
| |
| return undefined; |
| } |
|
|
| |
| const parsed = chrono.parseDate(trimmed); |
| if (!parsed) return undefined; |
|
|
| return format(parsed, "yyyy-MM-dd"); |
| } |
|
|
| export function formatAmountValue({ |
| amount, |
| inverted, |
| }: { |
| amount: string; |
| inverted?: boolean; |
| }) { |
| let value: number; |
|
|
| |
| const normalizedAmount = amount.replace(/−/g, "-"); |
|
|
| if (normalizedAmount.includes(",")) { |
| |
| value = +normalizedAmount.replace(/\./g, "").replace(",", "."); |
| } else if (normalizedAmount.match(/\.\d{2}$/)) { |
| |
| value = +normalizedAmount.replace(/\.(?=\d{3})/g, ""); |
| } else { |
| |
| value = +normalizedAmount; |
| } |
|
|
| if (inverted) { |
| return +(value * -1); |
| } |
|
|
| return value; |
| } |
|
|