Skip to content

Commit 226e6a6

Browse files
committed
fix: replace QUERY_URL_PATTERNS with DICE_COMPILED_PATTERNS in roll and validation logic
1 parent 6be6b00 commit 226e6a6

File tree

8 files changed

+29
-23
lines changed

8 files changed

+29
-23
lines changed

packages/bot/src/commands/roll/snippets.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
CHARACTER_DETECTION,
1919
calculateSimilarity,
2020
capitalizeBetweenPunct,
21-
QUERY_URL_PATTERNS,
21+
QUERY_URL_PATTERNS, DICE_COMPILED_PATTERNS,
2222
} from "@dicelette/utils";
2323
import * as Djs from "discord.js";
2424
import { rollWithInteraction } from "utils";
@@ -106,7 +106,7 @@ export default {
106106
const composed = composeRollBase(
107107
dice,
108108
threshold,
109-
QUERY_URL_PATTERNS.COMPARATOR,
109+
DICE_COMPILED_PATTERNS.COMPARATOR,
110110
undefined,
111111
undefined,
112112
"",
@@ -133,7 +133,7 @@ export default {
133133
const composed = composeRollBase(
134134
processedDice,
135135
threshold,
136-
QUERY_URL_PATTERNS.COMPARATOR,
136+
DICE_COMPILED_PATTERNS.COMPARATOR,
137137
undefined,
138138
undefined,
139139
expressionStr,

packages/bot/src/messages/roll_handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ async function replyToSource(
242242
replyOptions.content = content.split("\n")[0];
243243
//and after remove it from content and send as a file
244244
let newContent = content.split("\n").slice(1).join("\n");
245-
const compiledComments = COMPILED_COMMENTS.exec(newContent)?.groups?.comment;
245+
246+
const compiledComments = new RegExp(COMPILED_COMMENTS).exec(newContent)?.groups?.comment;
246247
if (
247248
compiledComments &&
248249
compiledComments.length > 0 &&

packages/bot/src/utils/roll.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
skillCustomCritical,
2424
} from "@dicelette/parse_result";
2525
import type { RollOptions, Translation, UserData } from "@dicelette/types";
26-
import { capitalizeBetweenPunct, profiler, QUERY_URL_PATTERNS } from "@dicelette/utils";
26+
import {capitalizeBetweenPunct, DICE_COMPILED_PATTERNS, profiler, QUERY_URL_PATTERNS} from "@dicelette/utils";
2727
import { getRightValue, getTemplate } from "database";
2828
import * as Djs from "discord.js";
2929
import { embedError, handleRollResult, reply } from "messages";
@@ -166,7 +166,7 @@ export async function rollMacro(
166166
const composed = composeRollBase(
167167
dice,
168168
threshold,
169-
QUERY_URL_PATTERNS.COMPARATOR,
169+
DICE_COMPILED_PATTERNS.COMPARATOR,
170170
userStatistique.stats,
171171
dollarValue?.total,
172172
expressionStr,
@@ -300,7 +300,7 @@ export async function rollStatistique(
300300
const composed = composeRollBase(
301301
dice,
302302
threshold,
303-
QUERY_URL_PATTERNS.COMPARATOR_SIMPLE,
303+
DICE_COMPILED_PATTERNS.COMPARATOR_SIMPLE,
304304
userStatistique.stats,
305305
userStatStr,
306306
expressionStr,

packages/parse_result/src/compose_roll.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DETECT_CRITICAL, generateStatsDice } from "@dicelette/core";
2-
import { QUERY_URL_PATTERNS } from "@dicelette/utils";
2+
import {DICE_COMPILED_PATTERNS, QUERY_URL_PATTERNS} from "@dicelette/utils";
33
import { trimAll } from "./utils";
44

55
/**
@@ -24,8 +24,8 @@ export function extractComparator(
2424
*/
2525
export function getThreshold(dice: string, threshold?: string): string {
2626
if (!threshold) return dice;
27-
const diceMatch = QUERY_URL_PATTERNS.COMPARATOR.exec(dice);
28-
const thresholdMatch = QUERY_URL_PATTERNS.COMPARATOR.exec(threshold);
27+
const diceMatch = DICE_COMPILED_PATTERNS.COMPARATOR.exec(dice);
28+
const thresholdMatch = DICE_COMPILED_PATTERNS.COMPARATOR.exec(threshold);
2929
if (thresholdMatch) {
3030
if (diceMatch) return dice.replace(diceMatch[0], thresholdMatch[0]);
3131
return dice + thresholdMatch[0];

packages/parse_result/src/custom_critical.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {
55
isNumber,
66
} from "@dicelette/core";
77
import type { CustomCriticalRoll, Translation } from "@dicelette/types";
8-
import {BotError, BotErrorLevel, type BotErrorOptions, QUERY_URL_PATTERNS} from "@dicelette/utils";
8+
import {
9+
BotError,
10+
BotErrorLevel,
11+
type BotErrorOptions,
12+
DICE_COMPILED_PATTERNS,
13+
} from "@dicelette/utils";
914
import { evaluate } from "mathjs";
1015
import { getRoll } from "./dice_extractor";
1116

@@ -21,7 +26,7 @@ export function parseCustomCritical(
2126
name: string,
2227
customCritical: string
2328
): Record<string, CustomCritical> | undefined {
24-
const findPart = new RegExp(QUERY_URL_PATTERNS.COMPARATOR, 'gi')
29+
const findPart = new RegExp(DICE_COMPILED_PATTERNS.COMPARATOR, 'gi')
2530
const match = findPart.exec(customCritical);
2631
if (!match) return;
2732
let { sign, comparator:value } = match.groups || {};

packages/parse_result/tests/compose_roll.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { QUERY_URL_PATTERNS} from "@dicelette/utils";
1+
import {DICE_COMPILED_PATTERNS} from "@dicelette/utils";
22
import { describe, expect, it } from "vitest";
33
import { composeRollBase, extractComparator, getThreshold } from "../src/compose_roll";
44

@@ -181,7 +181,7 @@ describe("composeRollBase", () => {
181181
const r = composeRollBase(
182182
"2d6>=10",
183183
undefined,
184-
QUERY_URL_PATTERNS.COMPARATOR,
184+
DICE_COMPILED_PATTERNS.COMPARATOR,
185185
undefined,
186186
undefined,
187187
"",
@@ -195,7 +195,7 @@ describe("composeRollBase", () => {
195195
const r = composeRollBase(
196196
"2d6>=10",
197197
undefined,
198-
QUERY_URL_PATTERNS.COMPARATOR,
198+
DICE_COMPILED_PATTERNS.COMPARATOR,
199199
undefined,
200200
undefined,
201201
"+3",
@@ -207,7 +207,7 @@ describe("composeRollBase", () => {
207207
const r = composeRollBase(
208208
"2d6>=10",
209209
">=15",
210-
QUERY_URL_PATTERNS.COMPARATOR,
210+
DICE_COMPILED_PATTERNS.COMPARATOR,
211211
undefined,
212212
undefined,
213213
"",
@@ -220,7 +220,7 @@ describe("composeRollBase", () => {
220220
const r = composeRollBase(
221221
"2d6{cf:<=2}>=10",
222222
undefined,
223-
QUERY_URL_PATTERNS.COMPARATOR,
223+
DICE_COMPILED_PATTERNS.COMPARATOR,
224224
undefined,
225225
undefined,
226226
"",
@@ -232,7 +232,7 @@ describe("composeRollBase", () => {
232232
const r = composeRollBase(
233233
"1d20+strength>=15",
234234
undefined,
235-
QUERY_URL_PATTERNS.COMPARATOR,
235+
DICE_COMPILED_PATTERNS.COMPARATOR,
236236
{ strength: 3 },
237237
undefined,
238238
"",
@@ -246,7 +246,7 @@ describe("composeRollBase", () => {
246246
const r = composeRollBase(
247247
"1d20>=strength+5",
248248
undefined,
249-
QUERY_URL_PATTERNS.COMPARATOR,
249+
DICE_COMPILED_PATTERNS.COMPARATOR,
250250
{ strength: 4 },
251251
undefined,
252252
"",
@@ -260,7 +260,7 @@ describe("composeRollBase", () => {
260260
const r = composeRollBase(
261261
"1d20>=10",
262262
">=strength+5",
263-
QUERY_URL_PATTERNS.COMPARATOR,
263+
DICE_COMPILED_PATTERNS.COMPARATOR,
264264
{ strength: 6 },
265265
undefined,
266266
"",

packages/utils/src/regex.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Pre-compiled regex patterns for better performance
22
export const QUERY_URL_PATTERNS = {
33
AVATAR_URL: /^(https:\/{2})[\w\-./%]+\/[\w\-.%]+\.(jpe?g|gifv?|png|webp)$/gi,
4-
COMPARATOR: /(?<sign>([><=]|!=)+)(?<comparator>(.+))/,
5-
COMPARATOR_SIMPLE: /(([><=]|!=)+)(.+)/,
64
DISCORD_CDN: /(cdn|media)\.discordapp\.(net|com)/gi,
75
PUNCTUATION_ENCLOSED: /(?<open>\p{P})(?<enclosed>.*?)(?<close>\p{P})/gu,
86
QUERY_PARAMS: /\?.*$/g,
@@ -27,6 +25,8 @@ export const DICE_COMPILED_PATTERNS = {
2725
//old version: /(?<first>([><=!]+)(.+?))(?<second>([><=!]+)(.+))
2826
OPPOSITION: /(?<first>(([><=]|!=)+)(.+?))(?<second>(([><=]|!=)+)(.+))/,
2927
TARGET_VALUE: /^\{(.*?)}$/,
28+
COMPARATOR: /(?<sign>([><=]|!=)+)(?<comparator>(.+))/,
29+
COMPARATOR_SIMPLE: /(([><=]|!=)+)(.+)/,
3030
STATS_REGEX_CACHE: new Map<string, RegExp>(),
3131
} as const;
3232

packages/utils/tests/regex.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe("COMPILED_PATTERNS", () => {
163163
];
164164

165165
for (const { input, sign, comparator } of tests) {
166-
const match = QUERY_URL_PATTERNS.COMPARATOR.exec(input);
166+
const match = DICE_COMPILED_PATTERNS.COMPARATOR.exec(input);
167167
expect(match?.groups?.sign).toBe(sign);
168168
expect(match?.groups?.comparator).toBe(comparator);
169169
}

0 commit comments

Comments
 (0)