@prezly/theme-kit-core
Version:
Data layer and utility library for developing Prezly themes with JavaScript
39 lines (38 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.matchLocaleSlug = matchLocaleSlug;
var _themeKitIntl = require("@prezly/theme-kit-intl");
var _isNumberCode = require("./isNumberCode.cjs");
/**
* Match the requested locale slug against the enabled locales.
* The enabled locales have to be provided in the order of importance:
* default first, then locales with public stories, then the rest.
*
* The logic is reversed from `getShortestLocaleSlug`, so that it "unwraps" the possible variants.
* @see {getShortestLocaleSlug()}
*
* The order of checks is:
* - exact locale slug match
* - match by language code
* - match by region code
*/
function matchLocaleSlug(slug, locales) {
var _ref, _ref2, _matchByExactLocaleSl;
return (_ref = (_ref2 = (_matchByExactLocaleSl = matchByExactLocaleSlug(slug, locales)) !== null && _matchByExactLocaleSl !== void 0 ? _matchByExactLocaleSl : matchByLangSlug(slug, locales)) !== null && _ref2 !== void 0 ? _ref2 : matchByRegionSlug(slug, locales)) !== null && _ref !== void 0 ? _ref : undefined;
}
function matchByExactLocaleSlug(slug, locales) {
if (!_themeKitIntl.Locale.isValid(slug)) return undefined;
var locale = _themeKitIntl.Locale.from(slug);
return locales.find(localeCode => localeCode === locale.code);
}
function matchByLangSlug(slug, locales) {
return locales.find(localeCode => _themeKitIntl.Locale.isLanguageCode(localeCode, slug.toLowerCase()));
}
function matchByRegionSlug(slug, locales) {
if (!slug || (0, _isNumberCode.isNumberCode)(slug)) {
return undefined;
}
return locales.find(localeCode => _themeKitIntl.Locale.isRegionCode(localeCode, slug.toUpperCase()));
}