@wulperstudio/cms
Version:
Wulper Studio Library Components CMS
53 lines • 2.42 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import { CONTINENTS, COUNTRIES } from '../constants';
import { matchIsArray } from './array';
export function getCallingCodeOfCountry(isoCode) {
var _COUNTRIES$isoCode;
return (_COUNTRIES$isoCode = COUNTRIES[isoCode]) == null ? void 0 : _COUNTRIES$isoCode[0];
}
export function sortedPreferredCountries(countries, preferredCountries) {
return _toConsumableArray(new Set(preferredCountries.concat(countries)));
}
export function getCountriesOfContinents(continents) {
return continents.flatMap(function (continentCode) {
return CONTINENTS[continentCode];
});
}
export function getOnlyCountries(countries, onlyCountries) {
return countries.filter(function (isoCode) {
return onlyCountries.includes(isoCode);
});
}
export function excludeCountries(countries, excludedCountries) {
if (matchIsArray(excludedCountries, true)) {
return countries.filter(function (isoCode) {
return !excludedCountries.includes(isoCode);
});
}
return countries;
}
export function filterCountries(countries, options) {
var onlyCountries = options.onlyCountries,
excludedCountries = options.excludedCountries,
continents = options.continents,
preferredCountries = options.preferredCountries;
if (matchIsArray(onlyCountries, true)) {
var filteredCountries = getOnlyCountries(countries, onlyCountries);
return matchIsArray(preferredCountries, true) ? sortedPreferredCountries(filteredCountries, preferredCountries) : filteredCountries;
}
var theCountries = matchIsArray(continents, true) ? getCountriesOfContinents(continents) : countries;
var sortedCountries = matchIsArray(preferredCountries, true) ? sortedPreferredCountries(theCountries, preferredCountries) : theCountries;
return matchIsArray(excludedCountries, true) ? excludeCountries(sortedCountries, excludedCountries) : sortedCountries;
}
export function matchContinentsIncludeCountry(continents, isoCode) {
return continents.some(function (continentCode) {
return CONTINENTS[continentCode].includes(isoCode);
});
}
export function sortAlphabeticallyCountryCodes(countryCodes, displayNames) {
return _toConsumableArray(countryCodes).sort(function (countryCodeA, countryCodeB) {
var countryA = displayNames.of(countryCodeA);
var countryB = displayNames.of(countryCodeB);
return countryA.localeCompare(countryB);
});
}