@dicelette/core 1.27.0 → 1.27.1

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.d.mts CHANGED
@@ -51,6 +51,14 @@ declare class NoStatisticsError extends Error {
51
51
  constructor();
52
52
  }
53
53
 
54
+ declare const COMMENT_REGEX: RegExp;
55
+ declare const SIGN_REGEX: RegExp;
56
+ declare const SIGN_REGEX_SPACE: RegExp;
57
+ declare const SYMBOL_DICE = "&";
58
+ declare const DETECT_CRITICAL: RegExp;
59
+ declare const OPTIONAL_COMMENT: RegExp;
60
+ declare const MIN_THRESHOLD_MATCH = 0.5;
61
+
54
62
  interface Resultat {
55
63
  /**
56
64
  * Original dice throw
@@ -237,13 +245,6 @@ declare enum SortOrder {
237
245
  None = "none"
238
246
  }
239
247
 
240
- declare const COMMENT_REGEX: RegExp;
241
- declare const SIGN_REGEX: RegExp;
242
- declare const SIGN_REGEX_SPACE: RegExp;
243
- declare const SYMBOL_DICE = "&";
244
- declare const DETECT_CRITICAL: RegExp;
245
- declare const OPTIONAL_COMMENT: RegExp;
246
-
247
248
  interface StatisticalSchema extends StatisticalTemplate {
248
249
  /**
249
250
  * Specifies the URL for the schema definition
@@ -293,6 +294,18 @@ declare const templateSchema: z.ZodObject<{
293
294
  damage: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
294
295
  }, z.core.$strip>;
295
296
 
297
+ declare const NORMALIZE_SINGLE_DICE: (str: string) => string;
298
+ declare const REMOVER_PATTERN: {
299
+ readonly ASTERISK_ESCAPE: RegExp;
300
+ readonly CRITICAL_BLOCK: RegExp;
301
+ readonly EXP_REMOVER: RegExp;
302
+ readonly SIGN_REMOVER: RegExp;
303
+ readonly STAT_COMMENTS_REMOVER: RegExp;
304
+ readonly STAT_MATCHER: RegExp;
305
+ };
306
+ declare function getCachedRegex(pattern: string, flags?: string): RegExp;
307
+ declare function includeDiceType(dice: string, diceType?: string, userStats?: boolean): boolean;
308
+
296
309
  /**
297
310
  * Parse the string provided and turn it as a readable dice for dice parser
298
311
  * @param {string} dice The dice string to parse and roll
@@ -326,6 +339,8 @@ declare function findBestStatMatch<T>(searchTerm: string, normalizedStats: Map<s
326
339
  * Tie-breaker: first encountered best similarity (deterministic).
327
340
  */
328
341
  declare function findBestRecord(record: Record<string, string>, searchTerm: string, similarityThreshold?: number): string | null;
342
+ declare function replaceUnknown(dice: string, replacer: string): string;
343
+ declare function verifyStatMatcherPattern(dice: string, replaceUnknow?: string): string;
329
344
 
330
345
  /**
331
346
  * Escape regex string
@@ -451,4 +466,4 @@ declare function testStatCombinaison(template: StatisticalTemplate, engine?: Eng
451
466
  */
452
467
  declare function generateRandomStat(total?: number | undefined, max?: number, min?: number, engine?: Engine | null): number;
453
468
 
454
- export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MaxGreater, type Modifier, NoStatisticsError, OPTIONAL_COMMENT, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, SortOrder, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculateSimilarity, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, findBestRecord, findBestStatMatch, generateRandomStat, generateStatsDice, getEngine, getEngineId, isNumber, levenshteinDistance, randomInt, replaceExpByRandom, replaceFormulaInDice, replaceInFormula, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyTemplateValue };
469
+ export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MIN_THRESHOLD_MATCH, MaxGreater, type Modifier, NORMALIZE_SINGLE_DICE, NoStatisticsError, OPTIONAL_COMMENT, REMOVER_PATTERN, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, SortOrder, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculateSimilarity, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, findBestRecord, findBestStatMatch, generateRandomStat, generateStatsDice, getCachedRegex, getEngine, getEngineId, includeDiceType, isNumber, levenshteinDistance, randomInt, replaceExpByRandom, replaceFormulaInDice, replaceInFormula, replaceUnknown, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyStatMatcherPattern, verifyTemplateValue };
package/dist/index.d.ts CHANGED
@@ -51,6 +51,14 @@ declare class NoStatisticsError extends Error {
51
51
  constructor();
52
52
  }
53
53
 
54
+ declare const COMMENT_REGEX: RegExp;
55
+ declare const SIGN_REGEX: RegExp;
56
+ declare const SIGN_REGEX_SPACE: RegExp;
57
+ declare const SYMBOL_DICE = "&";
58
+ declare const DETECT_CRITICAL: RegExp;
59
+ declare const OPTIONAL_COMMENT: RegExp;
60
+ declare const MIN_THRESHOLD_MATCH = 0.5;
61
+
54
62
  interface Resultat {
55
63
  /**
56
64
  * Original dice throw
@@ -237,13 +245,6 @@ declare enum SortOrder {
237
245
  None = "none"
238
246
  }
239
247
 
240
- declare const COMMENT_REGEX: RegExp;
241
- declare const SIGN_REGEX: RegExp;
242
- declare const SIGN_REGEX_SPACE: RegExp;
243
- declare const SYMBOL_DICE = "&";
244
- declare const DETECT_CRITICAL: RegExp;
245
- declare const OPTIONAL_COMMENT: RegExp;
246
-
247
248
  interface StatisticalSchema extends StatisticalTemplate {
248
249
  /**
249
250
  * Specifies the URL for the schema definition
@@ -293,6 +294,18 @@ declare const templateSchema: z.ZodObject<{
293
294
  damage: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
294
295
  }, z.core.$strip>;
295
296
 
297
+ declare const NORMALIZE_SINGLE_DICE: (str: string) => string;
298
+ declare const REMOVER_PATTERN: {
299
+ readonly ASTERISK_ESCAPE: RegExp;
300
+ readonly CRITICAL_BLOCK: RegExp;
301
+ readonly EXP_REMOVER: RegExp;
302
+ readonly SIGN_REMOVER: RegExp;
303
+ readonly STAT_COMMENTS_REMOVER: RegExp;
304
+ readonly STAT_MATCHER: RegExp;
305
+ };
306
+ declare function getCachedRegex(pattern: string, flags?: string): RegExp;
307
+ declare function includeDiceType(dice: string, diceType?: string, userStats?: boolean): boolean;
308
+
296
309
  /**
297
310
  * Parse the string provided and turn it as a readable dice for dice parser
298
311
  * @param {string} dice The dice string to parse and roll
@@ -326,6 +339,8 @@ declare function findBestStatMatch<T>(searchTerm: string, normalizedStats: Map<s
326
339
  * Tie-breaker: first encountered best similarity (deterministic).
327
340
  */
328
341
  declare function findBestRecord(record: Record<string, string>, searchTerm: string, similarityThreshold?: number): string | null;
342
+ declare function replaceUnknown(dice: string, replacer: string): string;
343
+ declare function verifyStatMatcherPattern(dice: string, replaceUnknow?: string): string;
329
344
 
330
345
  /**
331
346
  * Escape regex string
@@ -451,4 +466,4 @@ declare function testStatCombinaison(template: StatisticalTemplate, engine?: Eng
451
466
  */
452
467
  declare function generateRandomStat(total?: number | undefined, max?: number, min?: number, engine?: Engine | null): number;
453
468
 
454
- export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MaxGreater, type Modifier, NoStatisticsError, OPTIONAL_COMMENT, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, SortOrder, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculateSimilarity, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, findBestRecord, findBestStatMatch, generateRandomStat, generateStatsDice, getEngine, getEngineId, isNumber, levenshteinDistance, randomInt, replaceExpByRandom, replaceFormulaInDice, replaceInFormula, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyTemplateValue };
469
+ export { COMMENT_REGEX, type Compare, type ComparedValue, type Critical, type CustomCritical, type CustomCriticalMap, DETECT_CRITICAL, DiceTypeError, EmptyObjectError, FormulaError, MIN_THRESHOLD_MATCH, MaxGreater, type Modifier, NORMALIZE_SINGLE_DICE, NoStatisticsError, OPTIONAL_COMMENT, REMOVER_PATTERN, type Resultat, SIGN_REGEX, SIGN_REGEX_SPACE, SYMBOL_DICE, type Sign, SortOrder, type Statistic, type StatisticalSchema, type StatisticalTemplate, TooManyDice, TooManyStats, calculateSimilarity, createCriticalCustom, diceRandomParse, diceTypeRandomParse, escapeRegex, evalCombinaison, evalOneCombinaison, evalStatsDice, findBestRecord, findBestStatMatch, generateRandomStat, generateStatsDice, getCachedRegex, getEngine, getEngineId, includeDiceType, isNumber, levenshteinDistance, randomInt, replaceExpByRandom, replaceFormulaInDice, replaceInFormula, replaceUnknown, roll, standardizeDice, templateSchema, testDiceRegistered, testStatCombinaison, verifyStatMatcherPattern, verifyTemplateValue };
package/dist/index.js CHANGED
@@ -25,9 +25,12 @@ __export(index_exports, {
25
25
  DiceTypeError: () => DiceTypeError,
26
26
  EmptyObjectError: () => EmptyObjectError,
27
27
  FormulaError: () => FormulaError,
28
+ MIN_THRESHOLD_MATCH: () => MIN_THRESHOLD_MATCH,
28
29
  MaxGreater: () => MaxGreater,
30
+ NORMALIZE_SINGLE_DICE: () => NORMALIZE_SINGLE_DICE,
29
31
  NoStatisticsError: () => NoStatisticsError,
30
32
  OPTIONAL_COMMENT: () => OPTIONAL_COMMENT,
33
+ REMOVER_PATTERN: () => REMOVER_PATTERN,
31
34
  SIGN_REGEX: () => SIGN_REGEX,
32
35
  SIGN_REGEX_SPACE: () => SIGN_REGEX_SPACE,
33
36
  SYMBOL_DICE: () => SYMBOL_DICE,
@@ -46,19 +49,23 @@ __export(index_exports, {
46
49
  findBestStatMatch: () => findBestStatMatch,
47
50
  generateRandomStat: () => generateRandomStat,
48
51
  generateStatsDice: () => generateStatsDice,
52
+ getCachedRegex: () => getCachedRegex,
49
53
  getEngine: () => getEngine,
50
54
  getEngineId: () => getEngineId,
55
+ includeDiceType: () => includeDiceType,
51
56
  isNumber: () => isNumber,
52
57
  levenshteinDistance: () => levenshteinDistance,
53
58
  randomInt: () => randomInt,
54
59
  replaceExpByRandom: () => replaceExpByRandom,
55
60
  replaceFormulaInDice: () => replaceFormulaInDice,
56
61
  replaceInFormula: () => replaceInFormula,
62
+ replaceUnknown: () => replaceUnknown,
57
63
  roll: () => roll,
58
64
  standardizeDice: () => standardizeDice,
59
65
  templateSchema: () => templateSchema,
60
66
  testDiceRegistered: () => testDiceRegistered,
61
67
  testStatCombinaison: () => testStatCombinaison,
68
+ verifyStatMatcherPattern: () => verifyStatMatcherPattern,
62
69
  verifyTemplateValue: () => verifyTemplateValue
63
70
  });
64
71
  module.exports = __toCommonJS(index_exports);
@@ -157,14 +164,6 @@ var NoStatisticsError = class extends Error {
157
164
  }
158
165
  };
159
166
 
160
- // src/interfaces/index.ts
161
- var SortOrder = /* @__PURE__ */ ((SortOrder2) => {
162
- SortOrder2["Ascending"] = "sa";
163
- SortOrder2["Descending"] = "sd";
164
- SortOrder2["None"] = "none";
165
- return SortOrder2;
166
- })(SortOrder || {});
167
-
168
167
  // src/interfaces/constant.ts
169
168
  var COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(?<comment>.*)/gi;
170
169
  var SIGN_REGEX = /==|!=|(?<![!<>])>=|(?<![!<>])<=|(?<!!)(?<![<>])>|(?<!!)(?<![<>])<|(?<!!)(?<![<>])=/;
@@ -172,6 +171,15 @@ var SIGN_REGEX_SPACE = /(==|!=|(?<![!<>])>=|(?<![!<>])<=|(?<!!)(?<![<>])>|(?<!!)
172
171
  var SYMBOL_DICE = "&";
173
172
  var DETECT_CRITICAL = /\{\*?c[fs]:([<>=]|!=)+(.+?)}/gim;
174
173
  var OPTIONAL_COMMENT = /\s+(#|\/{2}|\[|\/\*)?(?<comment>.*)/gi;
174
+ var MIN_THRESHOLD_MATCH = 0.5;
175
+
176
+ // src/interfaces/index.ts
177
+ var SortOrder = /* @__PURE__ */ ((SortOrder2) => {
178
+ SortOrder2["Ascending"] = "sa";
179
+ SortOrder2["Descending"] = "sd";
180
+ SortOrder2["None"] = "none";
181
+ return SortOrder2;
182
+ })(SortOrder || {});
175
183
 
176
184
  // src/interfaces/zod.ts
177
185
  var import_zod = require("zod");
@@ -229,6 +237,45 @@ var templateSchema = import_zod.z.object({
229
237
  damage: damageSchema
230
238
  });
231
239
 
240
+ // src/regex.ts
241
+ var regexCache = /* @__PURE__ */ new Map();
242
+ var NORMALIZE_SINGLE_DICE = (str) => str.replace(/\b1d(\d+)/gi, "d$1");
243
+ var REMOVER_PATTERN = {
244
+ ASTERISK_ESCAPE: /\*/g,
245
+ CRITICAL_BLOCK: /\{\*?c[fs]:([<>=]|!=)+.+?\}/gim,
246
+ EXP_REMOVER: /\{exp(.*?)\}/g,
247
+ SIGN_REMOVER: /([><=]|!=)+.*$/,
248
+ STAT_COMMENTS_REMOVER: /%%.*%%/,
249
+ STAT_MATCHER: /\(?\$([\p{L}\p{M}_.][\p{L}\p{M}0-9_.]*)\)?/giu
250
+ };
251
+ function getCachedRegex(pattern, flags = "") {
252
+ const key = `${pattern}|${flags}`;
253
+ let regex = regexCache.get(key);
254
+ if (!regex) {
255
+ regex = new RegExp(pattern, flags);
256
+ regexCache.set(key, regex);
257
+ }
258
+ return regex;
259
+ }
260
+ function includeDiceType(dice, diceType, userStats) {
261
+ if (!diceType) return false;
262
+ diceType = NORMALIZE_SINGLE_DICE(diceType);
263
+ dice = NORMALIZE_SINGLE_DICE(dice);
264
+ if (userStats && diceType.includes("$")) {
265
+ diceType = diceType.replace("$", ".+?");
266
+ }
267
+ if (SIGN_REGEX.test(diceType)) {
268
+ diceType = diceType.replace(REMOVER_PATTERN.SIGN_REMOVER, "").trim();
269
+ dice = dice.replace(REMOVER_PATTERN.SIGN_REMOVER, "").trim();
270
+ }
271
+ if (diceType.includes("{exp")) {
272
+ diceType = diceType.replace(REMOVER_PATTERN.EXP_REMOVER, "").trim();
273
+ dice = dice.replace(REMOVER_PATTERN.EXP_REMOVER, "").trim();
274
+ }
275
+ const detectDiceType = getCachedRegex(`\\b${diceType}\\b`, "i");
276
+ return detectDiceType.test(dice);
277
+ }
278
+
232
279
  // src/roll.ts
233
280
  var import_rpg_dice_roller7 = require("@dice-roller/rpg-dice-roller");
234
281
  var import_mathjs8 = require("mathjs");
@@ -248,7 +295,6 @@ var import_rpg_dice_roller2 = require("@dice-roller/rpg-dice-roller");
248
295
  var import_random_js = require("random-js");
249
296
 
250
297
  // src/similarity.ts
251
- var MIN_THRESHOLD_MATCH = 0.5;
252
298
  function calculateSimilarity(str1, str2) {
253
299
  const longer = str1.length > str2.length ? str1 : str2;
254
300
  const shorter = str1.length > str2.length ? str2 : str1;
@@ -311,6 +357,19 @@ function findBestRecord(record, searchTerm, similarityThreshold = MIN_THRESHOLD_
311
357
  similarityThreshold
312
358
  ) || null;
313
359
  }
360
+ function replaceUnknown(dice, replacer) {
361
+ return dice.replaceAll(REMOVER_PATTERN.STAT_MATCHER, replacer).replaceAll("+0", "").replaceAll("-0", "");
362
+ }
363
+ function verifyStatMatcherPattern(dice, replaceUnknow) {
364
+ if (REMOVER_PATTERN.STAT_MATCHER.test(dice)) {
365
+ if (replaceUnknow)
366
+ return replaceUnknown(dice, replaceUnknow);
367
+ const matched = dice.matchAll(new RegExp(REMOVER_PATTERN.STAT_MATCHER));
368
+ const stats = matched ? Array.from(matched, (m) => m?.[0]).map((s) => `\`${s}\``).join(", ") : "unknown";
369
+ throw new DiceTypeError("error.invalidDice.stats");
370
+ }
371
+ return dice.replaceAll("+0", "").replaceAll("-0", "");
372
+ }
314
373
 
315
374
  // src/utils.ts
316
375
  function escapeRegex(string) {
@@ -1532,9 +1591,12 @@ function generateRandomStat(total = 100, max, min, engine = import_rpg_dice_roll
1532
1591
  DiceTypeError,
1533
1592
  EmptyObjectError,
1534
1593
  FormulaError,
1594
+ MIN_THRESHOLD_MATCH,
1535
1595
  MaxGreater,
1596
+ NORMALIZE_SINGLE_DICE,
1536
1597
  NoStatisticsError,
1537
1598
  OPTIONAL_COMMENT,
1599
+ REMOVER_PATTERN,
1538
1600
  SIGN_REGEX,
1539
1601
  SIGN_REGEX_SPACE,
1540
1602
  SYMBOL_DICE,
@@ -1553,19 +1615,23 @@ function generateRandomStat(total = 100, max, min, engine = import_rpg_dice_roll
1553
1615
  findBestStatMatch,
1554
1616
  generateRandomStat,
1555
1617
  generateStatsDice,
1618
+ getCachedRegex,
1556
1619
  getEngine,
1557
1620
  getEngineId,
1621
+ includeDiceType,
1558
1622
  isNumber,
1559
1623
  levenshteinDistance,
1560
1624
  randomInt,
1561
1625
  replaceExpByRandom,
1562
1626
  replaceFormulaInDice,
1563
1627
  replaceInFormula,
1628
+ replaceUnknown,
1564
1629
  roll,
1565
1630
  standardizeDice,
1566
1631
  templateSchema,
1567
1632
  testDiceRegistered,
1568
1633
  testStatCombinaison,
1634
+ verifyStatMatcherPattern,
1569
1635
  verifyTemplateValue
1570
1636
  });
1571
1637
  //# sourceMappingURL=index.js.map