@shopgate/pwa-common
Version:
Common library for the Shopgate Connect PWA.
15 lines • 1.39 kB
JavaScript
import IntlMessageFormat from'intl-messageformat';import curry from'lodash/curry';import messageCache from"./messageCache";/**
* Returns an instance of IntlMessageFormat from cache based on a hash.
* The hash is generated from given language code and translation key.
* If no instance exists yet, a new instance will be created and returned.
* @param {string} langCode A language code.
* @param {number} fractions With or without fraction digits.
* @returns {IntlMessageFormat}
*/var getFormattedNumberFromCache=function getFormattedNumberFromCache(langCode,fractions){var hash="".concat(langCode,"_number_fr_").concat(fractions);// Check if a cached instance already exists.
if(messageCache[hash]){return messageCache[hash];}messageCache[hash]=new IntlMessageFormat('{value, number, decimal}',langCode,{number:{decimal:{style:'decimal',minimumFractionDigits:fractions,maximumFractionDigits:fractions}}});return messageCache[hash];};/**
* Get a formatted number by a language code.
* @param {string} langCode A language code.
* @param {number} value The number to format.
* @param {boolean} fractions Number of digits after dot.
* @returns {string}
*/var formatNumber=function formatNumber(langCode,value,fractions){return getFormattedNumberFromCache(langCode,fractions).format({value:value});};var getNumberFormatter=curry(formatNumber);export default getNumberFormatter;