UNPKG

maz-ui

Version:

A standalone components library for Vue.Js 3 & Nuxt.Js 3

112 lines (111 loc) 9.53 kB
import { r } from "../chunks/truthyFilter.DZvXmxeK.js"; import { toValue, computed } from "vue"; const bcp47Codes = ["af-ZA", "sq-AL", "ar-DZ", "ar-BH", "ar-EG", "ar-IQ", "ar-JO", "ar-KW", "ar-LB", "ar-LY", "ar-MA", "ar-OM", "ar-QA", "ar-SA", "ar-SY", "ar-TN", "ar-AE", "ar-YE", "hy-AM", "Cy-az-AZ", "Lt-az-AZ", "eu-ES", "be-BY", "bg-BG", "ca-ES", "zh-CN", "zh-HK", "zh-MO", "zh-SG", "zh-TW", "zh-CHS", "zh-CHT", "hr-HR", "cs-CZ", "da-DK", "div-MV", "nl-BE", "nl-NL", "en-AU", "en-BZ", "en-CA", "en-CB", "en-IE", "en-JM", "en-NZ", "en-PH", "en-ZA", "en-TT", "en-GB", "en-US", "en-ZW", "et-EE", "fo-FO", "fa-IR", "fi-FI", "fr-BE", "fr-CA", "fr-FR", "fr-LU", "fr-MC", "fr-CH", "gl-ES", "ka-GE", "de-AT", "de-DE", "de-LI", "de-LU", "de-CH", "el-GR", "gu-IN", "he-IL", "hi-IN", "hu-HU", "is-IS", "id-ID", "it-IT", "it-CH", "ja-JP", "kn-IN", "kk-KZ", "kok-IN", "ko-KR", "ky-KZ", "lv-LV", "lt-LT", "mk-MK", "ms-BN", "ms-MY", "mr-IN", "mn-MN", "nb-NO", "nn-NO", "pl-PL", "pt-BR", "pt-PT", "pa-IN", "ro-RO", "ru-RU", "sa-IN", "Cy-sr-SP", "Lt-sr-SP", "sk-SK", "sl-SI", "es-AR", "es-BO", "es-CL", "es-CO", "es-CR", "es-DO", "es-EC", "es-SV", "es-GT", "es-HN", "es-MX", "es-NI", "es-PA", "es-PY", "es-PE", "es-PR", "es-ES", "es-UY", "es-VE", "sw-KE", "sv-FI", "sv-SE", "syr-SY", "ta-IN", "tt-RU", "te-IN", "th-TH", "tr-TR", "uk-UA", "ur-PK", "Cy-uz-UZ", "Lt-uz-UZ", "vi-VN"], countryCodes = ["AA", "AC", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AR", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GT", "GU", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PR", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ", "TA", "TC", "TD", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "XK", "YE", "YT", "ZA", "ZM", "ZW"], iso6391Codes = ["aa", "ab", "ae", "af", "ak", "am", "an", "ar", "as", "av", "ay", "az", "ba", "be", "bg", "bh", "bi", "bm", "bn", "bo", "br", "bs", "ca", "ce", "ch", "co", "cr", "cs", "cu", "cv", "cy", "da", "de", "dv", "dz", "ee", "el", "en", "eo", "es", "et", "eu", "fa", "ff", "fi", "fj", "fo", "fr", "fy", "ga", "gd", "gl", "gn", "gu", "gv", "ha", "he", "hi", "ho", "hr", "ht", "hu", "hy", "hz", "ia", "id", "ie", "ig", "ii", "ik", "io", "is", "it", "iu", "ja", "jv", "ka", "kg", "ki", "kj", "kk", "kl", "km", "kn", "ko", "kr", "ks", "ku", "kv", "kw", "ky", "la", "lb", "lg", "li", "ln", "lo", "lt", "lu", "lv", "mg", "mh", "mi", "mk", "ml", "mn", "mr", "ms", "mt", "my", "na", "nb", "nd", "ne", "ng", "nl", "nn", "no", "nr", "nv", "ny", "oc", "oj", "om", "or", "os", "pa", "pi", "pl", "ps", "pt", "qu", "rm", "rn", "ro", "ru", "rw", "sa", "sc", "sd", "se", "sg", "si", "sk", "sl", "sm", "sn", "so", "sq", "sr", "ss", "st", "su", "sv", "sw", "ta", "te", "tg", "th", "ti", "tk", "tl", "tn", "to", "tr", "ts", "tt", "tw", "ty", "ug", "uk", "ur", "uz", "ve", "vi", "vo", "wa", "wo", "xh", "yi", "yo", "za", "zh", "zu"], displayNamesCodes = [...iso6391Codes, ...bcp47Codes, ...countryCodes], DEFAULT_DISPLAY_NAMES_OPTIONS = { type: "region", languageDisplay: "standard", fallback: "code", style: "long", localeMatcher: "lookup" }, displayNamesCache = /* @__PURE__ */ new Map(); let cacheCleanupTimer = null; function scheduleCacheCleanup() { typeof globalThis.window > "u" || (cacheCleanupTimer && clearTimeout(cacheCleanupTimer), cacheCleanupTimer = setTimeout(() => { displayNamesCache.size > 50 && displayNamesCache.clear(), cacheCleanupTimer = null; }, 300 * 1e3)); } function isSameLanguageThanCode(language, code) { return !language || language?.toLocaleLowerCase() === code.toLocaleLowerCase(); } function normalizeCode(code, type) { return type === "region" ? code.toUpperCase() : type === "language" ? code.toLowerCase() : code; } function getDisplayNamesInstance(locale, options = DEFAULT_DISPLAY_NAMES_OPTIONS) { const cacheKey = `${locale}-${JSON.stringify(options)}`; if (!displayNamesCache.has(cacheKey)) try { const instance = new Intl.DisplayNames([locale], { ...options, type: options.type ?? DEFAULT_DISPLAY_NAMES_OPTIONS.type }); displayNamesCache.set(cacheKey, instance), scheduleCacheCleanup(); } catch (error) { return console.warn(`[maz-ui] (useDisplayNames) Failed to create DisplayNames instance for locale ${locale}:`, error), null; } return displayNamesCache.get(cacheKey) || null; } function getDisplayNamesCodes(codesType) { return codesType === "iso" ? iso6391Codes : codesType === "bcp" ? bcp47Codes : codesType === "country" ? countryCodes : displayNamesCodes; } function getName(code, locale, options) { const opts = { ...DEFAULT_DISPLAY_NAMES_OPTIONS, ...options, type: options?.type ?? DEFAULT_DISPLAY_NAMES_OPTIONS.type }, displayNamesInstance = getDisplayNamesInstance(locale, opts), normalizedCode = normalizeCode(code, opts.type); return displayNamesInstance?.of(normalizedCode); } function getDisplayName(code, options) { return computed(() => { const resolvedOptions = toValue(options), resolvedLocale = toValue(resolvedOptions?.locale), resolvedType = toValue(resolvedOptions?.type) ?? DEFAULT_DISPLAY_NAMES_OPTIONS.type, resolvedCode = toValue(code), resolvedLanguageDisplay = toValue(resolvedOptions?.languageDisplay), resolvedFallback = toValue(resolvedOptions?.fallback), resolvedStyle = toValue(resolvedOptions?.style), resolvedLocaleMatcher = toValue(resolvedOptions?.localeMatcher); if (!resolvedLocale) return resolvedCode; try { return getName(resolvedCode, resolvedLocale, { type: resolvedType, languageDisplay: resolvedLanguageDisplay, fallback: resolvedFallback, style: resolvedStyle, localeMatcher: resolvedLocaleMatcher }); } catch { return resolvedCode; } }); } function getAllDisplayNames(options) { return computed(() => { const resolvedOptions = toValue(options), resolvedLocale = toValue(resolvedOptions?.locale), resolvedCodesType = toValue(resolvedOptions?.codesType) ?? "all", resolvedOnlyCodes = toValue(resolvedOptions.onlyCodes), resolvedExcludedCodes = toValue(resolvedOptions.excludedCodes), resolvedPreferredCodes = toValue(resolvedOptions.preferredCodes), resolvedRemoveDuplicates = toValue(resolvedOptions.removeDuplicates) ?? "name", resolvedRemoveUnmatched = toValue(resolvedOptions.removeUnmatched) ?? !0, resolvedType = toValue(resolvedOptions.type) ?? DEFAULT_DISPLAY_NAMES_OPTIONS.type, resolvedLanguageDisplay = toValue(resolvedOptions.languageDisplay), resolvedFallback = toValue(resolvedOptions.fallback), resolvedStyle = toValue(resolvedOptions.style), resolvedLocaleMatcher = toValue(resolvedOptions.localeMatcher); if (!resolvedLocale) { console.warn("[maz-ui] (useDisplayNames) locale is required"); return; } const filteredCodes = (resolvedOnlyCodes ?? getDisplayNamesCodes(resolvedCodesType)).filter((code) => !resolvedExcludedCodes?.includes(code)), mappedResults = []; for (const code of filteredCodes) try { const name = getName(code, resolvedLocale, { type: resolvedType, languageDisplay: resolvedLanguageDisplay, fallback: resolvedFallback, style: resolvedStyle, localeMatcher: resolvedLocaleMatcher }); if (resolvedRemoveUnmatched && (!name || isSameLanguageThanCode(name, code))) continue; mappedResults.push({ name: name ?? code, code }); } catch { } const result = mappedResults.filter(r).sort((a, b) => a.name && b.name ? a.name.localeCompare(b.name) : 0).sort((a, b) => { const aIndex = resolvedPreferredCodes?.indexOf(a.code) ?? -1, bIndex = resolvedPreferredCodes?.indexOf(b.code) ?? -1; return aIndex !== -1 && bIndex !== -1 ? aIndex - bIndex : aIndex !== -1 ? -1 : bIndex !== -1 ? 1 : 0; }); return resolvedRemoveDuplicates ? result.filter((item, index, self) => self.findIndex((t) => t[resolvedRemoveDuplicates].toLowerCase() === item[resolvedRemoveDuplicates].toLowerCase()) === index) : result; }); } function useDisplayNames(mainLocale) { return { getDisplayName: (code, options) => getDisplayName(code, { ...toValue(options), locale: toValue(options)?.locale || mainLocale }), getAllDisplayNames: (options) => getAllDisplayNames({ ...toValue(options), locale: toValue(options)?.locale || mainLocale }) }; } export { useDisplayNames };