Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { hasRightToLeftLetter, markdownProseDirection, textDirection } from "./textDirection";
4
5describe("textDirection", () => {
6 it.each([
7 ["Arabic", "اكتب دالة تجمع الأرقام", "rtl"],
8 ["Hebrew", "תקן את הבאג", "rtl"],
9 ["English", "fix the failing test", "ltr"],
10 [
11 "Arabic that opens with an English term",
12 "StrictMode: يعيد الـ render مرتين في التطوير",
13 "rtl",
14 ],
15 ["English quoting one Arabic word", "translate the label مرحبا for the login screen", "ltr"],
16 [
17 "tied sentences use the whole-text word majority",
18 "Done. تم إصلاح المشكلة في الملف الرئيسي",
19 "rtl",
20 ],
21 ["a tie, by its first word", "React مكتبة", "ltr"],
22 ["a tie that opens in Arabic", "مكتبة React", "rtl"],
23 ["Arabic around numbers and punctuation", "عدّل السطر 42، ثم شغّل الاختبارات (3)", "rtl"],
24 [
25 "mostly Arabic sentences beside one long English one",
26 "هذا مثال على paragraph يحتوي على نص عربي و English words معًا. Arabic text should flow right-to-left while English words like JavaScript, React, and API should remain left-to-right within the same line. الأرقام 1234567890 ورموز مثل @username و #hashtag و 100$ تظهر في الاتجاه الصحيح. هذه جملة لاختبار RTL rendering في المتصفح أو المحرر.",
27 "rtl",
28 ],
29 [
30 "mostly English sentences beside one Arabic one",
31 "Run the migration first. ثم شغّل الاختبارات كلها. Then deploy to staging.",
32 "ltr",
33 ],
34 [
35 "Arabic with inline abbreviations",
36 "هذه جملة عربية تتحدث عن e.g. React i.e. TypeScript JavaScript",
37 "rtl",
38 ],
39 [
40 "an abbreviation at a sentence boundary",
41 "This long English sentence describes the technology sector in the U.S. هذه جملة عربية. وهذه جملة عربية أخرى.",
42 "rtl",
43 ],
44 ["Arabic after an unspaced em dash", "English—مرحبا بالعالم", "rtl"],
45 ["a sentence tie after an unspaced em dash", "Done. Hi—זה באמת עובד", "rtl"],
46 ["Hebrew words split by a zero-width space", "A שלום\u200Bעולם", "rtl"],
47 [
48 "sentences split by zero-width spaces",
49 "one\u200Btwo\u200Bthree\u200Bfour\u200Bfive.\u200Bשלום עולם.\u200Bשלום עולם",
50 "rtl",
51 ],
52 ])("reads %s", (_, text, direction) => {
53 expect(textDirection(text)).toBe(direction);
54 });
55
56 it("leaves text without letters to its surroundings", () => {
57 expect(textDirection("42 — 7 ✓")).toBeUndefined();
58 });
59});
60
61describe("hasRightToLeftLetter", () => {
62 it("does not count a byte order mark as a right-to-left letter", () => {
63 expect(hasRightToLeftLetter("\uFEFFHello")).toBe(false);
64 });
65});
66
67describe("markdownProseDirection", () => {
68 it.each([
69 [
70 "Hebrew around a code block",
71 "תתקן את זה:\n```ts\nexport function useCheckout(props: CheckoutProps) {\n return props.items.length;\n}\n```\nותריץ שוב",
72 "rtl",
73 ],
74 ["Hebrew around inline code", "לערוך את `apps/web/src/chat.tsx` או להשאיר `useMemo`?", "rtl"],
75 ["Hebrew around double-backtick code", "תבדוק ``const a = `b c d e` + f`` עכשיו", "rtl"],
76 [
77 "Hebrew after a four-backtick fence",
78 "````md\none two three four five six\n````\nשלום עולם, זה עובד",
79 "rtl",
80 ],
81 [
82 "Hebrew after a tilde fence",
83 "~~~\nnpm run build --filter web --force\n~~~\nזה נכשל אצלי",
84 "rtl",
85 ],
86 [
87 "Hebrew before an unclosed fence",
88 "שלום, הנה השגיאה:\n```\nError: cannot find module react dom client",
89 "rtl",
90 ],
91 ["Hebrew around a URL", "ראה https://github.com/pingdotgg/t3code/pull/24050 לפני", "rtl"],
92 ["Hebrew after an uppercase URL scheme", "HTTP://example.org שלום", "rtl"],
93 ["Hebrew immediately after an angle autolink", "<https://example.org>שלום", "rtl"],
94 ["English around inline code", "Should I edit `useMemo` or keep it?", "ltr"],
95 ["English quoting one Hebrew word", "Please check the שלום label before the release", "ltr"],
96 ])("reads %s", (_, markdown, direction) => {
97 expect(markdownProseDirection(markdown)).toBe(direction);
98 });
99
100 it("leaves markdown without prose letters to its surroundings", () => {
101 expect(markdownProseDirection("```\nconst a = 1;\n```")).toBeUndefined();
102 });
103});