-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
messagePartParser.ts
130 lines (116 loc) · 3.51 KB
/
messagePartParser.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
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
import { utf8Length, utf8Substring } from '@d-fischer/shared-utils';
import { type ParsedMessageCheerPart, type ParsedMessageEmotePart, type ParsedMessagePart } from './ParsedMessagePart';
/**
* Transforms the parts of the given text that are marked as emotes.
*
* @param text The message text.
* @param emoteOffsets The emote offsets. An emote name maps to a list of text ranges.
*/
export function parseEmotePositions(text: string, emoteOffsets: Map<string, string[]>): ParsedMessageEmotePart[] {
return [...emoteOffsets.entries()]
.flatMap(([emote, placements]) =>
placements.map((placement): ParsedMessageEmotePart => {
const [startStr, endStr] = placement.split('-');
const start = +startStr;
const end = +endStr;
const name = utf8Substring(text, start, end + 1);
return {
type: 'emote',
position: start,
length: end - start + 1,
id: emote,
name
};
})
)
.sort((a, b) => a.position - b.position);
}
/**
* Finds the positions of all cheermotes in the given message.
*
* @param text The message text.
* @param names The names of the cheermotes to find.
*/
export function findCheermotePositions(text: string, names: string[]): ParsedMessageCheerPart[] {
const result: ParsedMessageCheerPart[] = [];
const re = new RegExp('(?<=^|\\s)([a-z]+(?:\\d+[a-z]+)*)(\\d+)(?=\\s|$)', 'gi');
let match: RegExpExecArray | null = null;
while ((match = re.exec(text))) {
const name = match[1].toLowerCase();
if (names.includes(name)) {
const amount = Number(match[2]);
result.push({
type: 'cheer',
name,
amount,
position: utf8Length(text.slice(0, match.index)),
length: match[0].length
});
}
}
return result;
}
/**
* Add text parts to the given list of message parts for all the text that's unaccounted for.
*
* @param text The message text.
* @param otherPositions The parsed non-text parts of the message.
*/
export function fillTextPositions(text: string, otherPositions: ParsedMessagePart[]): ParsedMessagePart[] {
const messageLength = utf8Length(text);
if (!otherPositions.length) {
return [
{
type: 'text',
position: 0,
length: messageLength,
text: text
}
];
}
const result: ParsedMessagePart[] = [];
let currentPosition = 0;
for (const token of otherPositions) {
if (token.position > currentPosition) {
result.push({
type: 'text',
position: currentPosition,
length: token.position - currentPosition,
text: utf8Substring(text, currentPosition, token.position)
});
}
result.push(token);
currentPosition = token.position + token.length;
}
if (currentPosition < messageLength) {
result.push({
type: 'text',
position: currentPosition,
length: messageLength - currentPosition,
text: utf8Substring(text, currentPosition)
});
}
return result;
}
/**
* Parse a chat message with emotes and optionally cheermotes.
*
* @param text The message text.
* @param emoteOffsets The emote offsets. An emote name maps to a list of text ranges.
* @param cheermoteNames The names of the cheermotes to find. Will not do cheermote parsing if not given.
*/
export function parseChatMessage(
text: string,
emoteOffsets: Map<string, string[]>,
cheermoteNames?: string[]
): ParsedMessagePart[] {
if (!text) {
return [];
}
const foundParts: ParsedMessagePart[] = parseEmotePositions(text, emoteOffsets);
if (cheermoteNames) {
foundParts.push(...findCheermotePositions(text, cheermoteNames));
foundParts.sort((a, b) => a.position - b.position);
}
return fillTextPositions(text, foundParts);
}