Skip to content

Commit

Permalink
Fix milestone message durationText parser
Browse files Browse the repository at this point in the history
  • Loading branch information
HitomaruKonpaku committed Dec 10, 2022
1 parent e5c8b6b commit e2e969c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/chat/actions/addChatItemAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,10 @@ export function parseLiveChatMembershipItemRenderer(
if (isMilestoneMessage) {
const message = renderer.message ? renderer.message.runs : null;
const durationText = renderer
.headerPrimaryText!.runs.slice(1)
.map((r) => r.text)
.join("");
.headerPrimaryText!.runs.map((r) => r.text)
.join("")
.replace("Member for", "")
.trim();
// duration > membership.since
// e.g. 12 months > 6 months
const duration = durationToSeconds(durationText);
Expand Down
16 changes: 12 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ export function durationToSeconds(durationText: string): number {
const match = /^(a|\d+)\s(year|month|week|day|hour|minute|second)s?$/.exec(
durationText
);
if (!match) throw new Error(`Invalid duration: ${durationText}`);
if (!match) {
throw new Error(`Invalid duration: ${durationText}`);
}

const [_, duration, unit] = match;
const durationInt = parseInt(duration) || 1;
Expand All @@ -300,7 +302,9 @@ export function durationToSeconds(durationText: string): number {
minute: 60,
second: 1,
}[unit];
if (!multiplier) throw new Error(`Invalid duration unit: ${unit}`);
if (!multiplier) {
throw new Error(`Invalid duration unit: ${unit}`);
}

return durationInt * multiplier;
}
Expand All @@ -309,7 +313,9 @@ export function durationToISO8601(durationText: string): string {
const match = /^(a|\d+)\s(year|month|week|day|hour|minute|second)s?$/.exec(
durationText
);
if (!match) throw new Error(`Invalid duration: ${durationText}`);
if (!match) {
throw new Error(`Invalid duration: ${durationText}`);
}

const [_, duration, unit] = match;
const durationInt = parseInt(duration) || 1;
Expand All @@ -322,7 +328,9 @@ export function durationToISO8601(durationText: string): string {
minute: "TM",
second: "TS",
}[unit];
if (!durationUnit) throw new Error(`Invalid duration unit: ${unit}`);
if (!durationUnit) {
throw new Error(`Invalid duration unit: ${unit}`);
}

return `P${durationInt}${durationUnit}`;
}
Expand Down

0 comments on commit e2e969c

Please sign in to comment.