MCPcopy Index your code
hub / github.com/formatjs/formatjs / getFormatter

Function getFormatter

packages/intl/dateTime.ts:35–76  ·  view source on GitHub ↗
(
  {
    locale,
    formats,
    onError,
    timeZone,
  }: {
    locale: string
    timeZone?: string
    formats: CustomFormats
    onError: OnErrorFn
  },
  type: 'date' | 'time' | 'dateTimeRange',
  getDateTimeFormat: Formatters['getDateTimeFormat'],
  options: Parameters<IntlFormatters['formatDate']>[1] = {}
)

Source from the content-addressed store, hash-verified

33]
34
35export function getFormatter(
36 {
37 locale,
38 formats,
39 onError,
40 timeZone,
41 }: {
42 locale: string
43 timeZone?: string
44 formats: CustomFormats
45 onError: OnErrorFn
46 },
47 type: 'date' | 'time' | 'dateTimeRange',
48 getDateTimeFormat: Formatters['getDateTimeFormat'],
49 options: Parameters<IntlFormatters['formatDate']>[1] = {}
50): Intl.DateTimeFormat {
51 const {format} = options
52 const defaults = {
53 ...(timeZone && {timeZone}),
54 ...(format && getNamedFormat(formats!, type, format, onError)),
55 }
56
57 let filteredOptions = filterProps(
58 options,
59 DATE_TIME_FORMAT_OPTIONS,
60 defaults
61 ) as Intl.DateTimeFormatOptions
62
63 if (
64 type === 'time' &&
65 !filteredOptions.hour &&
66 !filteredOptions.minute &&
67 !filteredOptions.second &&
68 !filteredOptions.timeStyle &&
69 !filteredOptions.dateStyle
70 ) {
71 // Add default formatting options if hour, minute, or second isn't defined.
72 filteredOptions = {...filteredOptions, hour: 'numeric', minute: 'numeric'}
73 }
74
75 return getDateTimeFormat(locale, filteredOptions)
76}
77
78export function formatDate(
79 config: {

Callers 5

formatDateFunction · 0.70
formatTimeFunction · 0.70
formatDateTimeRangeFunction · 0.70
formatDateToPartsFunction · 0.70
formatTimeToPartsFunction · 0.70

Calls 3

getNamedFormatFunction · 0.85
filterPropsFunction · 0.85
getDateTimeFormatFunction · 0.85

Tested by

no test coverage detected