-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdecodeHtml.ts
More file actions
executable file
·21 lines (20 loc) · 661 Bytes
/
decodeHtml.ts
File metadata and controls
executable file
·21 lines (20 loc) · 661 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { requireNonEmptyString } from "../lang";
/**
* Decodes common HTML entities in a string.
*
* @param {string} value - The string containing HTML entities to decode.
* @returns {string} The string with decoded HTML entities.
* @throws {@link EmptyStringException}
* @see {@link encodeHtml}
* @since 1.0.0
* @version 1.3.0
*/
export function decodeHtml(value: string): string {
return requireNonEmptyString(value)
.replace(/&#(\d+);/g, (match, dec) => {
return String.fromCharCode(parseInt(dec, 10));
})
.replace(/&#x([0-9a-fA-F]+);/g, (match, hex) => {
return String.fromCharCode(parseInt(hex, 16));
});
}