UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

17 lines 1.47 kB
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 {string} format The date format. * Possible values: 'short', 'medium' (default), 'long','full' * @returns {IntlMessageFormat} */var getFormattedDateFromCache=function getFormattedDateFromCache(langCode,format){var hash="".concat(langCode,"_date_").concat(format);// Check if a cached instance already exists. if(messageCache[hash]){return messageCache[hash];}messageCache[hash]=new IntlMessageFormat("{timestamp, date, ".concat(format,"}"),langCode);return messageCache[hash];};/** * Get a formatted date from a timestamp. * @param {string} langCode A language code. * @param {number} timestamp The current date's timestamp. * @param {string} [format='medium'] The date format. * Possible values: 'short', 'medium', 'long','full' * @returns {string} */var formatDate=function formatDate(langCode,timestamp){var format=arguments.length>2&&arguments[2]!==undefined?arguments[2]:'medium';return getFormattedDateFromCache(langCode,format).format({timestamp:timestamp});};export default curry(formatDate);