index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = max;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _index = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. /**
  11. * @name max
  12. * @category Common Helpers
  13. * @summary Return the latest of the given dates.
  14. *
  15. * @description
  16. * Return the latest of the given dates.
  17. *
  18. * @param {Date[]|Number[]} datesArray - the dates to compare
  19. * @returns {Date} the latest of the dates
  20. * @throws {TypeError} 1 argument required
  21. *
  22. * @example
  23. * // Which of these dates is the latest?
  24. * const result = max([
  25. * new Date(1989, 6, 10),
  26. * new Date(1987, 1, 11),
  27. * new Date(1995, 6, 2),
  28. * new Date(1990, 0, 1)
  29. * ])
  30. * //=> Sun Jul 02 1995 00:00:00
  31. */
  32. function max(dirtyDatesArray) {
  33. (0, _index2.default)(1, arguments);
  34. var datesArray;
  35. // `dirtyDatesArray` is Array, Set or Map, or object with custom `forEach` method
  36. if (dirtyDatesArray && typeof dirtyDatesArray.forEach === 'function') {
  37. datesArray = dirtyDatesArray;
  38. // If `dirtyDatesArray` is Array-like Object, convert to Array.
  39. } else if ((0, _typeof2.default)(dirtyDatesArray) === 'object' && dirtyDatesArray !== null) {
  40. datesArray = Array.prototype.slice.call(dirtyDatesArray);
  41. } else {
  42. // `dirtyDatesArray` is non-iterable, return Invalid Date
  43. return new Date(NaN);
  44. }
  45. var result;
  46. datesArray.forEach(function (dirtyDate) {
  47. var currentDate = (0, _index.default)(dirtyDate);
  48. if (result === undefined || result < currentDate || isNaN(Number(currentDate))) {
  49. result = currentDate;
  50. }
  51. });
  52. return result || new Date(NaN);
  53. }
  54. module.exports = exports.default;