API reference
closestTo
API index · Input types and conventions · Installation
closestTo(target, values) → input kind | undefined
Select the value at the smallest absolute distance from target. Target and candidates must share a supported kind, even when another candidate already matches exactly. Ties retain the first candidate.
import { closestTo } from '@frantss/temporal-fns';
const target = Temporal.PlainTime.from('12:00');
const first = Temporal.PlainTime.from('11:00');
const second = Temporal.PlainTime.from('13:00');
closestTo(target, [first, second]) === first; // => true
closestTo(target, []); // => undefined
closestTo(Temporal.PlainTime.from('23:59'), [
Temporal.PlainTime.from('00:00'),
Temporal.PlainTime.from('22:00'),
])?.toString(); // => '22:00:00'Related: closestIndexTo, min, max.
Behaviors: SEL-002: Closest selection uses the first occurrence in a distance tie; SEL-003: Empty collections return undefined; SEL-006: Closest exact instants retain nanosecond differences; SEL-007: Clock distance is linear and never wraps across midnight; SEL-009: Plain date-time distance is wall-clock distance; SEL-010: Mixed kinds are rejected throughout a collection.