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

String.test.ts

40 lines1.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.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});