HTML Entity Parser LeetCode solution in JavaScript

1
/**
2
* @param {string} text
3
* @return {string}
4
*/
5
var entityParser = function (text) {
6
const entityMap = {
7
""": `"`,
8
"'": `'`,
9
"&": `&`,
10
">": `>`,
11
"&lt;": `<`,
12
"&frasl;": `/`,
13
};
14

15
(stack = []), (entity = "");
16

17
for (const char of text) {
18
stack.push(char);
19
if (char == "&") {
20
if (entity.length > 0) entity = "";
21
entity += char;
22
} else if (char == ";" && entity.length > 0) {
23
entity += char;
24

25
if (entity in entityMap) {
26
while (stack.length && stack[stack.length - 1] !== "&") {
27
stack.pop();
28
}
29
stack.pop();
30
stack.push(entityMap[entity]);
31
}
32

33
entity = "";
34
} else if (entity.length > 0) {
35
entity += char;
36
}
37
}
38

39
return stack.join("");
40
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0