registration

This commit is contained in:
User
2025-02-02 16:08:03 +03:00
parent 7f6495eb4d
commit 78afbaed71
6334 changed files with 196774 additions and 165754 deletions

View File

@@ -1,6 +1,4 @@
"use strict";
exports.closestIndexTo = closestIndexTo;
var _index = require("./toDate.js");
import { toDate } from "./toDate.js";
/**
* @name closestIndexTo
@@ -10,8 +8,6 @@ var _index = require("./toDate.js");
* @description
* Return an index of the closest date from the array comparing to the given date.
*
* @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).
*
* @param dateToCompare - The date to compare with
* @param dates - The array to search
*
@@ -28,25 +24,26 @@ var _index = require("./toDate.js");
* const result = closestIndexTo(dateToCompare, datesArray)
* //=> 1
*/
function closestIndexTo(dateToCompare, dates) {
const date = (0, _index.toDate)(dateToCompare);
export function closestIndexTo(dateToCompare, dates) {
// [TODO] It would be better to return -1 here rather than undefined, as this
// is how JS behaves, but it would be a breaking change, so we need
// to consider it for v4.
const timeToCompare = +toDate(dateToCompare);
if (isNaN(Number(date))) return NaN;
const timeToCompare = date.getTime();
if (isNaN(timeToCompare)) return NaN;
let result;
let minDistance;
dates.forEach(function (dirtyDate, index) {
const currentDate = (0, _index.toDate)(dirtyDate);
dates.forEach((date, index) => {
const date_ = toDate(date);
if (isNaN(Number(currentDate))) {
if (isNaN(+date_)) {
result = NaN;
minDistance = NaN;
return;
}
const distance = Math.abs(timeToCompare - currentDate.getTime());
const distance = Math.abs(timeToCompare - +date_);
if (result == null || distance < minDistance) {
result = index;
minDistance = distance;
@@ -55,3 +52,6 @@ function closestIndexTo(dateToCompare, dates) {
return result;
}
// Fallback for modularized imports:
export default closestIndexTo;