-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (150 loc) · 3.05 KB
/
index.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"use strict";
/**
* @description Object containing Latin-1 characters and their corresponding UTF-8 characters.
* @type {Record<string, string>}
*/
const replacements = {
// Actual: Expected
"€": "€",
"‚": "‚",
"Æ’": "ƒ",
"„": "„",
"…": "…",
"â€\u00A0": "†",
"‡": "‡",
"ˆ": "ˆ",
"‰": "‰",
"Å\u00A0": "Š",
"‹": "‹",
"Å’": "Œ",
"Ž": "Ž",
"‘": "‘",
"’": "’",
"“": "“",
"â€\u009D": "”",
"•": "•",
"–": "–",
"—": "—",
Ëœ: "˜",
"â„¢": "™",
"Å¡": "š",
"›": "›",
"Å“": "œ",
"ž": "ž",
"Ÿ": "Ÿ",
"Â ": " ",
"¡": "¡",
"¢": "¢",
"£": "£",
"¤": "¤",
"Â¥": "¥",
"¦": "¦",
"§": "§",
"¨": "¨",
"©": "©",
ª: "ª",
"«": "«",
"¬": "¬",
"Â": "",
"®": "®",
"¯": "¯",
"°": "°",
"±": "±",
"²": "²",
"³": "³",
"´": "´",
µ: "µ",
"¶": "¶",
"·": "·",
"¸": "¸",
"¹": "¹",
º: "º",
"»": "»",
"¼": "¼",
"½": "½",
"¾": "¾",
"¿": "¿",
"À": "À",
"Â": "Â",
Ã: "Ã",
"Ä": "Ä",
"Ã…": "Å",
"Æ": "Æ",
"Ç": "Ç",
È: "È",
"É": "É",
Ê: "Ê",
"Ë": "Ë",
ÃŒ: "Ì",
"Ã\u008D": "Í",
ÃŽ: "Î",
"Ã\u008F": "Ï",
"Ã\u0090": "Ð",
"Ñ": "Ñ",
"Ã’": "Ò",
"Ó": "Ó",
"Ô": "Ô",
"Õ": "Õ",
"Ö": "Ö",
"×": "×",
"Ø": "Ø",
"Ù": "Ù",
Ú: "Ú",
"Û": "Û",
Ãœ: "Ü",
"Ã\u009D": "Ý",
Þ: "Þ",
ß: "ß",
"Ã\u00A0": "à",
"á": "á",
"â": "â",
"ã": "ã",
"ä": "ä",
"Ã¥": "å",
"æ": "æ",
"ç": "ç",
"è": "è",
"é": "é",
ê: "ê",
"ë": "ë",
"ì": "ì",
"Ã\u00AD": "í",
"î": "î",
"ï": "ï",
"ð": "ð",
"ñ": "ñ",
"ò": "ò",
"ó": "ó",
"ô": "ô",
õ: "õ",
"ö": "ö",
"÷": "÷",
"ø": "ø",
"ù": "ù",
ú: "ú",
"û": "û",
"ü": "ü",
"ý": "ý",
"þ": "þ",
"ÿ": "ÿ",
};
// Cache immutable regex as they are expensive to create and garbage collect
// eslint-disable-next-line security/detect-non-literal-regexp -- Static regex, no user input
const matchRegex = new RegExp(Object.keys(replacements).join("|"), "gu");
/**
* @author Frazer Smith
* @description Fixes common encoding errors when converting from Latin-1 (and Windows-1252) to UTF-8.
* @see {@link http://www.i18nqa.com/debug/utf8-debug.html | UTF-8 Encoding Debugging Chart}
* @param {string} str - The string to be converted.
* @returns {string} The converted string.
*/
function fixLatin1ToUtf8(str) {
if (typeof str !== "string") {
throw new TypeError("Expected a string");
}
return str.replace(matchRegex, (match) => replacements[match]).normalize();
}
module.exports = fixLatin1ToUtf8; // CommonJS export
module.exports.default = fixLatin1ToUtf8; // ESM default export
module.exports.fixLatin1ToUtf8 = fixLatin1ToUtf8; // TypeScript and named export
module.exports.replacements = replacements;