feat: enhance date parsing to support month-only formats and normalize URLs to prevent duplicates
This commit is contained in:
33
src/App.jsx
33
src/App.jsx
@@ -31,8 +31,37 @@ const DATE_RANGE_OPTIONS = [
|
||||
|
||||
const parseDate = (value) => {
|
||||
if (!value) return null;
|
||||
const date = new Date(value);
|
||||
return Number.isNaN(date.getTime()) ? null : date;
|
||||
|
||||
const toDate = (input) => {
|
||||
const date = new Date(input);
|
||||
return Number.isNaN(date.getTime()) ? null : date;
|
||||
};
|
||||
|
||||
let date = value instanceof Date ? value : null;
|
||||
|
||||
if (!date && (typeof value === "number" || typeof value === "string")) {
|
||||
date = toDate(value);
|
||||
|
||||
if (!date && typeof value === "string") {
|
||||
const trimmed = value.trim();
|
||||
const dayMatch = trimmed.match(/^([0-9]{1,2})[\/\-]([0-9]{1,2})[\/\-]([0-9]{2,4})$/);
|
||||
|
||||
if (dayMatch) {
|
||||
const [, day, month, year] = dayMatch;
|
||||
const normalizedYear = year.length === 2 ? `20${year}` : year;
|
||||
date = toDate(`${normalizedYear.padStart(4, "0")}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`);
|
||||
} else {
|
||||
const monthMatch = trimmed.match(/^([0-9]{1,2})[\/\-]([0-9]{2,4})$/);
|
||||
if (monthMatch) {
|
||||
const [, month, year] = monthMatch;
|
||||
const normalizedYear = year.length === 2 ? `20${year}` : year;
|
||||
date = toDate(`${normalizedYear.padStart(4, "0")}-${month.padStart(2, "0")}-01`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return date;
|
||||
};
|
||||
|
||||
const matchesDateRange = (annonce, range) => {
|
||||
|
||||
Reference in New Issue
Block a user