temporal-fns
API reference

closestIndexTo

API index · Input types and conventions · Installation

closestIndexTo(target, values) → number | undefined

Return the zero-based index selected by closestTo, or undefined for an empty array. Tied distances choose the first index.

import { closestIndexTo } from '@frantss/temporal-fns';

const target = Temporal.PlainYearMonth.from('2026-11');
closestIndexTo(target, [
  Temporal.PlainYearMonth.from('2026-10'),
  Temporal.PlainYearMonth.from('2026-12'),
]); // => 0
closestIndexTo(target, [
  Temporal.PlainYearMonth.from('2026-01'),
  Temporal.PlainYearMonth.from('2026-11'),
]); // => 1
closestIndexTo(target, []); // => undefined

Related: closestTo, min, max.

Behaviors: SEL-002: Closest selection uses the first occurrence in a distance tie; SEL-003: Empty collections return undefined; SEL-010: Mixed kinds are rejected throughout a collection; SEL-006: Closest exact instants retain nanosecond differences.