This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
/
tag-values.ts
140 lines (126 loc) · 4.75 KB
/
tag-values.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
131
132
133
134
135
136
137
138
139
140
import { TwitchBadgesList } from "../badges";
import { Color } from "../color";
import { TwitchEmoteList } from "../emotes";
import { TwitchFlagList } from "../flags";
import { IRCMessageTags } from "../irc/tags";
import { parseBadges } from "./badges";
import { parseColor } from "./color";
import { parseEmoteSets, TwitchEmoteSets } from "./emote-sets";
import { parseEmotes } from "./emotes";
import { parseFlags } from "./flags";
import { MissingTagError } from "./missing-tag-error";
import { ParseError } from "./parse-error";
export function requireData<V, A extends any[]>(
ircTags: IRCMessageTags,
key: string,
converter: (value: string, ...converterArgs: A) => V | undefined,
...converterArgs: A
): V {
const stringValue = ircTags[key];
if (stringValue == null) {
throw new MissingTagError(key, stringValue);
}
const value = converter(stringValue, ...converterArgs);
if (value == null) {
throw new MissingTagError(key, stringValue);
}
return value;
}
export function getData<V, A extends any[]>(
ircTags: IRCMessageTags,
key: string,
converter: (value: string, ...converterArgs: A) => V,
...converterArgs: A
): V | undefined {
const stringValue = ircTags[key];
if (stringValue == null) {
return undefined;
}
return converter(stringValue, ...converterArgs);
}
export function convertToString(value: string): string {
return value;
}
export function convertToInt(value: string): number {
const parsedInt = parseInt(value);
if (isNaN(parsedInt)) {
throw new ParseError(`Failed to parse integer from tag value "${value}"`);
}
return parsedInt;
}
export function convertToBoolean(value: string): boolean {
return Boolean(convertToInt(value));
}
export function convertToColor(value: string): Color | undefined {
if (value.length <= 0) {
return undefined;
}
return parseColor(value);
}
export function convertToTimestamp(value: string): Date {
return new Date(convertToInt(value));
}
export function convertToBadges(value: string): TwitchBadgesList {
return parseBadges(value);
}
export function convertToEmotes(
value: string,
messageText: string
): TwitchEmoteList {
return parseEmotes(messageText, value);
}
export function convertToEmoteSets(value: string): TwitchEmoteSets {
return parseEmoteSets(value);
}
export function convertToFlags(
value: string,
messageText: string
): TwitchFlagList {
return parseFlags(messageText, value);
}
export interface TagValueParser {
getString(key: string): string | undefined;
requireString(key: string): string;
getInt(key: string): number | undefined;
requireInt(key: string): number;
getBoolean(key: string): boolean | undefined;
requireBoolean(key: string): boolean;
getColor(key: string): Color | undefined;
requireColor(key: string): Color;
getTimestamp(key: string): Date | undefined;
requireTimestamp(key: string): Date;
getBadges(key: string): TwitchBadgesList | undefined;
requireBadges(key: string): TwitchBadgesList;
getEmotes(key: string, messageText: string): TwitchEmoteList | undefined;
requireEmotes(key: string, messageText: string): TwitchEmoteList;
getEmoteSets(key: string): TwitchEmoteSets | undefined;
requireEmoteSets(key: string): TwitchEmoteSets;
getFlags(key: string, messageText: string): TwitchFlagList | undefined;
}
export function tagParserFor(ircTags: IRCMessageTags): TagValueParser {
return {
getString: (key: string) => getData(ircTags, key, convertToString),
requireString: (key: string) => requireData(ircTags, key, convertToString),
getInt: (key: string) => getData(ircTags, key, convertToInt),
requireInt: (key: string) => requireData(ircTags, key, convertToInt),
getBoolean: (key: string) => getData(ircTags, key, convertToBoolean),
requireBoolean: (key: string) =>
requireData(ircTags, key, convertToBoolean),
getColor: (key: string) => getData(ircTags, key, convertToColor),
requireColor: (key: string) => requireData(ircTags, key, convertToColor),
getTimestamp: (key: string) => getData(ircTags, key, convertToTimestamp),
requireTimestamp: (key: string) =>
requireData(ircTags, key, convertToTimestamp),
getBadges: (key: string) => getData(ircTags, key, convertToBadges),
requireBadges: (key: string) => requireData(ircTags, key, convertToBadges),
getEmotes: (key: string, messageText: string) =>
getData(ircTags, key, convertToEmotes, messageText),
requireEmotes: (key: string, messageText: string) =>
requireData(ircTags, key, convertToEmotes, messageText),
getEmoteSets: (key: string) => getData(ircTags, key, convertToEmoteSets),
requireEmoteSets: (key: string) =>
requireData(ircTags, key, convertToEmoteSets),
getFlags: (key: string, messageText: string) =>
getData(ircTags, key, convertToFlags, messageText),
};
}