forked from teralove/boss-skill-logger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.js
31 lines (27 loc) · 812 Bytes
/
format.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
const entities = {
amp: '&',
lt: '<',
gt: '>',
quot: '"',
apos: "'",
};
const reversedEscapeChars = {};
for (let k in entities) reversedEscapeChars[entities[k]] = k;
module.exports = {
stripTags: s => s.replace(/<\/?[^<>]*>/gi, ''),
escapeHTML: s => s.replace(/[&<>"']/g, m => `&${reversedEscapeChars[m]};`),
decodeHTMLEntities: s => s
.replace(/&#(\d+);?/g, (_, code) => String.fromCharCode(code))
.replace(/&#[xX]([A-Fa-f0-9]+);?/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))
.replace(/&([^;\W]+;?)/g, (m, e) => {
const target = entities[e.replace(/;$/, '')];
switch (typeof target) {
case 'number':
return String.fromCharCode(target);
case 'string':
return target;
default:
return m;
}
}),
};