{"ast":null,"code":"import { constructFrom } from \"./constructFrom.mjs\";\nimport { toDate } from \"./toDate.mjs\";\n\n/**\n * @name closestTo\n * @category Common Helpers\n * @summary Return a date from the array closest to the given date.\n *\n * @description\n * Return a date from the array closest to the given date.\n *\n * @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).\n *\n * @param dateToCompare - The date to compare with\n * @param dates - The array to search\n *\n * @returns The date from the array closest to the given date or undefined if no valid value is given\n *\n * @example\n * // Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030?\n * const dateToCompare = new Date(2015, 8, 6)\n * const result = closestTo(dateToCompare, [\n *   new Date(2000, 0, 1),\n *   new Date(2030, 0, 1)\n * ])\n * //=> Tue Jan 01 2030 00:00:00\n */\nexport function closestTo(dateToCompare, dates) {\n  const date = toDate(dateToCompare);\n  if (isNaN(Number(date))) return constructFrom(dateToCompare, NaN);\n  const timeToCompare = date.getTime();\n  let result;\n  let minDistance;\n  dates.forEach(dirtyDate => {\n    const currentDate = toDate(dirtyDate);\n    if (isNaN(Number(currentDate))) {\n      result = constructFrom(dateToCompare, NaN);\n      minDistance = NaN;\n      return;\n    }\n    const distance = Math.abs(timeToCompare - currentDate.getTime());\n    if (result == null || distance < minDistance) {\n      result = currentDate;\n      minDistance = distance;\n    }\n  });\n  return result;\n}\n\n// Fallback for modularized imports:\nexport default closestTo;","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}