( cache: IntlCache = createIntlCache() )
| 112 | * @param cache explicit cache to prevent leaking memory |
| 113 | */ |
| 114 | export function createFormatters( |
| 115 | cache: IntlCache = createIntlCache() |
| 116 | ): Formatters { |
| 117 | const RelativeTimeFormat = (Intl as any).RelativeTimeFormat |
| 118 | const ListFormat = (Intl as any).ListFormat |
| 119 | const DisplayNames = (Intl as any).DisplayNames |
| 120 | const getDateTimeFormat = memoize( |
| 121 | (...args) => new Intl.DateTimeFormat(...args), |
| 122 | { |
| 123 | cache: createFastMemoizeCache(cache.dateTime), |
| 124 | strategy: strategies.variadic, |
| 125 | } |
| 126 | ) |
| 127 | const getNumberFormat = memoize((...args) => new Intl.NumberFormat(...args), { |
| 128 | cache: createFastMemoizeCache(cache.number), |
| 129 | strategy: strategies.variadic, |
| 130 | }) |
| 131 | const getPluralRules = memoize((...args) => new Intl.PluralRules(...args), { |
| 132 | cache: createFastMemoizeCache(cache.pluralRules), |
| 133 | strategy: strategies.variadic, |
| 134 | }) |
| 135 | return { |
| 136 | getDateTimeFormat, |
| 137 | getNumberFormat, |
| 138 | getMessageFormat: memoize( |
| 139 | (message, locales, overrideFormats, opts) => |
| 140 | new IntlMessageFormat(message, locales, overrideFormats, { |
| 141 | formatters: { |
| 142 | getNumberFormat, |
| 143 | getDateTimeFormat, |
| 144 | getPluralRules, |
| 145 | }, |
| 146 | ...opts, |
| 147 | }), |
| 148 | { |
| 149 | cache: createFastMemoizeCache(cache.message), |
| 150 | strategy: strategies.variadic, |
| 151 | } |
| 152 | ), |
| 153 | getRelativeTimeFormat: memoize( |
| 154 | (...args) => new RelativeTimeFormat(...args), |
| 155 | { |
| 156 | cache: createFastMemoizeCache(cache.relativeTime), |
| 157 | strategy: strategies.variadic, |
| 158 | } |
| 159 | ), |
| 160 | getPluralRules, |
| 161 | getListFormat: memoize((...args) => new ListFormat(...args), { |
| 162 | cache: createFastMemoizeCache(cache.list), |
| 163 | strategy: strategies.variadic, |
| 164 | }), |
| 165 | getDisplayNames: memoize((...args) => new DisplayNames(...args), { |
| 166 | cache: createFastMemoizeCache(cache.displayNames), |
| 167 | strategy: strategies.variadic, |
| 168 | }), |
| 169 | } |
| 170 | } |
| 171 |
no test coverage detected