UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

33 lines 988 B
import IntlMessageFormat from 'intl-messageformat'; const ICU_PATTERN = /\{[^}]+,\s*(?:plural|select|selectordinal|number|date|time)\b/; const MAX_CACHE_SIZE = 1000; const cache = new Map(); export const icu = { isICU(message) { return ICU_PATTERN.test(message); }, format(message, values, locale) { const cacheKey = `${locale}::${message}`; let formatter = cache.get(cacheKey); if (!formatter) { formatter = new IntlMessageFormat(message, locale); if (cache.size >= MAX_CACHE_SIZE) { cache.delete(cache.keys().next().value); } cache.set(cacheKey, formatter); } const result = formatter.format(values); if (typeof result === 'string') { return result; } if (Array.isArray(result)) { if (result.every(part => typeof part === 'string')) { return result.join(''); } return result; } return String(result); } }; export default icu; //# sourceMappingURL=icuFormatMessage.js.map