Rocky_Mountain_Vending/.pnpm-store/v10/files/bf/29aa6cfc87fcf442ed19689c3c2dd9b5e49379c495c51476e5243260f0b64c1e5a8e9d8410ba39e2532d6c234843e9106510bf7b09a789312a5d779ce45552
DMleadgen 46d973904b
Initial commit: Rocky Mountain Vending website
Next.js website for Rocky Mountain Vending company featuring:
- Product catalog with Stripe integration
- Service areas and parts pages
- Admin dashboard with Clerk authentication
- SEO optimized pages with JSON-LD structured data

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-12 16:22:15 -07:00

56 lines
1.5 KiB
Text

"use strict";
exports.closestIndexTo = closestIndexTo;
var _index = require("./toDate.cjs");
/**
* @name closestIndexTo
* @category Common Helpers
* @summary Return an index of the closest date from the array comparing to the given date.
*
* @description
* Return an index of the closest date from the array comparing to the given date.
*
* @param dateToCompare - The date to compare with
* @param dates - The array to search
*
* @returns An index of the date closest to the given date or undefined if no valid value is given
*
* @example
* // Which date is closer to 6 September 2015?
* const dateToCompare = new Date(2015, 8, 6)
* const datesArray = [
* new Date(2015, 0, 1),
* new Date(2016, 0, 1),
* new Date(2017, 0, 1)
* ]
* const result = closestIndexTo(dateToCompare, datesArray)
* //=> 1
*/
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 = +(0, _index.toDate)(dateToCompare);
if (isNaN(timeToCompare)) return NaN;
let result;
let minDistance;
dates.forEach((date, index) => {
const date_ = (0, _index.toDate)(date);
if (isNaN(+date_)) {
result = NaN;
minDistance = NaN;
return;
}
const distance = Math.abs(timeToCompare - +date_);
if (result == null || distance < minDistance) {
result = index;
minDistance = distance;
}
});
return result;
}