min.mjs 996 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name min
  4. * @category Common Helpers
  5. * @summary Returns the earliest of the given dates.
  6. *
  7. * @description
  8. * Returns the earliest of the given dates.
  9. *
  10. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  11. *
  12. * @param dates - The dates to compare
  13. *
  14. * @returns The earliest of the dates
  15. *
  16. * @example
  17. * // Which of these dates is the earliest?
  18. * const result = min([
  19. * new Date(1989, 6, 10),
  20. * new Date(1987, 1, 11),
  21. * new Date(1995, 6, 2),
  22. * new Date(1990, 0, 1)
  23. * ])
  24. * //=> Wed Feb 11 1987 00:00:00
  25. */
  26. export function min(dates) {
  27. let result;
  28. dates.forEach((dirtyDate) => {
  29. const date = toDate(dirtyDate);
  30. if (!result || result > date || isNaN(+date)) {
  31. result = date;
  32. }
  33. });
  34. return result || new Date(NaN);
  35. }
  36. // Fallback for modularized imports:
  37. export default min;