All files / app/i18n casing.ts

71.42% Statements 10/14
50% Branches 4/8
100% Functions 1/1
83.33% Lines 10/12

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  47x         369x 369x           369x 369x 20x 20x   369x 369x 369x    
// Creating Intl.Segmenter every time is slow, so cache one per locale.
const segmenterCache = new Map<string, Intl.Segmenter>();
 
/**
 * Uppercases the first letter of a string (if capitalizable), leaving the rest untouched.
 */
export function capitalizeFirstLetter(value: string, locale: string): string {
  Iif (typeof Intl.Segmenter !== "function") {
    // Naive fallback for environments without Intl.Segmenter, like older Firefox browsers.
    if (!value) return value;
    return value[0].toLocaleUpperCase(locale) + value.slice(1);
  }
 
  let segmenter = segmenterCache.get(locale);
  if (!segmenter) {
    segmenter = new Intl.Segmenter(locale, { granularity: "grapheme" });
    segmenterCache.set(locale, segmenter);
  }
  const first = segmenter.segment(value)[Symbol.iterator]().next().value?.segment;
  Iif (!first) return value;
  return first.toLocaleUpperCase(locale) + value.slice(first.length);
}