UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

17 lines 1.92 kB
function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}import IntlMessageFormat from'intl-messageformat';import curry from'lodash/curry';import appConfig from'@shopgate/pwa-common/helpers/config';import messageCache from"./messageCache";var showPriceGrouping=appConfig.showPriceGrouping;/** * 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 {string} currency The current currency. * @param {boolean} fractions With or without fraction digits. * @returns {IntlMessageFormat} */var getFormattedPriceFromCache=function getFormattedPriceFromCache(langCode,currency,fractions){var hash="".concat(langCode,"_price_").concat(currency,"_").concat(fractions);// Check if a cached instance already exists. if(messageCache[hash]){return messageCache[hash];}messageCache[hash]=new IntlMessageFormat("{price, number, ".concat(currency,"}"),langCode,{number:_defineProperty({},currency,{style:'currency',currency:currency,minimumFractionDigits:fractions?2:0,maximumFractionDigits:fractions?2:0,useGrouping:showPriceGrouping})});return messageCache[hash];};/** * Get a formatted price by currency and language code. * @param {string} langCode A language code. * @param {number} price The price to format. * @param {string} currency The current currency. * @param {boolean} fractions With or without fraction digits. * @returns {string} */var formatPrice=function formatPrice(langCode,price,currency,fractions){return getFormattedPriceFromCache(langCode,currency,fractions).format({price:price});};var getPriceFormatter=curry(formatPrice);export default getPriceFormatter;