max.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. exports.max = max;
  3. var _index = require("./toDate.js");
  4. /**
  5. * @name max
  6. * @category Common Helpers
  7. * @summary Return the latest of the given dates.
  8. *
  9. * @description
  10. * Return the latest 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 latest of the dates
  17. *
  18. * @example
  19. * // Which of these dates is the latest?
  20. * const result = max([
  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. * //=> Sun Jul 02 1995 00:00:00
  27. */
  28. function max(dates) {
  29. let result;
  30. dates.forEach(function (dirtyDate) {
  31. const currentDate = (0, _index.toDate)(dirtyDate);
  32. if (
  33. result === undefined ||
  34. result < currentDate ||
  35. isNaN(Number(currentDate))
  36. ) {
  37. result = currentDate;
  38. }
  39. });
  40. return result || new Date(NaN);
  41. }