max.cjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. exports.max = max;
  3. var _index = require("./constructFrom.cjs");
  4. var _index2 = require("./toDate.cjs");
  5. /**
  6. * The {@link max} function options.
  7. */
  8. /**
  9. * @name max
  10. * @category Common Helpers
  11. * @summary Return the latest of the given dates.
  12. *
  13. * @description
  14. * Return the latest of the given dates.
  15. *
  16. * @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).
  17. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  18. *
  19. * @param dates - The dates to compare
  20. *
  21. * @returns The latest of the dates
  22. *
  23. * @example
  24. * // Which of these dates is the latest?
  25. * const result = max([
  26. * new Date(1989, 6, 10),
  27. * new Date(1987, 1, 11),
  28. * new Date(1995, 6, 2),
  29. * new Date(1990, 0, 1)
  30. * ])
  31. * //=> Sun Jul 02 1995 00:00:00
  32. */
  33. function max(dates, options) {
  34. let result;
  35. let context = options?.in;
  36. dates.forEach((date) => {
  37. // Use the first date object as the context function
  38. if (!context && typeof date === "object")
  39. context = _index.constructFrom.bind(null, date);
  40. const date_ = (0, _index2.toDate)(date, context);
  41. if (!result || result < date_ || isNaN(+date_)) result = date_;
  42. });
  43. return (0, _index.constructFrom)(context, result || NaN);
  44. }