Files
klz-cables.com/.pnpm-store/v10/files/9a/338799aff0cec062e4d8858431175c045f574fb59360019a678d500869fdeab7e4e76fb0dc29ed64057b1d17238aae59edc342975c99469c19bd88c38d11bb
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

68 lines
2.5 KiB
Plaintext

"use strict";
exports.roundToNearestMinutes = roundToNearestMinutes;
var _index = require("./_lib/getRoundingMethod.cjs");
var _index2 = require("./constructFrom.cjs");
var _index3 = require("./toDate.cjs");
/**
* The {@link roundToNearestMinutes} function options.
*/
/**
* @name roundToNearestMinutes
* @category Minute Helpers
* @summary Rounds the given date to the nearest minute
*
* @description
* Rounds the given date to the nearest minute (or number of minutes).
* Rounds up when the given date is exactly between the nearest round minutes.
*
* @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 ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
*
* @param date - The date to round
* @param options - An object with options.
*
* @returns The new date rounded to the closest minute
*
* @example
* // Round 10 July 2014 12:12:34 to nearest minute:
* const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
* //=> Thu Jul 10 2014 12:13:00
*
* @example
* // Round 10 July 2014 12:12:34 to nearest quarter hour:
* const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 })
* //=> Thu Jul 10 2014 12:15:00
*
* @example
* // Floor (rounds down) 10 July 2014 12:12:34 to nearest minute:
* const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'floor' })
* //=> Thu Jul 10 2014 12:12:00
*
* @example
* // Ceil (rounds up) 10 July 2014 12:12:34 to nearest half hour:
* const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'ceil', nearestTo: 30 })
* //=> Thu Jul 10 2014 12:30:00
*/
function roundToNearestMinutes(date, options) {
const nearestTo = options?.nearestTo ?? 1;
if (nearestTo < 1 || nearestTo > 30)
return (0, _index2.constructFrom)(date, NaN);
const date_ = (0, _index3.toDate)(date, options?.in);
const fractionalSeconds = date_.getSeconds() / 60;
const fractionalMilliseconds = date_.getMilliseconds() / 1000 / 60;
const minutes =
date_.getMinutes() + fractionalSeconds + fractionalMilliseconds;
const method = options?.roundingMethod ?? "round";
const roundingMethod = (0, _index.getRoundingMethod)(method);
const roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo;
date_.setMinutes(roundedMinutes, 0, 0);
return date_;
}