index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = closestIndexTo;
  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 closestIndexTo
  11. * @category Common Helpers
  12. * @summary Return an index of the closest date from the array comparing to the given date.
  13. *
  14. * @description
  15. * Return an index of the closest date from the array comparing 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, `closestIndexTo` 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 {Number} an index of the date closest to the given date
  27. * @throws {TypeError} 2 arguments required
  28. *
  29. * @example
  30. * // Which date is closer to 6 September 2015?
  31. * var dateToCompare = new Date(2015, 8, 6)
  32. * var datesArray = [
  33. * new Date(2015, 0, 1),
  34. * new Date(2016, 0, 1),
  35. * new Date(2017, 0, 1)
  36. * ]
  37. * var result = closestIndexTo(dateToCompare, datesArray)
  38. * //=> 1
  39. */
  40. function closestIndexTo(dirtyDateToCompare, dirtyDatesArray) {
  41. (0, _index2.default)(2, arguments);
  42. var dateToCompare = (0, _index.default)(dirtyDateToCompare);
  43. if (isNaN(dateToCompare)) {
  44. return NaN;
  45. }
  46. var timeToCompare = dateToCompare.getTime();
  47. var datesArray; // `dirtyDatesArray` is undefined or null
  48. if (dirtyDatesArray == null) {
  49. datesArray = []; // `dirtyDatesArray` is Array, Set or Map, or object with custom `forEach` method
  50. } else if (typeof dirtyDatesArray.forEach === 'function') {
  51. datesArray = dirtyDatesArray; // If `dirtyDatesArray` is Array-like Object, convert to Array. Otherwise, make it empty Array
  52. } else {
  53. datesArray = Array.prototype.slice.call(dirtyDatesArray);
  54. }
  55. var result;
  56. var minDistance;
  57. datesArray.forEach(function (dirtyDate, index) {
  58. var currentDate = (0, _index.default)(dirtyDate);
  59. if (isNaN(currentDate)) {
  60. result = NaN;
  61. minDistance = NaN;
  62. return;
  63. }
  64. var distance = Math.abs(timeToCompare - currentDate.getTime());
  65. if (result == null || distance < minDistance) {
  66. result = index;
  67. minDistance = distance;
  68. }
  69. });
  70. return result;
  71. }
  72. module.exports = exports.default;