From 9eca94c758edb40f5b31806804c23084c6615e57 Mon Sep 17 00:00:00 2001 From: haltcase Date: Mon, 16 Nov 2026 19:47:17 -0600 Subject: [PATCH] fix: keep surrogate pairs intact when wrapping Closes #57 --- src/utilities.ts | 5 ++++- .../issues/57-wrapping-surrogate-pairs.md | 5 +++++ tests/issues/57-wrapping-surrogate-pairs.test.ts | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/__snapshots__/issues/57-wrapping-surrogate-pairs.md create mode 100644 tests/issues/57-wrapping-surrogate-pairs.test.ts diff --git a/src/utilities.ts b/src/utilities.ts index 90e994f..07b4877 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -31,6 +31,7 @@ type StringWidthMethod = ( ) => number; const pipeRegex = /\|/g; +const astralCharacterRegex = /[\u{10000}-\u{10FFFF}]/u; export const ansiRegex = getAnsiRegex({ onlyFirst: true }); /** @@ -39,7 +40,9 @@ export const ansiRegex = getAnsiRegex({ onlyFirst: true }); * styles, or CJK characters. */ export const hasSpecialCharacters = (string: string): boolean => - ansiRegex.test(string) || string.length !== stringWidth(string); + ansiRegex.test(string) || + astralCharacterRegex.test(string) || + string.length !== stringWidth(string); export const basicStringWrap: StringWrapMethod = (string, width) => wordwrap.lines(string, { diff --git a/tests/__snapshots__/issues/57-wrapping-surrogate-pairs.md b/tests/__snapshots__/issues/57-wrapping-surrogate-pairs.md new file mode 100644 index 0000000..aa918d1 --- /dev/null +++ b/tests/__snapshots__/issues/57-wrapping-surrogate-pairs.md @@ -0,0 +1,5 @@ +| A | +| :---- | +| 😀😀 | + 😀😀 + 😀😀 diff --git a/tests/issues/57-wrapping-surrogate-pairs.test.ts b/tests/issues/57-wrapping-surrogate-pairs.test.ts new file mode 100644 index 0000000..590952d --- /dev/null +++ b/tests/issues/57-wrapping-surrogate-pairs.test.ts @@ -0,0 +1,14 @@ +// https://github.com/haltcase/tablemark/issues/57 + +import { expect, test } from "vitest"; + +import { tablemark } from "../../src/index.js"; +import { snapshotFile } from "../helpers.js"; + +test("wraps emoji without splitting surrogate pairs in auto mode", async () => { + const data = [{ a: "😀😀😀😀😀😀" }]; + + await expect(tablemark(data, { maxWidth: 5 })).toMatchFileSnapshot( + snapshotFile("issues/57-wrapping-surrogate-pairs") + ); +});