MCPcopy Create free account
hub / github.com/formatjs/formatjs / formatRelativeTime

Function formatRelativeTime

packages/intl/relativeTime.ts:42–79  ·  view source on GitHub ↗
(
  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] = {}
)

Source from the content-addressed store, hash-verified

40}
41
42export 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.
61Try 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}

Callers 2

Calls 2

getFormatterFunction · 0.70
formatMethod · 0.65

Tested by

no test coverage detected