-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtitle.ts
51 lines (47 loc) · 1.55 KB
/
title.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/** Convert a string to titleLc format
*
* - Converts spaces (` `) to underscores (`_`)
* - Converts uppercase to lowercase
*
* Primarily used for comparing links for equality
*
* @param text - String to convert
* @returns A {@linkcode string} containing the converted text in titleLc format
*/
export const toTitleLc = (text: string): string =>
text.replaceAll(" ", "_").toLowerCase();
/** Convert underscores (`_`) to single-byte spaces
*
* @param text - String to convert
* @returns A {@linkcode string} with underscores converted to spaces
*/
export const revertTitleLc = (text: string): string =>
text.replaceAll("_", " ");
/** Encode a title into a URI-safe format
*
* @param title - Title to encode
* @returns A {@linkcode string} containing the URI-safe encoded title
*/
export const encodeTitleURI = (title: string): string => {
return [...title].map((char, index) => {
if (char === " ") return "_";
if (
!noEncodeChars.includes(char) ||
(index === title.length - 1 && noTailChars.includes(char))
) {
return encodeURIComponent(char);
}
return char;
}).join("");
};
const noEncodeChars = '@$&+=:;",';
const noTailChars = ':;",';
/** Convert a title to a URI-safe format while minimizing percent encoding
*
* @param title - Title to convert
* @returns A {@linkcode string} containing the URI-safe title with minimal percent encoding
*/
export const toReadableTitleURI = (title: string): string => {
return title.replaceAll(" ", "_")
.replace(/[/?#\{}^|<>%]/g, (char) => encodeURIComponent(char));
};