All files / client/src/utils joinWithConjunction.ts

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 4/4

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    521x       141x 139x   2x    
// Joins list items with commas but places the given localized conjunction
// (e.g. "and" / "ja") before the last item: "a, b and c"
export const joinWithConjunction = (
  items: readonly string[],
  conjunction: string,
): string => {
  if (items.length <= 1) {
    return items.join("");
  }
  return `${items.slice(0, -1).join(", ")} ${conjunction} ${items.slice(-1).join("")}`;
};