Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2โ
3import { truncate } from "./String.ts";
4โ
5describe("truncate", () => {
6 it("cuts by Unicode code point so emoji stay whole", () => {
7 expect(truncate("๐๐๐", 5)).toBe("๐๐๐");
8 expect(truncate("๐๐๐๐", 3)).toBe("๐๐๐...");
9 });
10โ
11 it("trims before measuring", () => {
12 expect(truncate(" hello ", 5)).toBe("hello");
13 });
14โ
15 it("drops whitespace at the cut before the ellipsis", () => {
16 expect(truncate("hello world", 6)).toBe("hello...");
17 });
18โ
19 it("moves the cut back so combining marks stay with their base", () => {
20 expect(truncate("cafe\u0301!", 4)).toBe("caf...");
21 expect(truncate("เคจเคฎเคธเฅเคคเฅ", 3)).toBe("เคจเคฎ...");
22 expect(truncate("เธเนเธฒเธเธเธตเน", 6)).toBe("เธเนเธฒเธ...");
23 expect(truncate("\u26A0\uFE0F\u26A0\uFE0F", 3)).toBe("\u26A0\uFE0F...");
24 });
25โ
26 it("moves the cut back so flag emoji stay whole", () => {
27 expect(truncate("Trip ๐ฏ๐ต๐ฐ๐ท plans", 6)).toBe("Trip...");
28 expect(truncate("Trip ๐ฏ๐ต๐ฐ๐ท plans", 7)).toBe("Trip ๐ฏ๐ต...");
29 });
30โ
31 it.each([
32 ["before a joiner", "๐ฉโ๐ป", 1],
33 ["after a joiner", "๐ฉโ๐ป", 2],
34 ["inside a multi-part emoji", "๐ฉ๐ฝโโค๏ธโ๐ฉ๐ป", 5],
35 ["before a skin tone", "๐๐ฝ", 1],
36 ["inside a tag flag", "๐ด\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", 3],
37 ] as const)("moves the cut back %s", (_boundary, emoji, offset) => {
38 expect(truncate(`Title ${emoji} notes`, 6 + offset)).toBe("Title...");
39 });
40});