import type { Resource } from "@src/tems"; function isValidDateValue(value: unknown): value is string | number | Date { return !Number.isNaN(new Date((value as Date)).getTime()); } const checkDateIsAfterRecursive = ( data: any, propName: string, thresholdDate: Date ): boolean => { if (data === null || data === undefined) { return false; } const propPath = propName.split("."); let current = data; for (const segment of propPath) { if (current && typeof current === "object" && segment in current) { current = current[segment]; } else { return false; } } if (isValidDateValue(current)) { const date = new Date(current); if (date > thresholdDate) { return true; } } else { return false; } if (Array.isArray(data)) { return data.some((item) => checkDateIsAfterRecursive(item, propName, thresholdDate) ); } if (typeof data === "object") { return Object.entries(data).some(([key, value]) => { if (key === propName && isValidDateValue(value)) { return false; } return checkDateIsAfterRecursive(value, propName, thresholdDate); }); } return false; }; const filterObjectByDateAfter = ( array: Resource[], propName: string, thresholdDateString: string ): Resource[] => { if ( !propName || !thresholdDateString || typeof thresholdDateString !== "string" ) { return array; } const thresholdDate = new Date(thresholdDateString); if (Number.isNaN(thresholdDate.getTime())) { console.warn(`Invalid threshold date provided: ${thresholdDateString}`); return array; } return array.filter((obj) => checkDateIsAfterRecursive(obj, propName, thresholdDate) ); }; export default filterObjectByDateAfter;