Skip to content

Commit

Permalink
Attempt to fix cheering token parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Mar 9, 2018
1 parent 0cc0561 commit e2ce9b3
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {

export default class CheeringWithCheermotesHandler
extends MultiEventSubscriptionManager<IIncomingCheeringEvent | IIncomingCheermotesEvent> {
public cheerTokenPrefixAmountRx: RegExp;
public currentCheermotes: IIncomingCheermotesEvent | null;
private incomingCheeringWithCheermotesEvent: IEventEmitter<IIncomingCheeringWithCheermotesEvent>;

Expand All @@ -62,6 +63,8 @@ export default class CheeringWithCheermotesHandler
// NOTE: expecting to collect data from a single channel event source, but not verifying that the
// channel doesn't change over time and/or per command.
this.currentCheermotes = null;

this.cheerTokenPrefixAmountRx = /(\w+?)(\d+)/;
}

protected async dataHandler(data: IIncomingCheeringEvent | IIncomingCheermotesEvent): Promise<void> {
Expand Down Expand Up @@ -133,17 +136,17 @@ export default class CheeringWithCheermotesHandler

const messageTokens = incomingCheeringEvent.message.split(/\s+/);
const possibleCheerTokens = messageTokens
.filter((messageToken) => /\w+\d+/.test(messageToken))
.filter((messageToken) => this.cheerTokenPrefixAmountRx.test(messageToken))
.map((cheerToken) => {
const matches = /(\w+)(\d+)/.exec(cheerToken);
const matches = this.cheerTokenPrefixAmountRx.exec(cheerToken);

if (!matches) {
throw new Error("No matches found.");
}

const result = {
amount: parseInt(matches[1][2], 10),
prefix: matches[1][1],
amount: parseInt(matches[2], 10),
prefix: matches[1],
};

return result;
Expand Down Expand Up @@ -179,7 +182,7 @@ export default class CheeringWithCheermotesHandler
}

const actionsWithPrefix = this.currentCheermotes.cheermotes.actions
.filter((action) => action.prefix === cheerToken.prefix);
.filter((action) => action.prefix.toLowerCase() === cheerToken.prefix.toLowerCase());

const actionWithPrefix = actionsWithPrefix[0];

Expand Down

0 comments on commit e2ce9b3

Please sign in to comment.