9f185ff6fd4fae200bbef703dfba6a0d0ebaaf2b362e5aef74ad101ee3b0befa.json 5.9 KB

1
  1. {"ast":null,"code":"import toDate from \"../toDate/index.js\";\nimport requiredArgs from \"../_lib/requiredArgs/index.js\";\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 * @param {Date | Number} dateToCompare - the date to compare with\n * @param {Array<Date> | Array<number>} datesArray - the array to search\n * @returns {Date | undefined} the date from the array closest to the given date or undefined if no valid value is given\n * @throws {TypeError} 2 arguments required\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 default function closestTo(dirtyDateToCompare, dirtyDatesArray) {\n requiredArgs(2, arguments);\n var dateToCompare = toDate(dirtyDateToCompare);\n if (isNaN(Number(dateToCompare))) return new Date(NaN);\n var timeToCompare = dateToCompare.getTime();\n var datesArray;\n // `dirtyDatesArray` is undefined or null\n if (dirtyDatesArray == null) {\n datesArray = [];\n\n // `dirtyDatesArray` is Array, Set or Map, or object with custom `forEach` method\n } else if (typeof dirtyDatesArray.forEach === 'function') {\n datesArray = dirtyDatesArray;\n\n // If `dirtyDatesArray` is Array-like Object, convert to Array. Otherwise, make it empty Array\n } else {\n datesArray = Array.prototype.slice.call(dirtyDatesArray);\n }\n var result;\n var minDistance;\n datesArray.forEach(function (dirtyDate) {\n var currentDate = toDate(dirtyDate);\n if (isNaN(Number(currentDate))) {\n result = new Date(NaN);\n minDistance = NaN;\n return;\n }\n var distance = Math.abs(timeToCompare - currentDate.getTime());\n if (result == null || distance < Number(minDistance)) {\n result = currentDate;\n minDistance = distance;\n }\n });\n return result;\n}","map":{"version":3,"names":["toDate","requiredArgs","closestTo","dirtyDateToCompare","dirtyDatesArray","arguments","dateToCompare","isNaN","Number","Date","NaN","timeToCompare","getTime","datesArray","forEach","Array","prototype","slice","call","result","minDistance","dirtyDate","currentDate","distance","Math","abs"],"sources":["F:/workspace/202226701027/huinongbao-app/node_modules/date-fns/esm/closestTo/index.js"],"sourcesContent":["import toDate from \"../toDate/index.js\";\nimport requiredArgs from \"../_lib/requiredArgs/index.js\";\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 * @param {Date | Number} dateToCompare - the date to compare with\n * @param {Array<Date> | Array<number>} datesArray - the array to search\n * @returns {Date | undefined} the date from the array closest to the given date or undefined if no valid value is given\n * @throws {TypeError} 2 arguments required\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 default function closestTo(dirtyDateToCompare, dirtyDatesArray) {\n requiredArgs(2, arguments);\n var dateToCompare = toDate(dirtyDateToCompare);\n if (isNaN(Number(dateToCompare))) return new Date(NaN);\n var timeToCompare = dateToCompare.getTime();\n var datesArray;\n // `dirtyDatesArray` is undefined or null\n if (dirtyDatesArray == null) {\n datesArray = [];\n\n // `dirtyDatesArray` is Array, Set or Map, or object with custom `forEach` method\n } else if (typeof dirtyDatesArray.forEach === 'function') {\n datesArray = dirtyDatesArray;\n\n // If `dirtyDatesArray` is Array-like Object, convert to Array. Otherwise, make it empty Array\n } else {\n datesArray = Array.prototype.slice.call(dirtyDatesArray);\n }\n var result;\n var minDistance;\n datesArray.forEach(function (dirtyDate) {\n var currentDate = toDate(dirtyDate);\n if (isNaN(Number(currentDate))) {\n result = new Date(NaN);\n minDistance = NaN;\n return;\n }\n var distance = Math.abs(timeToCompare - currentDate.getTime());\n if (result == null || distance < Number(minDistance)) {\n result = currentDate;\n minDist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