@dicelette/core 1.24.1 → 1.25.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.js CHANGED
@@ -154,11 +154,11 @@ var NoStatisticsError = class extends Error {
154
154
  };
155
155
 
156
156
  // src/interfaces/index.ts
157
- var SortOrder = /* @__PURE__ */ ((SortOrder2) => {
158
- SortOrder2["Ascending"] = "sa";
159
- SortOrder2["Descending"] = "sd";
160
- SortOrder2["None"] = "none";
161
- return SortOrder2;
157
+ var SortOrder = /* @__PURE__ */ ((SortOrder3) => {
158
+ SortOrder3["Ascending"] = "sa";
159
+ SortOrder3["Descending"] = "sd";
160
+ SortOrder3["None"] = "none";
161
+ return SortOrder3;
162
162
  })(SortOrder || {});
163
163
 
164
164
  // src/interfaces/constant.ts
@@ -268,7 +268,7 @@ function generateStatsDice(originalDice, stats, dollarValue) {
268
268
  if (!outsideText) {
269
269
  continue;
270
270
  }
271
- const tokenRegex = /([\p{L}\p{N}_]+)/gu;
271
+ const tokenRegex = /(\$?[\p{L}\p{N}_]+)/gu;
272
272
  let lastIndex = 0;
273
273
  let tokenMatch;
274
274
  while ((tokenMatch = tokenRegex.exec(outsideText)) !== null) {
@@ -320,7 +320,7 @@ function generateStatsDice(originalDice, stats, dollarValue) {
320
320
  }
321
321
  dice = result;
322
322
  }
323
- if (dollarValue) dice = dice.replaceAll("$", dollarValue);
323
+ if (dollarValue) dice = dice.replaceAll(/\$\B/g, dollarValue);
324
324
  return replaceFormulaInDice(dice);
325
325
  }
326
326
  function replaceFormulaInDice(dice) {
@@ -506,8 +506,28 @@ var import_rpg_dice_roller4 = require("@dice-roller/rpg-dice-roller");
506
506
  var import_mathjs3 = require("mathjs");
507
507
 
508
508
  // src/dice/replace.ts
509
- function replaceUnwantedText(dice) {
510
- return dice.replaceAll(/[{}]/g, "").replaceAll(/s[ad]/gi, "");
509
+ function replaceUnwantedText(dice, sortOrder) {
510
+ const d = dice.replaceAll(/[{}]/g, "").replaceAll(/s[ad]/gi, "");
511
+ if (sortOrder) return sortDice(d, sortOrder);
512
+ return d;
513
+ }
514
+ function sortDice(dice, sortOrder) {
515
+ if (sortOrder === "none" /* None */) return dice;
516
+ const dices = dice.split(/; ?/);
517
+ if (sortOrder === "sa" /* Ascending */) {
518
+ dices.sort((a, b) => {
519
+ const totalA = Number.parseInt(a.split("= ")[1], 10) || 0;
520
+ const totalB = Number.parseInt(b.split("= ")[1], 10) || 0;
521
+ return totalB - totalA;
522
+ });
523
+ } else if (sortOrder === "sd" /* Descending */) {
524
+ dices.sort((a, b) => {
525
+ const totalA = Number.parseInt(a.split("= ")[1], 10) || 0;
526
+ const totalB = Number.parseInt(b.split("= ")[1], 10) || 0;
527
+ return totalA - totalB;
528
+ });
529
+ }
530
+ return dices.join("; ");
511
531
  }
512
532
  function fixParenthesis(dice) {
513
533
  const parenthesisRegex = /d\((\d+)\)/g;
@@ -773,6 +793,11 @@ function prepareDice(diceInput) {
773
793
  isSimpleCurly
774
794
  };
775
795
  }
796
+ function getSortOrder(dice) {
797
+ if (dice.startsWith("sa") || dice.endsWith("sa")) return "sa" /* Ascending */;
798
+ if (dice.startsWith("sd") || dice.endsWith("sd")) return "sd" /* Descending */;
799
+ return "none" /* None */;
800
+ }
776
801
 
777
802
  // src/dice/bulk.ts
778
803
  function handleBulkRolls(dice, isCurlyBulk, bulkContent, compare, explodingSuccess, diceDisplay, engine, sort) {
@@ -797,6 +822,7 @@ function handleBulkRolls(dice, isCurlyBulk, bulkContent, compare, explodingSucce
797
822
  }
798
823
  }
799
824
  }
825
+ sort = sort ?? getSortOrder(diceToRoll);
800
826
  diceToRoll = setSortOrder(diceToRoll, sort);
801
827
  const activeCompare = compare || curlyCompare || (explodingSuccess ? { sign: explodingSuccess.sign, value: explodingSuccess.value } : void 0);
802
828
  if (activeCompare) {
@@ -810,7 +836,8 @@ function handleBulkRolls(dice, isCurlyBulk, bulkContent, compare, explodingSucce
810
836
  isCurlyBulk,
811
837
  curlyCompare,
812
838
  compare,
813
- engine
839
+ engine,
840
+ sort
814
841
  );
815
842
  }
816
843
  const roller = new import_rpg_dice_roller6.DiceRoller();
@@ -826,14 +853,14 @@ function handleBulkRolls(dice, isCurlyBulk, bulkContent, compare, explodingSucce
826
853
  const modificator = getModifier(dice);
827
854
  return {
828
855
  dice: finalDice,
829
- result: replaceUnwantedText(roller.output),
856
+ result: replaceUnwantedText(roller.output, sort),
830
857
  comment: comments,
831
858
  compare: compare ? compare : void 0,
832
859
  modifier: modificator,
833
860
  total: roller.total
834
861
  };
835
862
  }
836
- function handleBulkRollsWithComparison(numberOfDice, diceToRoll, comments, activeCompare, explodingSuccess, diceDisplay, isCurlyBulk, curlyCompare, compare, engine) {
863
+ function handleBulkRollsWithComparison(numberOfDice, diceToRoll, comments, activeCompare, explodingSuccess, diceDisplay, isCurlyBulk, curlyCompare, compare, engine, sort) {
837
864
  const results = [];
838
865
  let successCount = 0;
839
866
  const roller = new import_rpg_dice_roller6.DiceRoller();
@@ -917,7 +944,7 @@ function handleBulkRollsWithComparison(numberOfDice, diceToRoll, comments, activ
917
944
  }
918
945
  if (compare && trivialComparisonDetected) compare.trivial = true;
919
946
  const finalDice = isCurlyBulk ? `{${diceToRoll}${curlyCompare?.sign}${curlyCompare?.value}}` : diceToRoll;
920
- const resultOutput = replaceUnwantedText(results.join("; "));
947
+ const resultOutput = replaceUnwantedText(results.join("; "), sort);
921
948
  const finalTotal = explodingSuccess ? resultOutput.split(";").flatMap((segment) => extractValuesFromOutput(segment)).filter(
922
949
  (val) => matchComparison(explodingSuccess.sign, val, explodingSuccess.value)
923
950
  ).length : successCount;
@@ -975,7 +1002,8 @@ function roll(dice, engine = import_rpg_dice_roller7.NumberGenerator.engines.nod
975
1002
  pity,
976
1003
  prepared.explodingSuccess,
977
1004
  prepared.diceDisplay,
978
- prepared.isSharedCurly
1005
+ prepared.isSharedCurly,
1006
+ sort
979
1007
  );
980
1008
  }
981
1009
  let processedDice = fixParenthesis(prepared.dice);
@@ -1049,7 +1077,7 @@ function roll(dice, engine = import_rpg_dice_roller7.NumberGenerator.engines.nod
1049
1077
  };
1050
1078
  }
1051
1079
  }
1052
- let resultOutput = replaceUnwantedText(roller.output);
1080
+ let resultOutput = replaceUnwantedText(roller.output, sort);
1053
1081
  if (prepared.explodingSuccess) {
1054
1082
  const successes = countExplodingSuccesses(
1055
1083
  diceRoll,
@@ -1082,7 +1110,7 @@ function roll(dice, engine = import_rpg_dice_roller7.NumberGenerator.engines.nod
1082
1110
  trivial: compare?.trivial ? true : void 0
1083
1111
  };
1084
1112
  }
1085
- function sharedRolls(dice, engine = import_rpg_dice_roller7.NumberGenerator.engines.nodeCrypto, pity, explodingSuccessMain, diceDisplay, isSharedCurly) {
1113
+ function sharedRolls(dice, engine = import_rpg_dice_roller7.NumberGenerator.engines.nodeCrypto, pity, explodingSuccessMain, diceDisplay, isSharedCurly, sort) {
1086
1114
  if (!explodingSuccessMain)
1087
1115
  explodingSuccessMain = normalizeExplodingSuccess(dice.split(";")[0] ?? dice);
1088
1116
  if (explodingSuccessMain) {
@@ -1114,11 +1142,12 @@ function sharedRolls(dice, engine = import_rpg_dice_roller7.NumberGenerator.engi
1114
1142
  } else {
1115
1143
  diceMain = diceMainWithoutComments;
1116
1144
  }
1145
+ const sortFromMain = getSortOrder(diceMain);
1117
1146
  const rollBounds = getRollBounds(diceMain, engine);
1118
- let diceResult = roll(diceMain, engine, pity);
1147
+ let diceResult = roll(diceMain, engine, pity, sort);
1119
1148
  if (!diceResult || !diceResult.total) {
1120
1149
  if (hidden) {
1121
- diceResult = roll(fixParenthesis(split[0]), engine, pity);
1150
+ diceResult = roll(fixParenthesis(split[0]), engine, pity, sort);
1122
1151
  hidden = false;
1123
1152
  } else return void 0;
1124
1153
  }
@@ -1137,7 +1166,7 @@ function sharedRolls(dice, engine = import_rpg_dice_roller7.NumberGenerator.engi
1137
1166
  if (!total) {
1138
1167
  return {
1139
1168
  dice: displayDice,
1140
- result: results.join(";"),
1169
+ result: replaceUnwantedText(results.join(";"), sortFromMain),
1141
1170
  comment: mainComment,
1142
1171
  compare: aggregatedCompare,
1143
1172
  modifier: diceResult.modifier,
@@ -1219,7 +1248,7 @@ function sharedRolls(dice, engine = import_rpg_dice_roller7.NumberGenerator.engi
1219
1248
  results.shift();
1220
1249
  return {
1221
1250
  dice: displayDice,
1222
- result: results.join(";"),
1251
+ result: replaceUnwantedText(results.join(";"), sortFromMain),
1223
1252
  comment: mainComment,
1224
1253
  compare: hasTrivialComparison && aggregatedCompare ? { ...aggregatedCompare, trivial: true } : aggregatedCompare,
1225
1254
  modifier: diceResult.modifier,