index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = closestTo;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name closestTo
  11. * @category Common Helpers
  12. * @summary Return a date from the array closest to the given date.
  13. *
  14. * @description
  15. * Return a date from the array closest to the given date.
  16. *
  17. * ### v2.0.0 breaking changes:
  18. *
  19. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  20. *
  21. * - Now, `closestTo` doesn't throw an exception
  22. * when the second argument is not an array, and returns Invalid Date instead.
  23. *
  24. * @param {Date|Number} dateToCompare - the date to compare with
  25. * @param {Date[]|Number[]} datesArray - the array to search
  26. * @returns {Date} the date from the array closest to the given date
  27. * @throws {TypeError} 2 arguments required
  28. *
  29. * @example
  30. * // Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030?
  31. * var dateToCompare = new Date(2015, 8, 6)
  32. * var result = closestTo(dateToCompare, [
  33. * new Date(2000, 0, 1),
  34. * new Date(2030, 0, 1)
  35. * ])
  36. * //=> Tue Jan 01 2030 00:00:00
  37. */
  38. function closestTo(dirtyDateToCompare, dirtyDatesArray) {
  39. (0, _index2.default)(2, arguments);
  40. var dateToCompare = (0, _index.default)(dirtyDateToCompare);
  41. if (isNaN(dateToCompare)) {
  42. return new Date(NaN);
  43. }
  44. var timeToCompare = dateToCompare.getTime();
  45. var datesArray; // `dirtyDatesArray` is undefined or null
  46. if (dirtyDatesArray == null) {
  47. datesArray = []; // `dirtyDatesArray` is Array, Set or Map, or object with custom `forEach` method
  48. } else if (typeof dirtyDatesArray.forEach === 'function') {
  49. datesArray = dirtyDatesArray; // If `dirtyDatesArray` is Array-like Object, convert to Array. Otherwise, make it empty Array
  50. } else {
  51. datesArray = Array.prototype.slice.call(dirtyDatesArray);
  52. }
  53. var result;
  54. var minDistance;
  55. datesArray.forEach(function (dirtyDate) {
  56. var currentDate = (0, _index.default)(dirtyDate);
  57. if (isNaN(currentDate)) {
  58. result = new Date(NaN);
  59. minDistance = NaN;
  60. return;
  61. }
  62. var distance = Math.abs(timeToCompare - currentDate.getTime());
  63. if (result == null || distance < minDistance) {
  64. result = currentDate;
  65. minDistance = distance;
  66. }
  67. });
  68. return result;
  69. }
  70. module.exports = exports.default;