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,7 +1,17 @@
"use strict";
exports.closestTo = closestTo;
var _index = require("./constructFrom.js");
var _index2 = require("./toDate.js");
import { normalizeDates } from "./_lib/normalizeDates.js";
import { closestIndexTo } from "./closestIndexTo.js";
import { constructFrom } from "./constructFrom.js";
/**
* The {@link closestTo} function options.
*/
/**
* The {@link closestTo} function result type. It resolves the proper data type.
* It uses the first argument date object type, starting from the date argument,
* then the start interval date, and finally the end interval date. If
* a context function is passed, it uses the context function return type.
*/
/**
* @name closestTo
@@ -11,7 +21,9 @@ var _index2 = require("./toDate.js");
* @description
* Return a date from the array closest 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).
* @typeParam DateToCompare - Date to compare argument type.
* @typeParam DatesType - Dates array argument type.
* @typeParam Options - Options type.
*
* @param dateToCompare - The date to compare with
* @param dates - The array to search
@@ -27,30 +39,20 @@ var _index2 = require("./toDate.js");
* ])
* //=> Tue Jan 01 2030 00:00:00
*/
function closestTo(dateToCompare, dates) {
const date = (0, _index2.toDate)(dateToCompare);
export function closestTo(dateToCompare, dates, options) {
const [dateToCompare_, ...dates_] = normalizeDates(
options?.in,
dateToCompare,
...dates,
);
if (isNaN(Number(date))) return (0, _index.constructFrom)(dateToCompare, NaN);
const index = closestIndexTo(dateToCompare_, dates_);
const timeToCompare = date.getTime();
if (typeof index === "number" && isNaN(index))
return constructFrom(dateToCompare_, NaN);
let result;
let minDistance;
dates.forEach((dirtyDate) => {
const currentDate = (0, _index2.toDate)(dirtyDate);
if (isNaN(Number(currentDate))) {
result = (0, _index.constructFrom)(dateToCompare, NaN);
minDistance = NaN;
return;
}
const distance = Math.abs(timeToCompare - currentDate.getTime());
if (result == null || distance < minDistance) {
result = currentDate;
minDistance = distance;
}
});
return result;
if (index !== undefined) return dates_[index];
}
// Fallback for modularized imports:
export default closestTo;