UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

38 lines (36 loc) • 1.1 kB
import { Temporal } from "../../internal/temporal.js"; import { plainTimeOf } from "../../internal/inputChecks.js"; // src/plain/calculate/minTime.ts /** * Return the earliest (minimum) of the given PlainTime values. * * - Returns null if the array is empty or contains no valid times. * - Validation is performed on each item in the array. * * @param times Array of ISO PlainTime strings (e.g. "14:30:00") * @returns The earliest time string, or null on invalid input * * @example minTime(["14:30:00", "09:00:00", "20:45:00"]) // "09:00:00" * @example minTime([]) // null */ function minTime(times) { try { if (!Array.isArray(times) || !times.length) return null; const valid = []; times.forEach((item) => { const parsed = plainTimeOf(item); if (parsed !== null) valid.push(parsed); }); if (!valid.length) return null; try { return valid.reduce((currentMin, candidate) => { return Temporal.PlainTime.compare(candidate, currentMin) < 0 ? candidate : currentMin; }, valid[0]).toString(); } catch { return null; } } catch { return null; } } export { minTime };