All files / src/domain/i18n/useCases ensureIsSupported.ts

100% Statements 12/12
100% Branches 2/2
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37        134x 134x               804x     134x       5x 30x 5x     3x         2x       134x    
import domain from '../../../domain';
import { DEFAULT_OPTIONS } from '../../../constants';
 
export function setLocaleValidCase (locale: string): string {
  const localeParts = locale.split('-');
  return localeParts.length > 1
    ? `${localeParts[0].toLowerCase()}-${localeParts[1].toUpperCase()}`
    : localeParts[0].toLowerCase();
}
 
export default function ensureIsSupported (locale: string): string {
  let isSupported;
 
  const supportedLanguages = domain.i18n.getSupportedLanguages().map(language => language.toLowerCase());
 
  // Test RFC 3066 language
  isSupported = supportedLanguages.includes(locale.toLowerCase());
 
  // Test RFC 3066 language-country
  if (!isSupported) {
    const isoLocale = locale.substr(0, 2).toLowerCase();
    const indexIsoLocale = supportedLanguages.map(language => language.split('-')[0]).indexOf(isoLocale);
    isSupported = indexIsoLocale > -1;
 
    if (isSupported) {
      locale = supportedLanguages[indexIsoLocale];
    }
  }
 
  if (!isSupported) {
    locale = DEFAULT_OPTIONS.locale;
  }
 
  // Get default locale otherwise
  return setLocaleValidCase(locale);
}