Skip to content

Commit

Permalink
feat(latest): normalize DateTime 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 DateTimes
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
DateTime or throw.
  • Loading branch information
djcsdy committed Feb 16, 2024
1 parent dca17a6 commit 0b4063a
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,29 +578,41 @@ export function earliestFn(b: DateTimeOptions): (a: DateTimeOptions) => DateTime
* Curried variant of {@link earliestDateTime}. */
export const earliestDateTimeFn = earliestFn;

/** Compares two {@link DateTime}s and returns the later of the two. */
export function latest<T extends DateTimeOptions, U extends DateTimeOptions>(a: T, b: U): T | U {
return before(a, b) ? b : a;
/** Compares two {@link DateTime}s and returns the later of the two.
*
* @throws {Error} if both specified `DateTime`s contain numeric fields that
* are non-finite. */
export function latest(a: DateTimeOptions, b: DateTimeOptions): DateTime {
const as = toReferenceSeconds(a);
const bs = toReferenceSeconds(b);
return fromReferenceSeconds(as > bs ? as : bs);
}

/** Compares two {@link DateTime}s and returns the later of the two.
*
* Alias of {@link latest}, useful for disambiguation from similar functions
* that operate on other date/time types. */
* that operate on other date/time types.
*
* @throws {Error} if both specified `DateTime`s contain numeric fields that
* are non-finite. */
export const latestDateTime = latest;

/** Compares two {@link DateTime}s and returns the later of the two.
*
* Curried variant of {@link latest}. */
export function latestFn<T extends DateTimeOptions, U extends DateTimeOptions>(
b: U
): (a: T) => T | U {
* Curried variant of {@link latest}.
*
* @throws {Error} if both specified `DateTime`s contain numeric fields that
* are non-finite. */
export function latestFn(b: DateTimeOptions): (a: DateTimeOptions) => DateTime {
return a => latest(a, b);
}

/** Compares two {@link DateTime}s and returns the later of the two.
*
* Curried variant of {@link latestDateTime}. */
* Curried variant of {@link latestDateTime}.
*
* @throws {Error} if both specified `DateTime`s contain numeric fields that
* are non-finite. */
export const latestDateTimeFn = latestFn;

/** Returns the current date and time, according to UTC. */
Expand Down

0 comments on commit 0b4063a

Please sign in to comment.