From e6c8f7ed66d88812cdc0b8309d163fa52f72101d Mon Sep 17 00:00:00 2001 From: Huanyi Xie <74809554+xiehuanyi@users.noreply.github.com> Date: Tue, 6 Oct 2026 22:05:46 +0300 Subject: [PATCH] fix: preserve Unicode code points at acronym boundaries --- src/index.ts | 14 ++++++++------ test/scule.test.ts | 11 +++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index f1983fd..2861887 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,12 +60,14 @@ export function splitByCase< continue; } // Case falling edge - if (previousUpper === true && isUpper === false && buff.length > 1) { - const lastChar = buff.at(-1); - parts.push(buff.slice(0, Math.max(0, buff.length - 1))); - buff = lastChar + char; - previousUpper = isUpper; - continue; + if (previousUpper === true && isUpper === false) { + const lastChar = [...buff].at(-1)!; + if (buff.length > lastChar.length) { + parts.push(buff.slice(0, -lastChar.length)); + buff = lastChar + char; + previousUpper = isUpper; + continue; + } } } diff --git a/test/scule.test.ts b/test/scule.test.ts index bd5ec3b..cd1fe31 100644 --- a/test/scule.test.ts +++ b/test/scule.test.ts @@ -158,3 +158,14 @@ describe("flatCase", () => { expect(flatCase(input)).toMatchObject(expected); }); }); + +describe("splitByCase astral case boundaries", () => { + test.each([ + ["๐€foo", ["๐€foo"]], + ["๐€๐foo", ["๐€", "๐foo"]], + ["A๐€foo", ["A", "๐€foo"]], + ["foo๐€bar", ["foo", "๐€bar"]], + ])("keeps Unicode code points intact in %s", (input, expected) => { + expect(splitByCase(input as string)).toEqual(expected); + }); +});