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 , key , converter , ... converterArgs ) { 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 , key , converter , ... converterArgs ) { const stringValue = ircTags [ key ] ; if ( stringValue == null ) { return undefined ; } return converter ( stringValue , ... converterArgs ) ; } export function convertToString ( value ) { return value ; } export function convertToInt ( value ) { const parsedInt = parseInt ( value ) ; if ( isNaN ( parsedInt ) ) { throw new ParseError ( `Failed to parse integer from tag value "${ value }"` ) ; } return parsedInt ; } export function convertToBoolean ( value ) { return Boolean ( convertToInt ( value ) ) ; } export function convertToColor ( value ) { if ( value . length <= 0 ) { return undefined ; } return parseColor ( value ) ; } export function convertToTimestamp ( value ) { return new Date ( convertToInt ( value ) ) ; } export function convertToBadges ( value ) { return parseBadges ( value ) ; } export function convertToEmotes ( value , messageText ) { return parseEmotes ( messageText , value ) ; } export function convertToEmoteSets ( value ) { return parseEmoteSets ( value ) ; } export function convertToFlags ( value , messageText ) { return parseFlags ( messageText , value ) ; } export interface TagValueParser { getString ( key ) : string | undefined ; requireString ( key ) : string ; getInt ( key ) : number | undefined ; requireInt ( key ) : number ; getBoolean ( key ) : boolean | undefined ; requireBoolean ( key ) : boolean ; getColor ( key ) : Color | undefined ; requireColor ( key ) : Color ; getTimestamp ( key ) : Date | undefined ; requireTimestamp ( key ) : Date ; getBadges ( key ) : TwitchBadgesList | undefined ; requireBadges ( key ) : TwitchBadgesList ; getEmotes ( key , messageText ) : TwitchEmoteList | undefined ; requireEmotes ( key , messageText ) : TwitchEmoteList ; getEmoteSets ( key ) : TwitchEmoteSets | undefined ; requireEmoteSets ( key ) : TwitchEmoteSets ; getFlags ( key , messageText ) : TwitchFlagList | undefined ; } export function tagParserFor ( ircTags ) { return { getString : ( key ) => getData ( ircTags , key , convertToString ) , requireString : ( key ) => requireData ( ircTags , key , convertToString ) , getInt : ( key ) => getData ( ircTags , key , convertToInt ) , requireInt : ( key ) => requireData ( ircTags , key , convertToInt ) , getBoolean : ( key ) => getData ( ircTags , key , convertToBoolean ) , requireBoolean : ( key ) => requireData ( ircTags , key , convertToBoolean ) , getColor : ( key ) => getData ( ircTags , key , convertToColor ) , requireColor : ( key ) => requireData ( ircTags , key , convertToColor ) , getTimestamp : ( key ) => getData ( ircTags , key , convertToTimestamp ) , requireTimestamp : ( key ) => requireData ( ircTags , key , convertToTimestamp ) , getBadges : ( key ) => getData ( ircTags , key , convertToBadges ) , requireBadges : ( key ) => requireData ( ircTags , key , convertToBadges ) , getEmotes : ( key , messageText ) => getData ( ircTags , key , convertToEmotes , messageText ) , requireEmotes : ( key , messageText ) => requireData ( ircTags , key , convertToEmotes , messageText ) , getEmoteSets : ( key ) => getData ( ircTags , key , convertToEmoteSets ) , requireEmoteSets : ( key ) => requireData ( ircTags , key , convertToEmoteSets ) , getFlags : ( key , messageText ) => getData ( ircTags , key , convertToFlags , messageText ) , } ; }