1
+ // deno-lint-ignore-file no-irregular-whitespace
1
2
/** Convert a string to titleLc format
2
3
*
3
- * - Converts spaces (` `) to underscores (`_`)
4
+ * -
4
5
* - Converts uppercase to lowercase
5
6
*
6
7
* Primarily used for comparing links for equality
7
8
*
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
+ *
8
39
* @param text - String to convert
9
40
* @returns A {@linkcode string} containing the converted text in titleLc format
10
41
*/
11
42
export const toTitleLc = ( text : string ) : string =>
12
43
text . replaceAll ( " " , "_" ) . toLowerCase ( ) ;
13
44
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
+ * ```
15
56
*
16
57
* @param text - String to convert
17
58
* @returns A {@linkcode string} with underscores converted to spaces
@@ -20,6 +61,13 @@ export const revertTitleLc = (text: string): string =>
20
61
text . replaceAll ( "_" , " " ) ;
21
62
22
63
/** 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
+ * ```
23
71
*
24
72
* @param title - Title to encode
25
73
* @returns A {@linkcode string} containing the URI-safe encoded title
@@ -41,6 +89,60 @@ const noEncodeChars = '@$&+=:;",';
41
89
const noTailChars = ':;",' ;
42
90
43
91
/** 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
+ * ```
44
146
*
45
147
* @param title - Title to convert
46
148
* @returns A {@linkcode string} containing the URI-safe title with minimal percent encoding
0 commit comments