From 96ed373da385b4dc66957e965fd243964caaea2b Mon Sep 17 00:00:00 2001 From: Daniel Cassidy Date: Fri, 16 Feb 2024 04:13:39 +0000 Subject: [PATCH] feat(latest): normalize Date before returning result BREAKING CHANGE: The `latest` functions now throw `Error` if both specified Dates contain numeric fields that are non-finite. BREAKING CHANGE: The type parameters of the `latest` functions have been removed. These functions now always either return a valid Date or throw. --- index.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index 8c4a9b0..ca71532 100644 --- a/index.ts +++ b/index.ts @@ -694,9 +694,14 @@ export const earliestDateFn = earliestFn; /** * Compares two dates and returns the later of the two. + * + * @throws {Error} if both specified `Date`s contain numeric fields that + * are non-finite. */ -export function latest(a: T, b: U): T | U { - return before(a, b) ? b : a; +export function latest(a: DateOptions, b: DateOptions): Date { + const ad = toReferenceDays(a); + const bd = toReferenceDays(b); + return fromReferenceDays(ad > bd ? ad : bd); } /** @@ -704,6 +709,9 @@ export function latest(a: T, b: U) * * Alias of {@link latest}, useful for disambiguation from similar functions * that operate on other date/time types. + * + * @throws {Error} if both specified `Date`s contain numeric fields that + * are non-finite. */ export const latestDate = latest; @@ -711,8 +719,11 @@ export const latestDate = latest; * Compares two dates and returns the later of the two. * * Curried variant of {@link latest}. + * + * @throws {Error} if both specified `Date`s contain numeric fields that + * are non-finite. */ -export function latestFn(b: U): (a: T) => T | U { +export function latestFn(b: DateOptions): (a: DateOptions) => Date { return a => latest(a, b); } @@ -720,6 +731,9 @@ export function latestFn(b: U): (a * Compares two dates and returns the later of the two. * * Curried variant of {@link latestDate}. + * + * @throws {Error} if both specified `Date`s contain numeric fields that + * are non-finite. */ export const latestDateFn = latestFn;