closestIndexTo.d.ts 1.1 KB

123456789101112131415161718192021222324252627282930
  1. /**
  2. * @name closestIndexTo
  3. * @category Common Helpers
  4. * @summary Return an index of the closest date from the array comparing to the given date.
  5. *
  6. * @description
  7. * Return an index of the closest date from the array comparing to the given date.
  8. *
  9. * @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).
  10. *
  11. * @param dateToCompare - The date to compare with
  12. * @param dates - The array to search
  13. *
  14. * @returns An index of the date closest to the given date or undefined if no valid value is given
  15. *
  16. * @example
  17. * // Which date is closer to 6 September 2015?
  18. * const dateToCompare = new Date(2015, 8, 6)
  19. * const datesArray = [
  20. * new Date(2015, 0, 1),
  21. * new Date(2016, 0, 1),
  22. * new Date(2017, 0, 1)
  23. * ]
  24. * const result = closestIndexTo(dateToCompare, datesArray)
  25. * //=> 1
  26. */
  27. export declare function closestIndexTo<DateType extends Date>(
  28. dateToCompare: DateType | number | string,
  29. dates: Array<DateType | number | string>,
  30. ): number | undefined;