@dicelette/core 1.28.4 → 1.29.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
@@ -1,3 +1,72 @@
1
+ // src/custom_formula.ts
2
+ import { evaluate } from "mathjs";
3
+
4
+ // src/interfaces/constant.ts
5
+ var COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(?<comment>.*)/gi;
6
+ var SIGN_REGEX = /==|!=|(?<![!<>])>=|(?<![!<>])<=|(?<!!)(?<![<>])>|(?<!!)(?<![<>])<|(?<!!)(?<![<>])=/;
7
+ var SIGN_REGEX_SPACE = /(==|!=|(?<![!<>])>=|(?<![!<>])<=|(?<!!)(?<![<>])>|(?<!!)(?<![<>])<|(?<!!)(?<![<>])=)(\S+)/;
8
+ var SYMBOL_DICE = "&";
9
+ var DETECT_CRITICAL = /\{\*?c[fs]:([<>=]|!=)+(.+?)}/gim;
10
+ var OPTIONAL_COMMENT = /\s+(#|\/{2}|\[|\/\*)?(?<comment>.*)/gi;
11
+ var MIN_THRESHOLD_MATCH = 0.5;
12
+
13
+ // src/regex.ts
14
+ var REGEX_CACHE_MAX = 500;
15
+ var regexCache = /* @__PURE__ */ new Map();
16
+ var NORMALIZE_SINGLE_DICE = (str) => str.replace(/\b1d(\d+)/gi, "d$1");
17
+ var REMOVER_PATTERN = {
18
+ ASTERISK_ESCAPE: /\*/g,
19
+ CRITICAL_BLOCK: /\{\*?c[fs]:([<>=]|!=)+.+?\}/gim,
20
+ EXP_REMOVER: /\{exp(.*?)\}/g,
21
+ SIGN_REMOVER: /([><=]|!=)+.*$/,
22
+ STAT_COMMENTS_REMOVER: /%%.*%%/,
23
+ STAT_MATCHER: /\(?\$([\p{L}\p{M}_.][\p{L}\p{M}0-9_.]*)\)?/giu
24
+ };
25
+ function getCachedRegex(pattern, flags = "") {
26
+ const key = `${pattern}|${flags}`;
27
+ let regex = regexCache.get(key);
28
+ if (!regex) {
29
+ if (regexCache.size >= REGEX_CACHE_MAX) {
30
+ const oldest = regexCache.keys().next().value;
31
+ if (oldest !== void 0) regexCache.delete(oldest);
32
+ }
33
+ regex = new RegExp(pattern, flags);
34
+ regexCache.set(key, regex);
35
+ }
36
+ return regex;
37
+ }
38
+ function includeDiceType(dice, diceType, userStats) {
39
+ if (!diceType) return false;
40
+ diceType = NORMALIZE_SINGLE_DICE(diceType);
41
+ dice = NORMALIZE_SINGLE_DICE(dice);
42
+ if (userStats && diceType.includes("$")) {
43
+ diceType = diceType.replace("$", ".+?");
44
+ }
45
+ if (SIGN_REGEX.test(diceType)) {
46
+ diceType = diceType.replace(REMOVER_PATTERN.SIGN_REMOVER, "").trim();
47
+ dice = dice.replace(REMOVER_PATTERN.SIGN_REMOVER, "").trim();
48
+ }
49
+ if (diceType.includes("{exp")) {
50
+ diceType = diceType.replace(REMOVER_PATTERN.EXP_REMOVER, "").trim();
51
+ dice = dice.replace(REMOVER_PATTERN.EXP_REMOVER, "").trim();
52
+ }
53
+ const detectDiceType = getCachedRegex(`\\b${diceType}\\b`, "i");
54
+ return detectDiceType.test(dice);
55
+ }
56
+
57
+ // src/custom_formula.ts
58
+ var DICE_NOTATION = /\b\d*d\d+\b/gi;
59
+ function validateCustomFormula(formula) {
60
+ const expr = formula.replaceAll("$", "50").replace(REMOVER_PATTERN.CRITICAL_BLOCK, "").replace(REMOVER_PATTERN.EXP_REMOVER, "").replace(DICE_NOTATION, "3").trim();
61
+ if (!expr) return { ok: false, error: "Empty formula" };
62
+ try {
63
+ evaluate(expr);
64
+ return { ok: true };
65
+ } catch (e) {
66
+ return { ok: false, error: e instanceof Error ? e.message : String(e) };
67
+ }
68
+ }
69
+
1
70
  // src/engine.ts
2
71
  import { NumberGenerator } from "@dice-roller/rpg-dice-roller";
3
72
  function getEngineId(engine) {
@@ -92,15 +161,6 @@ var NoStatisticsError = class extends Error {
92
161
  }
93
162
  };
94
163
 
95
- // src/interfaces/constant.ts
96
- var COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(?<comment>.*)/gi;
97
- var SIGN_REGEX = /==|!=|(?<![!<>])>=|(?<![!<>])<=|(?<!!)(?<![<>])>|(?<!!)(?<![<>])<|(?<!!)(?<![<>])=/;
98
- var SIGN_REGEX_SPACE = /(==|!=|(?<![!<>])>=|(?<![!<>])<=|(?<!!)(?<![<>])>|(?<!!)(?<![<>])<|(?<!!)(?<![<>])=)(\S+)/;
99
- var SYMBOL_DICE = "&";
100
- var DETECT_CRITICAL = /\{\*?c[fs]:([<>=]|!=)+(.+?)}/gim;
101
- var OPTIONAL_COMMENT = /\s+(#|\/{2}|\[|\/\*)?(?<comment>.*)/gi;
102
- var MIN_THRESHOLD_MATCH = 0.5;
103
-
104
164
  // src/interfaces/index.ts
105
165
  var SortOrder = /* @__PURE__ */ ((SortOrder2) => {
106
166
  SortOrder2["Ascending"] = "sa";
@@ -165,57 +225,13 @@ var templateSchema = z.object({
165
225
  damage: damageSchema
166
226
  });
167
227
 
168
- // src/regex.ts
169
- var REGEX_CACHE_MAX = 500;
170
- var regexCache = /* @__PURE__ */ new Map();
171
- var NORMALIZE_SINGLE_DICE = (str) => str.replace(/\b1d(\d+)/gi, "d$1");
172
- var REMOVER_PATTERN = {
173
- ASTERISK_ESCAPE: /\*/g,
174
- CRITICAL_BLOCK: /\{\*?c[fs]:([<>=]|!=)+.+?\}/gim,
175
- EXP_REMOVER: /\{exp(.*?)\}/g,
176
- SIGN_REMOVER: /([><=]|!=)+.*$/,
177
- STAT_COMMENTS_REMOVER: /%%.*%%/,
178
- STAT_MATCHER: /\(?\$([\p{L}\p{M}_.][\p{L}\p{M}0-9_.]*)\)?/giu
179
- };
180
- function getCachedRegex(pattern, flags = "") {
181
- const key = `${pattern}|${flags}`;
182
- let regex = regexCache.get(key);
183
- if (!regex) {
184
- if (regexCache.size >= REGEX_CACHE_MAX) {
185
- const oldest = regexCache.keys().next().value;
186
- if (oldest !== void 0) regexCache.delete(oldest);
187
- }
188
- regex = new RegExp(pattern, flags);
189
- regexCache.set(key, regex);
190
- }
191
- return regex;
192
- }
193
- function includeDiceType(dice, diceType, userStats) {
194
- if (!diceType) return false;
195
- diceType = NORMALIZE_SINGLE_DICE(diceType);
196
- dice = NORMALIZE_SINGLE_DICE(dice);
197
- if (userStats && diceType.includes("$")) {
198
- diceType = diceType.replace("$", ".+?");
199
- }
200
- if (SIGN_REGEX.test(diceType)) {
201
- diceType = diceType.replace(REMOVER_PATTERN.SIGN_REMOVER, "").trim();
202
- dice = dice.replace(REMOVER_PATTERN.SIGN_REMOVER, "").trim();
203
- }
204
- if (diceType.includes("{exp")) {
205
- diceType = diceType.replace(REMOVER_PATTERN.EXP_REMOVER, "").trim();
206
- dice = dice.replace(REMOVER_PATTERN.EXP_REMOVER, "").trim();
207
- }
208
- const detectDiceType = getCachedRegex(`\\b${diceType}\\b`, "i");
209
- return detectDiceType.test(dice);
210
- }
211
-
212
228
  // src/roll.ts
213
229
  import { DiceRoller as DiceRoller3, NumberGenerator as NumberGenerator8 } from "@dice-roller/rpg-dice-roller";
214
- import { evaluate as evaluate10 } from "mathjs";
230
+ import { evaluate as evaluate11 } from "mathjs";
215
231
 
216
232
  // src/dice/bulk.ts
217
233
  import { DiceRoller as DiceRoller2, NumberGenerator as NumberGenerator7 } from "@dice-roller/rpg-dice-roller";
218
- import { evaluate as evaluate8 } from "mathjs";
234
+ import { evaluate as evaluate9 } from "mathjs";
219
235
 
220
236
  // src/utils.ts
221
237
  import "uniformize";
@@ -223,7 +239,7 @@ import { NumberGenerator as NumberGenerator3 } from "@dice-roller/rpg-dice-rolle
223
239
  import { Random as Random2 } from "random-js";
224
240
 
225
241
  // src/verify_template.ts
226
- import { evaluate } from "mathjs";
242
+ import { evaluate as evaluate2 } from "mathjs";
227
243
  import { Random } from "random-js";
228
244
  import "uniformize";
229
245
  import { NumberGenerator as NumberGenerator2 } from "@dice-roller/rpg-dice-roller";
@@ -291,7 +307,7 @@ function evalCombinaison(combinaison, stats) {
291
307
  formula = formula.replace(regex, value.toString());
292
308
  }
293
309
  try {
294
- newStats[stat] = evaluate(formula);
310
+ newStats[stat] = evaluate2(formula);
295
311
  } catch (error) {
296
312
  throw new FormulaError(stat, "evalCombinaison", error);
297
313
  }
@@ -305,7 +321,7 @@ function evalOneCombinaison(combinaison, stats) {
305
321
  formula = formula.replace(regex, value.toString());
306
322
  }
307
323
  try {
308
- return evaluate(formula);
324
+ return evaluate2(formula);
309
325
  } catch (error) {
310
326
  throw new FormulaError(combinaison, "evalOneCombinaison", error);
311
327
  }
@@ -410,7 +426,7 @@ function testStatCombinaison(template, engine = NumberGenerator2.engines.nodeCry
410
426
  formula = formula.replace(regex, randomStatValue.toString());
411
427
  }
412
428
  try {
413
- evaluate(formula);
429
+ evaluate2(formula);
414
430
  } catch (e) {
415
431
  error.push(stat);
416
432
  }
@@ -468,7 +484,7 @@ function createCriticalCustom(dice, customCritical, template, engine = NumberGen
468
484
 
469
485
  // src/dice/compare.ts
470
486
  import { NumberGenerator as NumberGenerator4 } from "@dice-roller/rpg-dice-roller";
471
- import { evaluate as evaluate2 } from "mathjs";
487
+ import { evaluate as evaluate3 } from "mathjs";
472
488
  function isTrivialComparison(maxValue, minValue, compare) {
473
489
  const canSucceed = canComparisonSucceed(maxValue, compare, minValue);
474
490
  const canFail = canComparisonFail(maxValue, compare, minValue);
@@ -507,7 +523,7 @@ function rollCompare(value, engine = NumberGenerator4.engines.nodeCrypto, pity)
507
523
  const rollComp = roll(value, engine, pity);
508
524
  if (!rollComp?.total) {
509
525
  try {
510
- return { value: evaluate2(value), diceResult: value };
526
+ return { value: evaluate3(value), diceResult: value };
511
527
  } catch (error) {
512
528
  return { value: 0, diceResult: value };
513
529
  }
@@ -529,7 +545,7 @@ function getCompare(dice, compareRegex, engine = NumberGenerator4.engines.nodeCr
529
545
  if (sign) {
530
546
  const toCalc = calc.replace(SIGN_REGEX, "").replace(/\s/g, "").replace(/;(.*)/, "");
531
547
  const rCompare = rollCompare(toCalc, engine, pity);
532
- const total = evaluate2(rCompare.value.toString());
548
+ const total = evaluate3(rCompare.value.toString());
533
549
  dice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);
534
550
  compare = {
535
551
  sign: compareSign,
@@ -571,11 +587,11 @@ function canComparisonSucceed(maxRollValue, compare, minRollValue) {
571
587
  }
572
588
 
573
589
  // src/dice/exploding.ts
574
- import { evaluate as evaluate4 } from "mathjs";
590
+ import { evaluate as evaluate5 } from "mathjs";
575
591
 
576
592
  // src/dice/signs.ts
577
593
  import { NumberGenerator as NumberGenerator5 } from "@dice-roller/rpg-dice-roller";
578
- import { evaluate as evaluate3 } from "mathjs";
594
+ import { evaluate as evaluate4 } from "mathjs";
579
595
 
580
596
  // src/dice/replace.ts
581
597
  var PARENTHESIS_REGEX = /d\((\d+)\)/g;
@@ -668,7 +684,7 @@ function compareSignFormule(toRoll, compareRegex, element, diceResult, engine =
668
684
  const toCompare = `${compareResult.dice}${compareResult.compare?.sign}${compareResult.compare?.value}`;
669
685
  let res;
670
686
  try {
671
- res = evaluate3(toCompare);
687
+ res = evaluate4(toCompare);
672
688
  } catch (error) {
673
689
  res = roll(toCompare, engine, pity);
674
690
  }
@@ -680,7 +696,7 @@ function compareSignFormule(toRoll, compareRegex, element, diceResult, engine =
680
696
  } else if (res instanceof Object) {
681
697
  const diceResult2 = res;
682
698
  if (diceResult2.compare) {
683
- const toEvaluate = evaluate3(
699
+ const toEvaluate = evaluate4(
684
700
  `${diceResult2.total}${diceResult2.compare.sign}${diceResult2.compare.value}`
685
701
  );
686
702
  const sign = toEvaluate ? "\u2713" : "\u2715";
@@ -717,7 +733,7 @@ function normalizeExplodingSuccess(dice) {
717
733
  let parsedValue = Number.parseFloat(valueStr);
718
734
  if (Number.isNaN(parsedValue)) {
719
735
  try {
720
- parsedValue = Number.parseFloat(evaluate4(valueStr));
736
+ parsedValue = Number.parseFloat(evaluate5(valueStr));
721
737
  } catch (_error) {
722
738
  parsedValue = 0;
723
739
  }
@@ -755,7 +771,7 @@ function countExplodingSuccesses(diceRoll, sign, value) {
755
771
  import { DiceRoller, NumberGenerator as NumberGenerator6 } from "@dice-roller/rpg-dice-roller";
756
772
 
757
773
  // src/similarities/generateStatsDice.ts
758
- import { evaluate as evaluate5 } from "mathjs";
774
+ import { evaluate as evaluate6 } from "mathjs";
759
775
 
760
776
  // src/similarities/similarity.ts
761
777
  function calculateSimilarity(str1, str2) {
@@ -913,7 +929,7 @@ function replaceFormulaInDice(dice) {
913
929
  if (match.groups?.formula) {
914
930
  const formulae = match.groups.formula.replaceAll("{{", "").replaceAll("}}", "");
915
931
  try {
916
- const result = evaluate5(formulae);
932
+ const result = evaluate6(formulae);
917
933
  modifiedDice = modifiedDice.replace(match.groups.formula, result.toString());
918
934
  } catch (error) {
919
935
  throw new FormulaError(match.groups.formula, "replaceFormulasInDice", error);
@@ -927,7 +943,7 @@ function cleanedDice(dice) {
927
943
  }
928
944
 
929
945
  // src/similarities/resolveFormula.ts
930
- import { evaluate as evaluate6 } from "mathjs";
946
+ import { evaluate as evaluate7 } from "mathjs";
931
947
  function toFiniteNumber(value) {
932
948
  if (typeof value === "number" && Number.isFinite(value)) return value;
933
949
  return void 0;
@@ -967,7 +983,7 @@ function resolveFormulaHint(formula, allAttributes, similarityThreshold = MIN_TH
967
983
  progress = false;
968
984
  for (const [normName, expr2] of pending) {
969
985
  try {
970
- const result = toFiniteNumber(evaluate6(expr2));
986
+ const result = toFiniteNumber(evaluate7(expr2));
971
987
  if (result === void 0) continue;
972
988
  resolved.set(normName, result);
973
989
  pending.delete(normName);
@@ -985,7 +1001,7 @@ function resolveFormulaHint(formula, allAttributes, similarityThreshold = MIN_TH
985
1001
  const normFormula = trimmed.standardize();
986
1002
  const expr = substituteFormulaTokens(normFormula, resolved, similarityThreshold);
987
1003
  try {
988
- const result = toFiniteNumber(evaluate6(expr));
1004
+ const result = toFiniteNumber(evaluate7(expr));
989
1005
  if (result !== void 0) return { kind: "resolved", value: result };
990
1006
  return { kind: "error" };
991
1007
  } catch {
@@ -994,10 +1010,10 @@ function resolveFormulaHint(formula, allAttributes, similarityThreshold = MIN_TH
994
1010
  }
995
1011
 
996
1012
  // src/dice/calculator.ts
997
- import { evaluate as evaluate7 } from "mathjs";
1013
+ import { evaluate as evaluate8 } from "mathjs";
998
1014
  function calculator(sign, value, total) {
999
1015
  if (sign === "^") sign = "**";
1000
- return evaluate7(`${total} ${sign} ${value}`);
1016
+ return evaluate8(`${total} ${sign} ${value}`);
1001
1017
  }
1002
1018
 
1003
1019
  // src/dice/extract.ts
@@ -1206,7 +1222,7 @@ function handleBulkRollsWithComparison(numberOfDice, diceToRoll, comments, activ
1206
1222
  results.push(formattedRollOutput);
1207
1223
  } else {
1208
1224
  const rollTotal = rollInstance.total;
1209
- const isSuccess = evaluate8(
1225
+ const isSuccess = evaluate9(
1210
1226
  `${rollTotal}${activeCompare.sign}${activeCompare.value}`
1211
1227
  );
1212
1228
  if (isSuccess) successCount++;
@@ -1269,7 +1285,7 @@ function handleBulkRollsWithComparison(numberOfDice, diceToRoll, comments, activ
1269
1285
  }
1270
1286
 
1271
1287
  // src/dice/pity.ts
1272
- import { evaluate as evaluate9 } from "mathjs";
1288
+ import { evaluate as evaluate10 } from "mathjs";
1273
1289
  function handlePitySystem(dice, compare, diceRoll, roller, engine) {
1274
1290
  const currentRoll = Array.isArray(diceRoll) ? diceRoll[0] : diceRoll;
1275
1291
  const maxPossible = currentRoll ? currentRoll.maxTotal : null;
@@ -1277,7 +1293,7 @@ function handlePitySystem(dice, compare, diceRoll, roller, engine) {
1277
1293
  if (!isComparisonPossible) {
1278
1294
  return { rerollCount: 0 };
1279
1295
  }
1280
- let isFail = evaluate9(`${roller.total}${compare.sign}${compare.value}`);
1296
+ let isFail = evaluate10(`${roller.total}${compare.sign}${compare.value}`);
1281
1297
  if (isFail) {
1282
1298
  return { rerollCount: 0 };
1283
1299
  }
@@ -1292,7 +1308,7 @@ function handlePitySystem(dice, compare, diceRoll, roller, engine) {
1292
1308
  }
1293
1309
  rerollCount++;
1294
1310
  if (res && res.total !== void 0) {
1295
- isFail = evaluate9(`${res.total}${compare.sign}${compare.value}`);
1311
+ isFail = evaluate10(`${res.total}${compare.sign}${compare.value}`);
1296
1312
  }
1297
1313
  }
1298
1314
  if (!res?.result.length) throw new DiceTypeError(dice, "empty_dice");
@@ -1503,7 +1519,7 @@ function sharedRolls(dice, engine = NumberGenerator8.engines.nodeCrypto, pity, e
1503
1519
  const { diceAll } = replaceText(element, diceResult.total, diceResult.dice);
1504
1520
  let successCount = 0;
1505
1521
  try {
1506
- const evaluated = evaluate10(toRoll);
1522
+ const evaluated = evaluate11(toRoll);
1507
1523
  successCount = evaluated ? 1 : 0;
1508
1524
  } catch (error) {
1509
1525
  const evaluated = roll(toRoll, engine, pity);
@@ -1537,7 +1553,7 @@ function sharedRolls(dice, engine = NumberGenerator8.engines.nodeCrypto, pity, e
1537
1553
  diceResult.dice
1538
1554
  );
1539
1555
  try {
1540
- const evaluated = evaluate10(toRoll);
1556
+ const evaluated = evaluate11(toRoll);
1541
1557
  results.push(`\u25C8 ${comment}${diceAll}: ${formule} = ${evaluated}`);
1542
1558
  total += Number.parseInt(evaluated, 10);
1543
1559
  } catch (error) {
@@ -1576,7 +1592,7 @@ function replaceInFormula(element, diceResult, compareResult, res, engine = Numb
1576
1592
  const invertedSign = res ? compareResult.compare.sign : inverseSign(compareResult.compare.sign);
1577
1593
  let evaluateRoll;
1578
1594
  try {
1579
- evaluateRoll = evaluate10(compareResult.dice);
1595
+ evaluateRoll = evaluate11(compareResult.dice);
1580
1596
  return `${validSign} ${diceAll}: ${formule} = ${evaluateRoll}${invertedSign}${compareResult.compare?.value}`;
1581
1597
  } catch (error) {
1582
1598
  const evaluateRoll2 = roll(compareResult.dice, engine, pity);
@@ -1632,6 +1648,7 @@ export {
1632
1648
  templateSchema,
1633
1649
  testDiceRegistered,
1634
1650
  testStatCombinaison,
1651
+ validateCustomFormula,
1635
1652
  verifyStatMatcherPattern,
1636
1653
  verifyTemplateValue
1637
1654
  };