From: Jukka Kurkela Date: Mon, 28 Dec 2020 17:31:19 +0000 (+0200) Subject: Use cached Intl.NumberFormat everywhere (#8244) X-Git-Tag: v3.0.0-beta.8~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59000abd1d96c4e29c6687967f948b47a7db3948;p=thirdparty%2FChart.js.git Use cached Intl.NumberFormat everywhere (#8244) --- diff --git a/src/controllers/controller.doughnut.js b/src/controllers/controller.doughnut.js index b71bc4c6c..2374fcd0e 100644 --- a/src/controllers/controller.doughnut.js +++ b/src/controllers/controller.doughnut.js @@ -1,4 +1,5 @@ import DatasetController from '../core/core.datasetController'; +import {formatNumber} from '../core/core.intl'; import {isArray, valueOrDefault} from '../helpers/helpers.core'; import {toRadians, PI, TAU, HALF_PI} from '../helpers/helpers.math'; @@ -235,7 +236,7 @@ export default class DoughnutController extends DatasetController { const meta = me._cachedMeta; const chart = me.chart; const labels = chart.data.labels || []; - const value = new Intl.NumberFormat(chart.options.locale).format(meta._parsed[index]); + const value = formatNumber(meta._parsed[index], chart.options.locale); return { label: labels[index] || '', diff --git a/src/core/core.intl.js b/src/core/core.intl.js new file mode 100644 index 000000000..88803cda2 --- /dev/null +++ b/src/core/core.intl.js @@ -0,0 +1,17 @@ + +const intlCache = new Map(); + +export function getNumberFormat(locale, options) { + options = options || {}; + const cacheKey = locale + JSON.stringify(options); + let formatter = intlCache.get(cacheKey); + if (!formatter) { + formatter = new Intl.NumberFormat(locale, options); + intlCache.set(cacheKey, formatter); + } + return formatter; +} + +export function formatNumber(num, locale, options) { + return getNumberFormat(locale, options).format(num); +} diff --git a/src/core/core.ticks.js b/src/core/core.ticks.js index 1b75c13b0..1e6770d64 100644 --- a/src/core/core.ticks.js +++ b/src/core/core.ticks.js @@ -1,7 +1,7 @@ import {isArray} from '../helpers/helpers.core'; import {log10} from '../helpers/helpers.math'; +import {formatNumber} from './core.intl'; -const intlCache = new Map(); /** * Namespace to hold formatters for different types of ticks * @namespace Chart.Ticks.formatters @@ -55,14 +55,7 @@ const formatters = { const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal}; Object.assign(options, this.options.ticks.format); - const cacheKey = locale + JSON.stringify(options); - let formatter = intlCache.get(cacheKey); - if (!formatter) { - formatter = new Intl.NumberFormat(locale, options); - intlCache.set(cacheKey, formatter); - } - - return formatter.format(tickValue); + return formatNumber(tickValue, locale, options); } }; diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index 1f26e0093..0ea2b9d1c 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -1,6 +1,7 @@ import {isNullOrUndef, valueOrDefault} from '../helpers/helpers.core'; import {almostEquals, almostWhole, log10, _decimalPlaces, _setMinAndMaxByKey, sign} from '../helpers/helpers.math'; import Scale from '../core/core.scale'; +import {formatNumber} from '../core/core.intl'; /** * Implementation of the nice number algorithm used in determining where axis labels will go @@ -244,6 +245,6 @@ export default class LinearScaleBase extends Scale { } getLabelForValue(value) { - return new Intl.NumberFormat(this.options.locale).format(value); + return formatNumber(value, this.options.locale); } } diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 955496408..949d67a4e 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -3,6 +3,7 @@ import {_setMinAndMaxByKey, log10} from '../helpers/helpers.math'; import Scale from '../core/core.scale'; import LinearScaleBase from './scale.linearbase'; import Ticks from '../core/core.ticks'; +import {formatNumber} from '../core/core.intl'; function isMajor(tickVal) { const remain = tickVal / (Math.pow(10, Math.floor(log10(tickVal)))); @@ -148,7 +149,7 @@ export default class LogarithmicScale extends Scale { * @return {string} */ getLabelForValue(value) { - return value === undefined ? '0' : new Intl.NumberFormat(this.options.locale).format(value); + return value === undefined ? '0' : formatNumber(value, this.options.locale); } /**