Typescript array min/max dates

Date: 2023-07-13
const getMin = <T extends number>(src: T[]) => src.length == 0 ? undefined : Math.min(...src);
const getMax = <T extends number>(src: T[]) => src.length == 0 ? undefined : Math.max(...src);

const getDateFromNumber = (n?: number) => n ? new Date(n) : undefined;

const getMinDate = (src: Date[]) => getDateFromNumber(getMin(src.map(x => x.getTime())));
const getMaxDate = (src: Date[]) => getDateFromNumber(getMax(src.map(x => x.getTime())));


const testDates = [new Date(2023, 3, 6), new Date(2022, 3, 6), new Date(2024, 3, 6)];
console.log(getMinDate(testDates));
console.log(getMaxDate(testDates));
79040cookie-checkTypescript array min/max dates