@dicelette/core 1.27.0 → 1.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -92,14 +92,6 @@ var NoStatisticsError = class extends Error {
92
92
  }
93
93
  };
94
94
 
95
- // src/interfaces/index.ts
96
- var SortOrder = /* @__PURE__ */ ((SortOrder2) => {
97
- SortOrder2["Ascending"] = "sa";
98
- SortOrder2["Descending"] = "sd";
99
- SortOrder2["None"] = "none";
100
- return SortOrder2;
101
- })(SortOrder || {});
102
-
103
95
  // src/interfaces/constant.ts
104
96
  var COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(?<comment>.*)/gi;
105
97
  var SIGN_REGEX = /==|!=|(?<![!<>])>=|(?<![!<>])<=|(?<!!)(?<![<>])>|(?<!!)(?<![<>])<|(?<!!)(?<![<>])=/;
@@ -107,6 +99,15 @@ var SIGN_REGEX_SPACE = /(==|!=|(?<![!<>])>=|(?<![!<>])<=|(?<!!)(?<![<>])>|(?<!!)
107
99
  var SYMBOL_DICE = "&";
108
100
  var DETECT_CRITICAL = /\{\*?c[fs]:([<>=]|!=)+(.+?)}/gim;
109
101
  var OPTIONAL_COMMENT = /\s+(#|\/{2}|\[|\/\*)?(?<comment>.*)/gi;
102
+ var MIN_THRESHOLD_MATCH = 0.5;
103
+
104
+ // src/interfaces/index.ts
105
+ var SortOrder = /* @__PURE__ */ ((SortOrder2) => {
106
+ SortOrder2["Ascending"] = "sa";
107
+ SortOrder2["Descending"] = "sd";
108
+ SortOrder2["None"] = "none";
109
+ return SortOrder2;
110
+ })(SortOrder || {});
110
111
 
111
112
  // src/interfaces/zod.ts
112
113
  import { z } from "zod";
@@ -164,6 +165,45 @@ var templateSchema = z.object({
164
165
  damage: damageSchema
165
166
  });
166
167
 
168
+ // src/regex.ts
169
+ var regexCache = /* @__PURE__ */ new Map();
170
+ var NORMALIZE_SINGLE_DICE = (str) => str.replace(/\b1d(\d+)/gi, "d$1");
171
+ var REMOVER_PATTERN = {
172
+ ASTERISK_ESCAPE: /\*/g,
173
+ CRITICAL_BLOCK: /\{\*?c[fs]:([<>=]|!=)+.+?\}/gim,
174
+ EXP_REMOVER: /\{exp(.*?)\}/g,
175
+ SIGN_REMOVER: /([><=]|!=)+.*$/,
176
+ STAT_COMMENTS_REMOVER: /%%.*%%/,
177
+ STAT_MATCHER: /\(?\$([\p{L}\p{M}_.][\p{L}\p{M}0-9_.]*)\)?/giu
178
+ };
179
+ function getCachedRegex(pattern, flags = "") {
180
+ const key = `${pattern}|${flags}`;
181
+ let regex = regexCache.get(key);
182
+ if (!regex) {
183
+ regex = new RegExp(pattern, flags);
184
+ regexCache.set(key, regex);
185
+ }
186
+ return regex;
187
+ }
188
+ function includeDiceType(dice, diceType, userStats) {
189
+ if (!diceType) return false;
190
+ diceType = NORMALIZE_SINGLE_DICE(diceType);
191
+ dice = NORMALIZE_SINGLE_DICE(dice);
192
+ if (userStats && diceType.includes("$")) {
193
+ diceType = diceType.replace("$", ".+?");
194
+ }
195
+ if (SIGN_REGEX.test(diceType)) {
196
+ diceType = diceType.replace(REMOVER_PATTERN.SIGN_REMOVER, "").trim();
197
+ dice = dice.replace(REMOVER_PATTERN.SIGN_REMOVER, "").trim();
198
+ }
199
+ if (diceType.includes("{exp")) {
200
+ diceType = diceType.replace(REMOVER_PATTERN.EXP_REMOVER, "").trim();
201
+ dice = dice.replace(REMOVER_PATTERN.EXP_REMOVER, "").trim();
202
+ }
203
+ const detectDiceType = getCachedRegex(`\\b${diceType}\\b`, "i");
204
+ return detectDiceType.test(dice);
205
+ }
206
+
167
207
  // src/roll.ts
168
208
  import { DiceRoller as DiceRoller3, NumberGenerator as NumberGenerator7 } from "@dice-roller/rpg-dice-roller";
169
209
  import { evaluate as evaluate8 } from "mathjs";
@@ -183,7 +223,6 @@ import { NumberGenerator as NumberGenerator2 } from "@dice-roller/rpg-dice-rolle
183
223
  import { Random } from "random-js";
184
224
 
185
225
  // src/similarity.ts
186
- var MIN_THRESHOLD_MATCH = 0.5;
187
226
  function calculateSimilarity(str1, str2) {
188
227
  const longer = str1.length > str2.length ? str1 : str2;
189
228
  const shorter = str1.length > str2.length ? str2 : str1;
@@ -246,6 +285,19 @@ function findBestRecord(record, searchTerm, similarityThreshold = MIN_THRESHOLD_
246
285
  similarityThreshold
247
286
  ) || null;
248
287
  }
288
+ function replaceUnknown(dice, replacer) {
289
+ return dice.replaceAll(REMOVER_PATTERN.STAT_MATCHER, replacer).replaceAll("+0", "").replaceAll("-0", "");
290
+ }
291
+ function verifyStatMatcherPattern(dice, replaceUnknow) {
292
+ if (REMOVER_PATTERN.STAT_MATCHER.test(dice)) {
293
+ if (replaceUnknow)
294
+ return replaceUnknown(dice, replaceUnknow);
295
+ const matched = dice.matchAll(new RegExp(REMOVER_PATTERN.STAT_MATCHER));
296
+ const stats = matched ? Array.from(matched, (m) => m?.[0]).map((s) => `\`${s}\``).join(", ") : "unknown";
297
+ throw new DiceTypeError("error.invalidDice.stats");
298
+ }
299
+ return dice.replaceAll("+0", "").replaceAll("-0", "");
300
+ }
249
301
 
250
302
  // src/utils.ts
251
303
  function escapeRegex(string) {
@@ -1466,9 +1518,12 @@ export {
1466
1518
  DiceTypeError,
1467
1519
  EmptyObjectError,
1468
1520
  FormulaError,
1521
+ MIN_THRESHOLD_MATCH,
1469
1522
  MaxGreater,
1523
+ NORMALIZE_SINGLE_DICE,
1470
1524
  NoStatisticsError,
1471
1525
  OPTIONAL_COMMENT,
1526
+ REMOVER_PATTERN,
1472
1527
  SIGN_REGEX,
1473
1528
  SIGN_REGEX_SPACE,
1474
1529
  SYMBOL_DICE,
@@ -1487,19 +1542,23 @@ export {
1487
1542
  findBestStatMatch,
1488
1543
  generateRandomStat,
1489
1544
  generateStatsDice,
1545
+ getCachedRegex,
1490
1546
  getEngine,
1491
1547
  getEngineId,
1548
+ includeDiceType,
1492
1549
  isNumber,
1493
1550
  levenshteinDistance,
1494
1551
  randomInt,
1495
1552
  replaceExpByRandom,
1496
1553
  replaceFormulaInDice,
1497
1554
  replaceInFormula,
1555
+ replaceUnknown,
1498
1556
  roll,
1499
1557
  standardizeDice,
1500
1558
  templateSchema,
1501
1559
  testDiceRegistered,
1502
1560
  testStatCombinaison,
1561
+ verifyStatMatcherPattern,
1503
1562
  verifyTemplateValue
1504
1563
  };
1505
1564
  //# sourceMappingURL=index.mjs.map