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

String.test.ts

25 lines909 Bytes

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});