Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

String.test.ts

77 lines3.5 KB

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("moves the cut back so Indic conjuncts stay whole", () => {
32 expect(truncate("नमस्ते", 4)).toBe("नम...");
33 expect(truncate("Go क्ष्मा notes", 7)).toBe("Go...");
34 expect(truncate("Go ক্ষমা notes", 5)).toBe("Go...");
35 expect(truncate("Go क्षमा notes", 6)).toBe("Go क्ष...");
36 expect(truncate("Go क्\u200dष notes", 5)).toBe("Go...");
37 expect(truncate("Go क्\u200dष notes", 6)).toBe("Go...");
38 expect(truncate("Go क्\u200cष notes", 6)).toBe("Go क्\u200c...");
39 });
40​
41 it("keeps Unicode 17 conjuncts whole at the title cutoff", () => {
42 const prefix = "a".repeat(47);
43 for (const conjunct of ["ក្ក", "က္က", "ᩈ᩠ᩈ", "ᬓ᭄ᬓ", "ꦏ꧀ꦏ", "𑼒𑽂𑼒"]) {
44 expect(truncate(`${prefix} ${conjunct} additional text`), conjunct).toBe(`${prefix}...`);
45 expect(truncate(`${prefix.slice(1)} ${conjunct} additional text`)).toBe(
46 `${prefix.slice(1)} ${conjunct}...`,
47 );
48 }
49 });
50​
51 it("keeps conjoining Hangul syllables whole at the title cutoff", () => {
52 expect(truncate(`${"a".repeat(48)} ᄒᆞᆫ글 follows`)).toBe(`${"a".repeat(48)}...`);
53 });
54​
55 it("keeps Thai and Lao AM vowels with their consonant", () => {
56 expect(truncate("Go \u0e01\u0e33 notes", 4)).toBe("Go...");
57 expect(truncate("Go \u0e81\u0eb3 notes", 4)).toBe("Go...");
58 expect(truncate("Go \u0e01\u0e33 notes", 5)).toBe("Go \u0e01\u0e33...");
59 });
60​
61 it.each([
62 ["before a joiner", "👩‍💻", 1],
63 ["after a joiner", "👩‍💻", 2],
64 ["inside a multi-part emoji", "👩🏽‍❤️‍👩🏻", 5],
65 ["before a skin tone", "👍🏽", 1],
66 ["inside a tag flag", "🏴\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", 3],
67 ] as const)("moves the cut back %s", (_boundary, emoji, offset) => {
68 expect(truncate(`Title ${emoji} notes`, 6 + offset)).toBe("Title...");
69 });
70​
71 it("moves the cut back so prepend signs stay with their following character", () => {
72 expect(truncate(`${"a".repeat(49)}\u0600\u0661 notes`, 50)).toBe(`${"a".repeat(49)}...`);
73 expect(truncate(`${"a".repeat(48)}\u0600 notes`, 50)).toBe(`${"a".repeat(48)}...`);
74 expect(truncate(`${"a".repeat(48)}\u0600\u0601\u0661 notes`, 50)).toBe(`${"a".repeat(48)}...`);
75 expect(truncate(`${"a".repeat(49)}\u0600\nnotes`, 50)).toBe(`${"a".repeat(49)}\u0600...`);
76 });
77});