Skip to content

Commit dca17a6

Browse files
committed
feat(earliest): normalize DateTime before returning result
BREAKING CHANGE: The `earliest` functions now throw `Error` if both specified DateTimes contain numeric fields that are non-finite. BREAKING CHANGE: The type parameters of the `earliest` functions have been removed. These functions now always either return a valid DateTime or throw.
1 parent 1083d42 commit dca17a6

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -544,23 +544,32 @@ export function afterOrEqualFn(b: DateTimeOptions): (a: DateTimeOptions) => bool
544544
* Curried variant of {@link dateTimeAfterOrEqual}. */
545545
export const dateTimeAfterOrEqualFn = afterOrEqualFn;
546546

547-
/** Compares two {@link DateTime}s and returns the earlier of the two. */
548-
export function earliest<T extends DateTimeOptions, U extends DateTimeOptions>(a: T, b: U): T | U {
549-
return after(a, b) ? b : a;
547+
/** Compares two {@link DateTime}s and returns the earlier of the two.
548+
*
549+
* @throws {Error} if both specified `DateTime`s contain numeric fields that
550+
* are non-finite. */
551+
export function earliest(a: DateTimeOptions, b: DateTimeOptions): DateTime {
552+
const as = toReferenceSeconds(a);
553+
const bs = toReferenceSeconds(b);
554+
return fromReferenceSeconds(as < bs ? as : bs);
550555
}
551556

552557
/** Compares two {@link DateTime}s and returns the earlier of the two.
553558
*
554559
* Alias of {@link earliest}, useful for disambiguation from similar functions
555-
* that operate on other date/time types. */
560+
* that operate on other date/time types.
561+
*
562+
* @throws {Error} if both specified `DateTime`s contain numeric fields that
563+
* are non-finite. */
556564
export const earliestDateTime = earliest;
557565

558566
/** Compares two {@link DateTime}s and returns the earlier of the two.
559567
*
560-
* Curried variant of {@link earliest}. */
561-
export function earliestFn<T extends DateTimeOptions, U extends DateTimeOptions>(
562-
b: U
563-
): (a: T) => T | U {
568+
* Curried variant of {@link earliest}.
569+
*
570+
* @throws {Error} if both specified `DateTime`s contain numeric fields that
571+
* are non-finite. */
572+
export function earliestFn(b: DateTimeOptions): (a: DateTimeOptions) => DateTime {
564573
return a => earliest(a, b);
565574
}
566575

0 commit comments

Comments
 (0)