Skip to content

Commit

Permalink
perf: Optimize size of Formatter (#2247)
Browse files Browse the repository at this point in the history
  • Loading branch information
VIKTORVAV99 authored Oct 19, 2024
1 parent 1736965 commit 8d91ccb
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/Formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,37 +77,30 @@ class Formatter {

this.options = options;
this.formats = {
number: createCachedFormatter((lng, opt) => {
const formatter = new Intl.NumberFormat(lng, { ...opt });
return (val) => formatter.format(val);
}),
currency: createCachedFormatter((lng, opt) => {
const formatter = new Intl.NumberFormat(lng, { ...opt, style: 'currency' });
return (val) => formatter.format(val);
}),
datetime: createCachedFormatter((lng, opt) => {
const formatter = new Intl.DateTimeFormat(lng, { ...opt });
return (val) => formatter.format(val);
}),
relativetime: createCachedFormatter((lng, opt) => {
const formatter = new Intl.RelativeTimeFormat(lng, { ...opt });
return (val) => formatter.format(val, opt.range || 'day');
}),
list: createCachedFormatter((lng, opt) => {
const formatter = new Intl.ListFormat(lng, { ...opt });
return (val) => formatter.format(val);
}),
number: createCachedFormatter(
(lng, opt) => (val) => new Intl.NumberFormat(lng, { ...opt }).format(val),
),
currency: createCachedFormatter(
(lng, opt) => (val) =>
new Intl.NumberFormat(lng, { ...opt, style: 'currency' }).format(val),
),
datetime: createCachedFormatter(
(lng, opt) => (val) => new Intl.DateTimeFormat(lng, { ...opt }).format(val),
),
relativetime: createCachedFormatter(
(lng, opt) => (val) =>
new Intl.RelativeTimeFormat(lng, { ...opt }).format(val, opt.range || 'day'),
),
list: createCachedFormatter(
(lng, opt) => (val) => new Intl.ListFormat(lng, { ...opt }).format(val),
),
};
this.init(options);
}

/* eslint no-param-reassign: 0 */
init(services, options = { interpolation: {} }) {
const iOpts = options.interpolation;

this.formatSeparator = iOpts.formatSeparator
? iOpts.formatSeparator
: iOpts.formatSeparator || ',';
this.formatSeparator = options.interpolation.formatSeparator || ',';
}

add(name, fc) {
Expand Down

0 comments on commit 8d91ccb

Please sign in to comment.