mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-18 02:31:56 +10:00
refactor(v4.0.0-alpha): beginning of a new era
This commit is contained in:
39
libs/utils/src/namespaces/date.ts
Normal file
39
libs/utils/src/namespaces/date.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
export const sortByDate = <T>(a: T, b: T, key: keyof T, desc = true) => {
|
||||
if (!a[key] || !b[key]) return 0;
|
||||
if (!(a[key] instanceof Date) || !(b[key] instanceof Date)) return 0;
|
||||
|
||||
if (desc) return dayjs(a[key] as Date).isBefore(dayjs(b[key] as Date)) ? 1 : -1;
|
||||
else return dayjs(a[key] as Date).isBefore(dayjs(b[key] as Date)) ? -1 : 1;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export const deepSearchAndParseDates = (obj: any, dateKeys: string[]): any => {
|
||||
if (typeof obj !== "object" || obj === null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
const keys = Object.keys(obj);
|
||||
|
||||
if (keys.length === 0) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
for (const key of keys) {
|
||||
let value = obj[key];
|
||||
|
||||
if (dateKeys.includes(key)) {
|
||||
if (typeof value === "string") {
|
||||
const parsedDate = new Date(value);
|
||||
if (!isNaN(parsedDate.getTime())) {
|
||||
value = parsedDate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
obj[key] = deepSearchAndParseDates(value, dateKeys);
|
||||
}
|
||||
|
||||
return obj;
|
||||
};
|
||||
Reference in New Issue
Block a user