Skip to content

Commit

Permalink
feat: update to support formatjs/intl ^1.7.1
Browse files Browse the repository at this point in the history
BREAKING CHANGE: formatjs/intl version required now is ^1.7.1.
  • Loading branch information
brawaru committed Apr 29, 2023
1 parent a9713d0 commit 3f81605
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
type ResolvedIntlConfig,
type FormatRelativeTimeOptions,
type FormatDateOptions,
IntlError,
IntlErrorCode,
type IntlShape,
} from '@formatjs/intl'
import { FormatError, ErrorCode } from 'intl-messageformat'

// Based on the original code from Omorphia, Modrinth
//
Expand Down Expand Up @@ -405,29 +404,20 @@ function formatRelativeTimeRange(
range: DateTimeRange,
options?: FormatOptions,
): string {
const reportError = (err: unknown) =>
onError(
new IntlError(
IntlErrorCode.FORMAT_ERROR,
'Error formatting time difference.',
err,
),
)

let from: number, to: number
try {
;[from, to] = toTimeSpan(range)
} catch (err) {
reportError(err)
return ''
}
const [from, to] = toTimeSpan(range)

try {
const relative = tryAsRelativeTime(formatRelativeTime, from, to, options)

if (relative != null) return relative
} catch (err) {
reportError(err)
onError(
new FormatError(
'Error formatting time difference as a relative time',
ErrorCode.INVALID_VALUE,
err instanceof Error ? err.message : String(err),
),
)
}

try {
Expand All @@ -439,7 +429,13 @@ function formatRelativeTimeRange(
},
)
} catch (err) {
reportError(err)
onError(
new FormatError(
'Error formatting time difference as a date',
ErrorCode.INVALID_VALUE,
err instanceof Error ? err.message : String(err),
),
)
}

return ''
Expand Down

0 comments on commit 3f81605

Please sign in to comment.