@dicelette/core 1.12.2 → 1.12.3

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,77 +1,57 @@
1
1
  // src/dice.ts
2
2
  import { DiceRoller } from "@dice-roller/rpg-dice-roller";
3
- import { evaluate } from "mathjs";
3
+ import { evaluate as evaluate2 } from "mathjs";
4
4
 
5
- // src/errors.ts
6
- var DiceTypeError = class extends Error {
7
- dice;
8
- cause;
9
- method;
10
- constructor(dice, cause, method) {
11
- super(dice);
12
- this.name = "Invalid_Dice_Type";
13
- this.dice = dice;
14
- this.cause = cause;
15
- this.method = method;
16
- }
17
- };
18
- var FormulaError = class extends Error {
19
- formula;
20
- cause;
21
- method;
22
- constructor(formula, cause, method) {
23
- super(formula);
24
- this.name = "Invalid_Formula";
25
- this.formula = formula;
26
- this.cause = cause;
27
- this.method = method;
28
- }
29
- };
30
- var MaxGreater = class extends Error {
31
- name;
32
- value;
33
- max;
34
- constructor(value, max) {
35
- super(value.toString());
36
- this.name = "Max_Greater";
37
- this.value = value;
38
- this.max = max;
39
- }
40
- };
41
- var EmptyObjectError = class extends Error {
42
- name;
43
- constructor() {
44
- super();
45
- this.name = "Empty_Object";
46
- }
47
- };
48
- var TooManyDice = class extends Error {
49
- name;
50
- constructor() {
51
- super();
52
- this.name = "Too_Many_Dice";
53
- }
54
- };
55
- var TooManyStats = class extends Error {
56
- name;
57
- constructor() {
58
- super();
59
- this.name = "Too_Many_Stats";
5
+ // src/utils.ts
6
+ import { evaluate } from "mathjs";
7
+ import "uniformize";
8
+ function escapeRegex(string) {
9
+ return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
10
+ }
11
+ function standardizeDice(dice) {
12
+ return dice.replace(
13
+ /(\[[^\]]+\])|([^[]+)/g,
14
+ (match, insideBrackets, outsideText) => insideBrackets ? insideBrackets : outsideText.standardize()
15
+ );
16
+ }
17
+ function generateStatsDice(originalDice, stats, dollarValue) {
18
+ let dice = originalDice.standardize();
19
+ if (stats && Object.keys(stats).length > 0) {
20
+ const allStats = Object.keys(stats);
21
+ for (const stat of allStats) {
22
+ const regex = new RegExp(`(?!\\[)${escapeRegex(stat.standardize())}(?!\\])`, "gi");
23
+ if (dice.match(regex)) {
24
+ const statValue = stats[stat];
25
+ dice = dice.replace(regex, statValue.toString());
26
+ }
27
+ }
60
28
  }
61
- };
62
- var NoStatisticsError = class extends Error {
63
- name;
64
- constructor() {
65
- super();
66
- this.name = "No_Statistics";
29
+ if (dollarValue) dice = dice.replaceAll("$", dollarValue);
30
+ return replaceFormulaInDice(dice);
31
+ }
32
+ function replaceFormulaInDice(dice) {
33
+ const formula = /(?<formula>\{{2}(.+?)}{2})/gim;
34
+ let match;
35
+ let modifiedDice = dice;
36
+ while ((match = formula.exec(dice)) !== null) {
37
+ if (match.groups?.formula) {
38
+ const formulae = match.groups.formula.replaceAll("{{", "").replaceAll("}}", "");
39
+ try {
40
+ const result = evaluate(formulae);
41
+ modifiedDice = modifiedDice.replace(match.groups.formula, result.toString());
42
+ } catch (error) {
43
+ throw new FormulaError(match.groups.formula, "replaceFormulasInDice", error);
44
+ }
45
+ }
67
46
  }
68
- };
69
-
70
- // src/interfaces/constant.ts
71
- var COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(?<comment>.*)/;
72
- var SIGN_REGEX = /[><=!]+/;
73
- var SIGN_REGEX_SPACE = /[><=!]+(\S+)/;
74
- var SYMBOL_DICE = "&";
47
+ return cleanedDice(modifiedDice);
48
+ }
49
+ function cleanedDice(dice) {
50
+ return dice.replaceAll("+-", "-").replaceAll("--", "+").replaceAll("++", "+").trimEnd();
51
+ }
52
+ function isNumber(value) {
53
+ return value !== void 0 && (typeof value === "number" || !Number.isNaN(Number(value)) && typeof value === "string" && value.trim().length > 0);
54
+ }
75
55
 
76
56
  // src/dice.ts
77
57
  function getCompare(dice, compareRegex) {
@@ -83,7 +63,7 @@ function getCompare(dice, compareRegex) {
83
63
  if (sign) {
84
64
  const toCalc = calc.replace(SIGN_REGEX, "").replace(/\s/g, "").replace(/;(.*)/, "");
85
65
  const rCompare = rollCompare(toCalc);
86
- const total = evaluate(rCompare.value.toString());
66
+ const total = evaluate2(rCompare.value.toString());
87
67
  dice = dice.replace(SIGN_REGEX_SPACE, `${compareSign}${total}`);
88
68
  compare = {
89
69
  sign: compareSign,
@@ -103,11 +83,10 @@ function getCompare(dice, compareRegex) {
103
83
  return { dice, compare };
104
84
  }
105
85
  function rollCompare(value) {
106
- const isNumber = (value2) => typeof value2 === "number" || !Number.isNaN(Number(value2)) && typeof value2 === "string";
107
86
  if (isNumber(value)) return { value: Number.parseInt(value, 10) };
108
87
  const rollComp = roll(value);
109
88
  if (!rollComp?.total)
110
- return { value: evaluate(value), diceResult: value };
89
+ return { value: evaluate2(value), diceResult: value };
111
90
  return {
112
91
  dice: value,
113
92
  value: rollComp.total,
@@ -201,7 +180,7 @@ function roll(dice) {
201
180
  }
202
181
  function calculator(sign, value, total) {
203
182
  if (sign === "^") sign = "**";
204
- return evaluate(`${total} ${sign} ${value}`);
183
+ return evaluate2(`${total} ${sign} ${value}`);
205
184
  }
206
185
  function inverseSign(sign) {
207
186
  switch (sign) {
@@ -231,7 +210,7 @@ function replaceInFormula(element, diceResult, compareResult, res) {
231
210
  const invertedSign = res ? compareResult.compare.sign : inverseSign(compareResult.compare.sign);
232
211
  let evaluateRoll;
233
212
  try {
234
- evaluateRoll = evaluate(compareResult.dice);
213
+ evaluateRoll = evaluate2(compareResult.dice);
235
214
  return `${validSign} ${diceAll}: ${formule} = ${evaluateRoll}${invertedSign}${compareResult.compare?.value}`;
236
215
  } catch (error) {
237
216
  const evaluateRoll2 = roll(compareResult.dice);
@@ -246,7 +225,7 @@ function compareSignFormule(toRoll, compareRegex, element, diceResult) {
246
225
  const toCompare = `${compareResult.dice}${compareResult.compare?.sign}${compareResult.compare?.value}`;
247
226
  let res;
248
227
  try {
249
- res = evaluate(toCompare);
228
+ res = evaluate2(toCompare);
250
229
  } catch (error) {
251
230
  res = roll(toCompare);
252
231
  }
@@ -255,7 +234,7 @@ function compareSignFormule(toRoll, compareRegex, element, diceResult) {
255
234
  } else if (res instanceof Object) {
256
235
  const diceResult2 = res;
257
236
  if (diceResult2.compare) {
258
- const toEvaluate = evaluate(
237
+ const toEvaluate = evaluate2(
259
238
  `${diceResult2.total}${diceResult2.compare.sign}${diceResult2.compare.value}`
260
239
  );
261
240
  const sign = toEvaluate ? "\u2713" : "\u2715";
@@ -323,7 +302,7 @@ function sharedRolls(dice) {
323
302
  diceResult.dice
324
303
  );
325
304
  try {
326
- const evaluated = evaluate(toRoll);
305
+ const evaluated = evaluate2(toRoll);
327
306
  results.push(`\u25C8 ${comment}${diceAll}: ${formule} = ${evaluated}`);
328
307
  total += Number.parseInt(evaluated, 10);
329
308
  } catch (error) {
@@ -349,114 +328,10 @@ function sharedRolls(dice) {
349
328
  };
350
329
  }
351
330
 
352
- // src/utils.ts
353
- import { evaluate as evaluate2 } from "mathjs";
354
- import "uniformize";
355
- function escapeRegex(string) {
356
- return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
357
- }
358
- function standardizeDice(dice) {
359
- return dice.replace(
360
- /(\[[^\]]+\])|([^[]+)/g,
361
- (match, insideBrackets, outsideText) => insideBrackets ? insideBrackets : outsideText.standardize()
362
- );
363
- }
364
- function generateStatsDice(originalDice, stats, dollarValue) {
365
- let dice = originalDice.standardize();
366
- if (stats && Object.keys(stats).length > 0) {
367
- const allStats = Object.keys(stats);
368
- for (const stat of allStats) {
369
- const regex = new RegExp(`(?!\\[)${escapeRegex(stat.standardize())}(?!\\])`, "gi");
370
- if (dice.match(regex)) {
371
- const statValue = stats[stat];
372
- dice = dice.replace(regex, statValue.toString());
373
- }
374
- }
375
- }
376
- if (dollarValue) dice = dice.replaceAll("$", dollarValue);
377
- return replaceFormulaInDice(dice);
378
- }
379
- function replaceFormulaInDice(dice) {
380
- const regExp = /(?<formula>\{{2}(.+?)}{2})/gim;
381
- let match;
382
- let modifiedDice = dice;
383
- while ((match = regExp.exec(dice)) !== null) {
384
- if (match.groups?.formula) {
385
- const formula = match.groups.formula.replaceAll("{{", "").replaceAll("}}", "");
386
- try {
387
- const result = evaluate2(formula);
388
- if (result instanceof Object || typeof result === "function") continue;
389
- modifiedDice = modifiedDice.replace(match.groups.formula, result.toString());
390
- } catch (error) {
391
- throw new FormulaError(match.groups.formula, "replaceFormulasInDice", error);
392
- }
393
- }
394
- }
395
- return cleanedDice(modifiedDice);
396
- }
397
- function cleanedDice(dice) {
398
- return dice.replaceAll("+-", "-").replaceAll("--", "+").replaceAll("++", "+").trimEnd();
399
- }
400
-
401
331
  // src/verify_template.ts
402
332
  import { evaluate as evaluate3 } from "mathjs";
403
333
  import { Random } from "random-js";
404
334
  import "uniformize";
405
-
406
- // src/interfaces/zod.ts
407
- import { z } from "zod";
408
- var statisticValueSchema = z.object({
409
- max: z.number().positive().optional(),
410
- min: z.number().positive().optional(),
411
- combinaison: z.string().transform((str) => str.trim() || void 0).optional(),
412
- exclude: z.boolean().optional()
413
- }).superRefine((data, ctx) => {
414
- if (data.max !== void 0 && data.min !== void 0 && data.max <= data.min) {
415
- ctx.addIssue({
416
- code: "custom",
417
- message: `Max_Greater; ${data.min}; ${data.max}`,
418
- path: ["max"]
419
- });
420
- }
421
- });
422
- var statisticSchema = z.record(statisticValueSchema).optional().refine((stats) => !stats || Object.keys(stats).length <= 25, {
423
- message: "TooManyStats"
424
- });
425
- var criticalSchema = z.object({
426
- success: z.string().or(z.number().positive()).optional(),
427
- failure: z.string().or(z.number().positive()).optional()
428
- }).transform((values) => {
429
- if (values.success === "") values.success = void 0;
430
- if (values.failure === "") values.failure = void 0;
431
- if (values.failure === 0) values.failure = void 0;
432
- if (values.success === 0) values.success = void 0;
433
- values.success = Number.parseInt(values.success, 10);
434
- values.failure = Number.parseInt(values.failure, 10);
435
- return values;
436
- });
437
- var criticalValueSchema = z.object({
438
- sign: z.enum(["<", ">", "<=", ">=", "!=", "=="]),
439
- value: z.string(),
440
- onNaturalDice: z.boolean().optional(),
441
- affectSkill: z.boolean().optional()
442
- });
443
- var damageSchema = z.record(z.string()).optional().refine((stats) => !stats || Object.keys(stats).length <= 25, {
444
- message: "TooManyDice"
445
- });
446
- var customCriticalSchema = z.record(criticalValueSchema).optional().refine((stats) => !stats || Object.keys(stats).length <= 22, {
447
- message: "TooManyDice"
448
- });
449
- var templateSchema = z.object({
450
- charName: z.boolean().optional(),
451
- statistics: statisticSchema,
452
- total: z.number().min(0).optional(),
453
- diceType: z.string().optional(),
454
- critical: criticalSchema.optional(),
455
- customCritical: customCriticalSchema,
456
- damage: damageSchema
457
- });
458
-
459
- // src/verify_template.ts
460
335
  function evalStatsDice(testDice, allStats) {
461
336
  let dice = testDice.trimEnd();
462
337
  if (allStats && Object.keys(allStats).length > 0) {
@@ -541,7 +416,6 @@ function evalOneCombinaison(combinaison, stats) {
541
416
  }
542
417
  function convertNumber(number) {
543
418
  if (!number) return void 0;
544
- const isNumber = (value) => typeof value === "number" || !Number.isNaN(Number(value)) && typeof value === "string";
545
419
  if (number.toString().length === 0) return void 0;
546
420
  if (isNumber(number)) return Number.parseInt(number.toString(), 10);
547
421
  return void 0;
@@ -653,6 +527,130 @@ function generateRandomStat(total = 100, max, min) {
653
527
  }
654
528
  return randomStatValue;
655
529
  }
530
+
531
+ // src/errors.ts
532
+ var DiceTypeError = class extends Error {
533
+ dice;
534
+ cause;
535
+ method;
536
+ constructor(dice, cause, method) {
537
+ super(dice);
538
+ this.name = "Invalid_Dice_Type";
539
+ this.dice = dice;
540
+ this.cause = cause;
541
+ this.method = method;
542
+ }
543
+ };
544
+ var FormulaError = class extends Error {
545
+ formula;
546
+ cause;
547
+ method;
548
+ constructor(formula, cause, method) {
549
+ super(formula);
550
+ this.name = "Invalid_Formula";
551
+ this.formula = formula;
552
+ this.cause = cause;
553
+ this.method = method;
554
+ }
555
+ };
556
+ var MaxGreater = class extends Error {
557
+ name;
558
+ value;
559
+ max;
560
+ constructor(value, max) {
561
+ super(value.toString());
562
+ this.name = "Max_Greater";
563
+ this.value = value;
564
+ this.max = max;
565
+ }
566
+ };
567
+ var EmptyObjectError = class extends Error {
568
+ name;
569
+ constructor() {
570
+ super();
571
+ this.name = "Empty_Object";
572
+ }
573
+ };
574
+ var TooManyDice = class extends Error {
575
+ name;
576
+ constructor() {
577
+ super();
578
+ this.name = "Too_Many_Dice";
579
+ }
580
+ };
581
+ var TooManyStats = class extends Error {
582
+ name;
583
+ constructor() {
584
+ super();
585
+ this.name = "Too_Many_Stats";
586
+ }
587
+ };
588
+ var NoStatisticsError = class extends Error {
589
+ name;
590
+ constructor() {
591
+ super();
592
+ this.name = "No_Statistics";
593
+ }
594
+ };
595
+
596
+ // src/interfaces/constant.ts
597
+ var COMMENT_REGEX = /\s+(#|\/{2}|\[|\/\*)(?<comment>.*)/;
598
+ var SIGN_REGEX = /[><=!]+/;
599
+ var SIGN_REGEX_SPACE = /[><=!]+(\S+)/;
600
+ var SYMBOL_DICE = "&";
601
+
602
+ // src/interfaces/zod.ts
603
+ import { z } from "zod";
604
+ var statisticValueSchema = z.object({
605
+ max: z.number().positive().optional(),
606
+ min: z.number().positive().optional(),
607
+ combinaison: z.string().transform((str) => str.trim() || void 0).optional(),
608
+ exclude: z.boolean().optional()
609
+ }).superRefine((data, ctx) => {
610
+ if (data.max !== void 0 && data.min !== void 0 && data.max <= data.min) {
611
+ ctx.addIssue({
612
+ code: "custom",
613
+ message: `Max_Greater; ${data.min}; ${data.max}`,
614
+ path: ["max"]
615
+ });
616
+ }
617
+ });
618
+ var statisticSchema = z.record(statisticValueSchema).optional().refine((stats) => !stats || Object.keys(stats).length <= 25, {
619
+ message: "TooManyStats"
620
+ });
621
+ var criticalSchema = z.object({
622
+ success: z.string().or(z.number().positive()).optional(),
623
+ failure: z.string().or(z.number().positive()).optional()
624
+ }).transform((values) => {
625
+ if (values.success === "") values.success = void 0;
626
+ if (values.failure === "") values.failure = void 0;
627
+ if (values.failure === 0) values.failure = void 0;
628
+ if (values.success === 0) values.success = void 0;
629
+ values.success = Number.parseInt(values.success, 10);
630
+ values.failure = Number.parseInt(values.failure, 10);
631
+ return values;
632
+ });
633
+ var criticalValueSchema = z.object({
634
+ sign: z.enum(["<", ">", "<=", ">=", "!=", "=="]),
635
+ value: z.string(),
636
+ onNaturalDice: z.boolean().optional(),
637
+ affectSkill: z.boolean().optional()
638
+ });
639
+ var damageSchema = z.record(z.string()).optional().refine((stats) => !stats || Object.keys(stats).length <= 25, {
640
+ message: "TooManyDice"
641
+ });
642
+ var customCriticalSchema = z.record(criticalValueSchema).optional().refine((stats) => !stats || Object.keys(stats).length <= 22, {
643
+ message: "TooManyDice"
644
+ });
645
+ var templateSchema = z.object({
646
+ charName: z.boolean().optional(),
647
+ statistics: statisticSchema,
648
+ total: z.number().min(0).optional(),
649
+ diceType: z.string().optional(),
650
+ critical: criticalSchema.optional(),
651
+ customCritical: customCriticalSchema,
652
+ damage: damageSchema
653
+ });
656
654
  export {
657
655
  COMMENT_REGEX,
658
656
  DiceTypeError,
@@ -666,7 +664,6 @@ export {
666
664
  TooManyDice,
667
665
  TooManyStats,
668
666
  calculator,
669
- cleanedDice,
670
667
  createCriticalCustom,
671
668
  diceRandomParse,
672
669
  diceTypeRandomParse,
@@ -676,6 +673,7 @@ export {
676
673
  evalStatsDice,
677
674
  generateRandomStat,
678
675
  generateStatsDice,
676
+ isNumber,
679
677
  replaceFormulaInDice,
680
678
  roll,
681
679
  standardizeDice,