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