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("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 Thai and Lao AM vowels with their consonant", () => {
42 expect(truncate("Go \u0e01\u0e33 notes", 4)).toBe("Go...");
43 expect(truncate("Go \u0e81\u0eb3 notes", 4)).toBe("Go...");
44 expect(truncate("Go \u0e01\u0e33 notes", 5)).toBe("Go \u0e01\u0e33...");
45 });
46
47 it.each([
48 ["before a joiner", "👩💻", 1],
49 ["after a joiner", "👩💻", 2],
50 ["inside a multi-part emoji", "👩🏽❤️👩🏻", 5],
51 ["before a skin tone", "👍🏽", 1],
52 ["inside a tag flag", "🏴\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}", 3],
53 ] as const)("moves the cut back %s", (_boundary, emoji, offset) => {
54 expect(truncate(`Title ${emoji} notes`, 6 + offset)).toBe("Title...");
55 });
56
57 it("moves the cut back so prepend signs stay with their following character", () => {
58 expect(truncate(`${"a".repeat(49)}\u0600\u0661 notes`, 50)).toBe(`${"a".repeat(49)}...`);
59 expect(truncate(`${"a".repeat(48)}\u0600 notes`, 50)).toBe(`${"a".repeat(48)}...`);
60 expect(truncate(`${"a".repeat(48)}\u0600\u0601\u0661 notes`, 50)).toBe(`${"a".repeat(48)}...`);
61 expect(truncate(`${"a".repeat(49)}\u0600\nnotes`, 50)).toBe(`${"a".repeat(49)}\u0600...`);
62 });
63});