(
config: {
locale: string
formats: CustomFormats
onError: OnErrorFn
},
getRelativeTimeFormat: Formatters['getRelativeTimeFormat'],
value: Parameters<IntlFormatters['formatRelativeTime']>[0],
unit?: Parameters<IntlFormatters['formatRelativeTime']>[1],
options: Parameters<IntlFormatters['formatRelativeTime']>[2] = {}
)
| 40 | } |
| 41 | |
| 42 | export function formatRelativeTime( |
| 43 | config: { |
| 44 | locale: string |
| 45 | formats: CustomFormats |
| 46 | onError: OnErrorFn |
| 47 | }, |
| 48 | getRelativeTimeFormat: Formatters['getRelativeTimeFormat'], |
| 49 | value: Parameters<IntlFormatters['formatRelativeTime']>[0], |
| 50 | unit?: Parameters<IntlFormatters['formatRelativeTime']>[1], |
| 51 | options: Parameters<IntlFormatters['formatRelativeTime']>[2] = {} |
| 52 | ): string { |
| 53 | if (!unit) { |
| 54 | unit = 'second' |
| 55 | } |
| 56 | const RelativeTimeFormat = Intl.RelativeTimeFormat |
| 57 | if (!RelativeTimeFormat) { |
| 58 | config.onError( |
| 59 | new FormatError( |
| 60 | `Intl.RelativeTimeFormat is not available in this environment. |
| 61 | Try polyfilling it using "@formatjs/intl-relativetimeformat" |
| 62 | `, |
| 63 | ErrorCode.MISSING_INTL_API |
| 64 | ) |
| 65 | ) |
| 66 | } |
| 67 | try { |
| 68 | return getFormatter(config, getRelativeTimeFormat, options).format( |
| 69 | value, |
| 70 | unit |
| 71 | ) |
| 72 | } catch (e) { |
| 73 | config.onError( |
| 74 | new IntlFormatError('Error formatting relative time.', config.locale, e) |
| 75 | ) |
| 76 | } |
| 77 | |
| 78 | return String(value) |
| 79 | } |
no test coverage detected