All files / app/i18n lists.ts

100% Statements 10/10
66.66% Branches 2/3
100% Functions 2/2
100% Lines 9/9

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 2011x     55x 55x 55x       7x       7x 14x 7x   55x    
const cache = new Map<string, string>();
 
/// Gets the list separator for the given locale.
export function listSeparatorForLocale(locale: string) {
  let separator = cache.get(locale);
  if (!separator) {
    // Intl.ListFormat doesn't have a way to format a list without using "and"/"or".
    // But we can extract the mid-list separator it uses, which is robust for Western, CJK and Arabic.
    // If the local is unknown, fallback to English, which uses commas.
    const formatter = new Intl.ListFormat([locale, "en"], {
      type: "conjunction",
      style: "long",
    });
    const formatParts = formatter.formatToParts(["a", "b", "c"]);
    separator = formatParts.find((p) => p.type === "literal")?.value || ", ";
    cache.set(locale, separator);
  }
  return separator;
}