index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = min;
  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 min
  12. * @category Common Helpers
  13. * @summary Returns the earliest of the given dates.
  14. *
  15. * @description
  16. * Returns the earliest of the given dates.
  17. *
  18. * @param {Date[]|Number[]} datesArray - the dates to compare
  19. * @returns {Date} - the earliest of the dates
  20. * @throws {TypeError} 1 argument required
  21. *
  22. * @example
  23. * // Which of these dates is the earliest?
  24. * const result = min([
  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. * //=> Wed Feb 11 1987 00:00:00
  31. */
  32. function min(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(currentDate.getDate())) {
  49. result = currentDate;
  50. }
  51. });
  52. return result || new Date(NaN);
  53. }
  54. module.exports = exports.default;