```ts
export function foldDigraphs(
  text: string,
  table: Array<[string, string]>,
): string {
  if (table.some(([pattern]) => pattern === "")) {
    throw new Error("empty pattern");
  }

  const result = [];
  let position = 0;

  while (position < text.length) {
    let matchInfo = null;
    for (const [pattern, output] of table) {
      if (text.startsWith(pattern, position)) {
        if (!matchInfo || pattern.length > matchInfo.matchLength) {
          matchInfo = { matchLength: pattern.length, output };
        }
      }
    }

    if (matchInfo) {
      result.push(matchInfo.output);
      position += matchInfo.matchLength;
    } else {
      result.push(text[position]);
      position++;
    }
  }

  return result.join("");
}
```