Skip to content

Commit

Permalink
feat(latest): normalize Date before returning result
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
djcsdy committed Feb 16, 2024
1 parent 3695908 commit 96ed373
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,32 +694,46 @@ 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<T extends DateOptions, U extends DateOptions>(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);
}

/**
* Compares two dates and returns the later of the two.
*
* 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;

/**
* 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<T extends DateOptions, U extends DateOptions>(b: U): (a: T) => T | U {
export function latestFn(b: DateOptions): (a: DateOptions) => Date {
return a => latest(a, b);
}

/**
* 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;

Expand Down

0 comments on commit 96ed373

Please sign in to comment.