min.js 974 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. exports.min = min;
  3. var _index = require("./toDate.js");
  4. /**
  5. * @name min
  6. * @category Common Helpers
  7. * @summary Returns the earliest of the given dates.
  8. *
  9. * @description
  10. * Returns the earliest of the given dates.
  11. *
  12. * @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).
  13. *
  14. * @param dates - The dates to compare
  15. *
  16. * @returns The earliest of the dates
  17. *
  18. * @example
  19. * // Which of these dates is the earliest?
  20. * const result = min([
  21. * new Date(1989, 6, 10),
  22. * new Date(1987, 1, 11),
  23. * new Date(1995, 6, 2),
  24. * new Date(1990, 0, 1)
  25. * ])
  26. * //=> Wed Feb 11 1987 00:00:00
  27. */
  28. function min(dates) {
  29. let result;
  30. dates.forEach((dirtyDate) => {
  31. const date = (0, _index.toDate)(dirtyDate);
  32. if (!result || result > date || isNaN(+date)) {
  33. result = date;
  34. }
  35. });
  36. return result || new Date(NaN);
  37. }