Skip to content

Commit a44081c

Browse files
committed
refactor(test): Move tests related to processing titles into JSDoc comments
1 parent bc07136 commit a44081c

File tree

2 files changed

+104
-83
lines changed

2 files changed

+104
-83
lines changed

title.test.ts

-81
This file was deleted.

title.ts

+104-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,58 @@
1+
// deno-lint-ignore-file no-irregular-whitespace
12
/** Convert a string to titleLc format
23
*
3-
* - Converts spaces (` `) to underscores (`_`)
4+
* -
45
* - Converts uppercase to lowercase
56
*
67
* Primarily used for comparing links for equality
78
*
9+
* @example Converts spaces (` `) to underscores (`_`)
10+
* ```ts
11+
* import { assertEquals } from "@std/assert/equals";
12+
*
13+
* assertEquals(toTitleLc("sample text"), "sample_text");
14+
* assertEquals(
15+
* toTitleLc("空白入り タイトル"),
16+
* "空白入り_タイトル",
17+
* );
18+
* assertEquals(
19+
* toTitleLc(" 前後にも 空白入り _タイトル "),
20+
* "_前後にも_空白入り__タイトル_",
21+
* );
22+
* ```
23+
*
24+
* @example Converts uppercase to lowercase
25+
* ```ts
26+
* import { assertEquals } from "@std/assert/equals";
27+
*
28+
* assertEquals(toTitleLc("Scrapbox-Gyazo"), "scrapbox-gyazo");
29+
* assertEquals(
30+
* toTitleLc("全角アルファベット「Scrapbox」も変換できる"),
31+
* "全角アルファベット「scrapbox」も変換できる",
32+
* );
33+
* assertEquals(
34+
* toTitleLc("Scrapbox is one of the products powered by Nota inc."),
35+
* "scrapbox_is_one_of_the_products_powered_by_nota_inc.",
36+
* );
37+
* ```
38+
*
839
* @param text - String to convert
940
* @returns A {@linkcode string} containing the converted text in titleLc format
1041
*/
1142
export const toTitleLc = (text: string): string =>
1243
text.replaceAll(" ", "_").toLowerCase();
1344

14-
/** Convert underscores (`_`) to single-byte spaces
45+
/** Convert underscores (`_`) to single-byte spaces (` `)
46+
*
47+
* ```ts
48+
* import { assertEquals } from "@std/assert/equals";
49+
*
50+
* assertEquals(revertTitleLc("sample_text"), "sample text");
51+
* assertEquals(
52+
* revertTitleLc("Title_with underscore"),
53+
* "Title with underscore",
54+
* );
55+
* ```
1556
*
1657
* @param text - String to convert
1758
* @returns A {@linkcode string} with underscores converted to spaces
@@ -20,6 +61,13 @@ export const revertTitleLc = (text: string): string =>
2061
text.replaceAll("_", " ");
2162

2263
/** Encode a title into a URI-safe format
64+
*
65+
* ```ts
66+
* import { assertEquals } from "@std/assert/equals";
67+
*
68+
* assertEquals(encodeTitleURI("sample text"), "sample_text");
69+
* assertEquals(encodeTitleURI(":title:"), ":title%3A");
70+
* ```
2371
*
2472
* @param title - Title to encode
2573
* @returns A {@linkcode string} containing the URI-safe encoded title
@@ -41,6 +89,60 @@ const noEncodeChars = '@$&+=:;",';
4189
const noTailChars = ':;",';
4290

4391
/** Convert a title to a URI-safe format while minimizing percent encoding
92+
*
93+
* @example Only words
94+
* ```ts
95+
* import { assertEquals } from "@std/assert/equals";
96+
*
97+
* assertEquals(
98+
* toReadableTitleURI("Normal_TitleAAA"),
99+
* "Normal_TitleAAA",
100+
* );
101+
* ```
102+
*
103+
* @example With spaces
104+
* ```ts
105+
* import { assertEquals } from "@std/assert/equals";
106+
*
107+
* assertEquals(
108+
* toReadableTitleURI("Title with Spaces"),
109+
* "Title_with_Spaces",
110+
* );
111+
* ```
112+
*
113+
* @example With special characters
114+
* ```ts
115+
* import { assertEquals } from "@std/assert/equals";
116+
*
117+
* assertEquals(
118+
* toReadableTitleURI("Title with special characters: /?{}^|<>%"),
119+
* "Title_with_special_characters:_%2F%3F%7B%7D%5E%7C%3C%3E%25",
120+
* );
121+
* ```
122+
*
123+
* @example With multibyte characters
124+
* ```ts
125+
* import { assertEquals } from "@std/assert/equals";
126+
*
127+
* assertEquals(
128+
* toReadableTitleURI("日本語_(絵文字✨つき) タイトル"),
129+
* "日本語_(絵文字✨つき) タイトル",
130+
* );
131+
* ```
132+
*
133+
* @example With percent encoding
134+
* ```ts
135+
* import { assertEquals } from "@std/assert/equals";
136+
*
137+
* assertEquals(
138+
* toReadableTitleURI("スラッシュ/は/percent encoding対象の/文字です"),
139+
* "スラッシュ%2Fは%2Fpercent_encoding対象の%2F文字です",
140+
* );
141+
* assertEquals(
142+
* toReadableTitleURI("%2Fなども/と同様percent encodingされる"),
143+
* "%252Fなども%2Fと同様percent_encodingされる",
144+
* );
145+
* ```
44146
*
45147
* @param title - Title to convert
46148
* @returns A {@linkcode string} containing the URI-safe title with minimal percent encoding

0 commit comments

Comments
 (0)