Skip to content

Commit

Permalink
fix: HH:MM:SS timestamps not being parsed correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern committed Feb 14, 2023
1 parent dd15b79 commit abc56b4
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,20 +334,9 @@ function parseComposerContent<T extends string>(submission: Interaction): Compos
return content as ComposerContent<T>;
}

const digitsExpression = new RegExp(/\d+/g);
const stringsExpression = new RegExp(/\p{L}+/gu);

function extractNumbers(expression: string): number[] {
return (expression.match(digitsExpression) ?? []).map((digits) => Number(digits));
}

function extractStrings(expression: string): string[] {
return expression.match(stringsExpression) ?? [];
}

// Expression to detect HH:MM:SS, MM:SS and SS timestamps.
const shortTimeExpression = new RegExp(
/^(?:(0?[0-9]|1[0-9]|2[0-4]):)?(?:(0?[0-9]|[1-5][0-9]|60):)?(0?[0-9]|[1-5][0-9]|60)$/,
/^(?:(?:(0?[0-9]|1[0-9]|2[0-4]):)?(?:(0?[0-9]|[1-5][0-9]|60):))?(0?[0-9]|[1-5][0-9]|60)$/,
);

function parseTimeExpression(
Expand All @@ -364,7 +353,7 @@ function parseShortTimeExpression(
convertToPhrase: boolean,
locale: string | undefined,
): ReturnType<typeof parseTimeExpression> {
const [secondsPart, minutesPart, hoursPart] = shortTimeExpression.exec(expression)!.slice(0).toReversed();
const [secondsPart, minutesPart, hoursPart] = shortTimeExpression.exec(expression)!.slice(1).toReversed();

const [seconds, minutes, hours] = [secondsPart, minutesPart, hoursPart].map((part) =>
part !== undefined ? Number(part) : undefined
Expand All @@ -381,12 +370,15 @@ function parseShortTimeExpression(
return [expression, totalSeconds * 1000];
}

let correctedExpression = `${seconds} ${localise(Misc.time.periods.second.descriptors, locale).at(-1)}`;
if (minutes !== undefined) {
correctedExpression += ` ${minutes} ${localise(Misc.time.periods.minute.descriptors, locale).at(-1)}`;
let correctedExpression = '';
if (seconds !== undefined && seconds !== 0) {
correctedExpression += `${seconds} ${localise(Misc.time.periods.second.descriptors, locale).at(-1)} `;
}
if (minutes !== undefined && minutes !== 0) {
correctedExpression += `${minutes} ${localise(Misc.time.periods.minute.descriptors, locale).at(-1)} `;
}
if (hours !== undefined) {
correctedExpression += ` ${hours} ${localise(Misc.time.periods.hour.descriptors, locale).at(-1)}`;
correctedExpression += `${hours} ${localise(Misc.time.periods.hour.descriptors, locale).at(-1)}`;
}

return parseTimeExpressionPhrase(correctedExpression, locale);
Expand All @@ -396,6 +388,16 @@ function parseTimeExpressionPhrase(
expression: string,
locale: string | undefined,
): ReturnType<typeof parseTimeExpression> {
function extractNumbers(expression: string): number[] {
const digitsExpression = new RegExp(/\d+/g);
return (expression.match(digitsExpression) ?? []).map((digits) => Number(digits));
}

function extractStrings(expression: string): string[] {
const stringsExpression = new RegExp(/\p{L}+/gu);
return expression.match(stringsExpression) ?? [];
}

// Extract the digits present in the expression.
const quantifiers = extractNumbers(expression).map((string) => Number(string));
// Extract the strings present in the expression.
Expand Down

0 comments on commit abc56b4

Please sign in to comment.