Skip to content

Commit 0b4063a

Browse files
committed
feat(latest): normalize DateTime before returning result
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.
1 parent dca17a6 commit 0b4063a

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -578,29 +578,41 @@ export function earliestFn(b: DateTimeOptions): (a: DateTimeOptions) => DateTime
578578
* Curried variant of {@link earliestDateTime}. */
579579
export const earliestDateTimeFn = earliestFn;
580580

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

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

592600
/** Compares two {@link DateTime}s and returns the later of the two.
593601
*
594-
* Curried variant of {@link latest}. */
595-
export function latestFn<T extends DateTimeOptions, U extends DateTimeOptions>(
596-
b: U
597-
): (a: T) => T | U {
602+
* Curried variant of {@link latest}.
603+
*
604+
* @throws {Error} if both specified `DateTime`s contain numeric fields that
605+
* are non-finite. */
606+
export function latestFn(b: DateTimeOptions): (a: DateTimeOptions) => DateTime {
598607
return a => latest(a, b);
599608
}
600609

601610
/** Compares two {@link DateTime}s and returns the later of the two.
602611
*
603-
* Curried variant of {@link latestDateTime}. */
612+
* Curried variant of {@link latestDateTime}.
613+
*
614+
* @throws {Error} if both specified `DateTime`s contain numeric fields that
615+
* are non-finite. */
604616
export const latestDateTimeFn = latestFn;
605617

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

0 commit comments

Comments
 (0)