Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/scule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});