```typescript
/**
 * The text, cut to limit characters with an ellipsis when it is longer. */
export function truncate(text: string, limit: number): string {
  if (typeof limit !== "number" || limit < 3) {
    throw Error("limit MUST be a 3 and above integer");
  }
  if (text.length <= limit) {
    return text;
  }
  const isAstral = (charCode: number) =>
    (charCode >= 0x10000 && charCode <= 0x10FFFF);
  let truncatedText = "";
  let charCount = 0;

  for (let i = 0; i < text.length; i++) {
    const char = text.charCodeAt(i);
    if (isAstral(char) && charCount + 3 > limit) {
      break;
    }
    truncatedText += text[i];
    if (charCount + 3 <= limit) {
      charCount++;
    } else {
      truncatedText += "...";
      break;
    }
  }

  return truncatedText;
}
```