|
| 1 | +import { |
| 2 | + getConstructor, |
| 3 | + isInstant, |
| 4 | + isPlainDate, |
| 5 | + isPlainDateTime, |
| 6 | + isPlainMonthDay, |
| 7 | + isPlainTime, |
| 8 | + isPlainYearMonth, |
| 9 | + isZonedDateTime, |
| 10 | +} from "../type-utils.js"; |
| 11 | +import type { ComparableDateTimeType, Temporal } from "../types.js"; |
| 12 | + |
| 13 | +export function compare( |
| 14 | + a: Temporal.Instant, |
| 15 | + b: Temporal.Instant, |
| 16 | +): Temporal.ComparisonResult; |
| 17 | +export function compare( |
| 18 | + a: Temporal.ZonedDateTime, |
| 19 | + b: Temporal.ZonedDateTime, |
| 20 | +): Temporal.ComparisonResult; |
| 21 | +export function compare( |
| 22 | + a: Temporal.PlainDate, |
| 23 | + b: Temporal.PlainDate, |
| 24 | +): Temporal.ComparisonResult; |
| 25 | +export function compare( |
| 26 | + a: Temporal.PlainTime, |
| 27 | + b: Temporal.PlainTime, |
| 28 | +): Temporal.ComparisonResult; |
| 29 | +export function compare( |
| 30 | + a: Temporal.PlainDateTime, |
| 31 | + b: Temporal.PlainDateTime, |
| 32 | +): Temporal.ComparisonResult; |
| 33 | +export function compare( |
| 34 | + a: Temporal.PlainYearMonth, |
| 35 | + b: Temporal.PlainYearMonth, |
| 36 | +): Temporal.ComparisonResult; |
| 37 | +export function compare( |
| 38 | + a: ComparableDateTimeType, |
| 39 | + b: ComparableDateTimeType, |
| 40 | +): Temporal.ComparisonResult; |
| 41 | +export function compare(a: ComparableDateTimeType, b: ComparableDateTimeType) { |
| 42 | + if (isInstant(a)) { |
| 43 | + if (!isInstant(b)) { |
| 44 | + throw new Error("Unmatched type"); |
| 45 | + } |
| 46 | + return getConstructor(a).compare(a, b); |
| 47 | + } |
| 48 | + if (isZonedDateTime(a)) { |
| 49 | + if (!isZonedDateTime(b)) { |
| 50 | + throw new Error("Unmatched type"); |
| 51 | + } |
| 52 | + return getConstructor(a).compare(a, b); |
| 53 | + } |
| 54 | + if (isPlainDate(a)) { |
| 55 | + if (!isPlainDate(b)) { |
| 56 | + throw new Error("Unmatched type"); |
| 57 | + } |
| 58 | + return getConstructor(a).compare(a, b); |
| 59 | + } |
| 60 | + if (isPlainTime(a)) { |
| 61 | + if (!isPlainTime(b)) { |
| 62 | + throw new Error("Unmatched type"); |
| 63 | + } |
| 64 | + return getConstructor(a).compare(a, b); |
| 65 | + } |
| 66 | + if (isPlainDateTime(a)) { |
| 67 | + if (!isPlainDateTime(b)) { |
| 68 | + throw new Error("Unmatched type"); |
| 69 | + } |
| 70 | + return getConstructor(a).compare(a, b); |
| 71 | + } |
| 72 | + if (isPlainYearMonth(a)) { |
| 73 | + if (!isPlainYearMonth(b)) { |
| 74 | + throw new Error("Unmatched type"); |
| 75 | + } |
| 76 | + return getConstructor(a).compare(a, b); |
| 77 | + } |
| 78 | + if (isPlainMonthDay(a) || isPlainMonthDay(b)) { |
| 79 | + throw new Error("Can't compare PlainMonthDay"); |
| 80 | + } |
| 81 | + throw new Error("Unknown type"); |
| 82 | +} |
0 commit comments