From 3f8160571573dca0b8ba7311122abd2569d301ab Mon Sep 17 00:00:00 2001 From: Sasha Sorokin <10401817+Brawaru@users.noreply.github.com> Date: Sat, 29 Apr 2023 07:59:05 +0200 Subject: [PATCH] feat: update to support formatjs/intl ^1.7.1 BREAKING CHANGE: formatjs/intl version required now is ^1.7.1. --- src/index.ts | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9489563..f042694 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 // @@ -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 { @@ -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 ''