@dicelette/core 1.26.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 +24 -9
- package/dist/index.d.ts +24 -9
- package/dist/index.js +88 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +81 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +59 -59
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;
|
|
@@ -210,23 +249,18 @@ function levenshteinDistance(str1, str2) {
|
|
|
210
249
|
}
|
|
211
250
|
return matrix[str2.length][str1.length];
|
|
212
251
|
}
|
|
213
|
-
function findBestStatMatch(searchTerm, normalizedStats, similarityThreshold = MIN_THRESHOLD_MATCH
|
|
252
|
+
function findBestStatMatch(searchTerm, normalizedStats, similarityThreshold = MIN_THRESHOLD_MATCH) {
|
|
214
253
|
const exact = normalizedStats.get(searchTerm);
|
|
215
254
|
if (exact) return exact;
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
if (candidates.length > 0) {
|
|
227
|
-
candidates.sort((a, b) => a[1] - b[1]);
|
|
228
|
-
return candidates[0][0];
|
|
229
|
-
}
|
|
255
|
+
const candidates = [];
|
|
256
|
+
for (const [normalizedKey, original] of normalizedStats) {
|
|
257
|
+
if (normalizedKey.startsWith(searchTerm))
|
|
258
|
+
candidates.push([original, calculateSimilarity(searchTerm, normalizedKey)]);
|
|
259
|
+
}
|
|
260
|
+
if (candidates.length === 1) return candidates[0][0];
|
|
261
|
+
if (candidates.length > 0) {
|
|
262
|
+
candidates.sort((a, b) => b[1] - a[1]);
|
|
263
|
+
if (candidates[0][1] >= similarityThreshold) return candidates[0][0];
|
|
230
264
|
}
|
|
231
265
|
let bestMatch;
|
|
232
266
|
let bestSimilarity = 0;
|
|
@@ -248,10 +282,22 @@ function findBestRecord(record, searchTerm, similarityThreshold = MIN_THRESHOLD_
|
|
|
248
282
|
return findBestStatMatch(
|
|
249
283
|
searchTerm.standardize(),
|
|
250
284
|
normalizeRecord,
|
|
251
|
-
similarityThreshold
|
|
252
|
-
false
|
|
285
|
+
similarityThreshold
|
|
253
286
|
) || null;
|
|
254
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
|
+
}
|
|
255
301
|
|
|
256
302
|
// src/utils.ts
|
|
257
303
|
function escapeRegex(string) {
|
|
@@ -268,7 +314,7 @@ function handleDiceAfterD(tokenStd, normalizedStats) {
|
|
|
268
314
|
if (!diceMatch) return null;
|
|
269
315
|
const diceCount = diceMatch[1] || "";
|
|
270
316
|
const afterD = diceMatch[2];
|
|
271
|
-
const bestMatch = findBestStatMatch(afterD, normalizedStats, 1
|
|
317
|
+
const bestMatch = findBestStatMatch(afterD, normalizedStats, 1);
|
|
272
318
|
if (bestMatch) {
|
|
273
319
|
const [, value] = bestMatch;
|
|
274
320
|
return `${diceCount}d${value.toString()}`;
|
|
@@ -276,7 +322,7 @@ function handleDiceAfterD(tokenStd, normalizedStats) {
|
|
|
276
322
|
return null;
|
|
277
323
|
}
|
|
278
324
|
function handleSimpleToken(tokenStd, token, normalizedStats, minThreshold) {
|
|
279
|
-
const bestMatch = findBestStatMatch(tokenStd, normalizedStats, minThreshold
|
|
325
|
+
const bestMatch = findBestStatMatch(tokenStd, normalizedStats, minThreshold);
|
|
280
326
|
if (bestMatch) {
|
|
281
327
|
const [, value] = bestMatch;
|
|
282
328
|
return value.toString();
|
|
@@ -1472,9 +1518,12 @@ export {
|
|
|
1472
1518
|
DiceTypeError,
|
|
1473
1519
|
EmptyObjectError,
|
|
1474
1520
|
FormulaError,
|
|
1521
|
+
MIN_THRESHOLD_MATCH,
|
|
1475
1522
|
MaxGreater,
|
|
1523
|
+
NORMALIZE_SINGLE_DICE,
|
|
1476
1524
|
NoStatisticsError,
|
|
1477
1525
|
OPTIONAL_COMMENT,
|
|
1526
|
+
REMOVER_PATTERN,
|
|
1478
1527
|
SIGN_REGEX,
|
|
1479
1528
|
SIGN_REGEX_SPACE,
|
|
1480
1529
|
SYMBOL_DICE,
|
|
@@ -1493,19 +1542,23 @@ export {
|
|
|
1493
1542
|
findBestStatMatch,
|
|
1494
1543
|
generateRandomStat,
|
|
1495
1544
|
generateStatsDice,
|
|
1545
|
+
getCachedRegex,
|
|
1496
1546
|
getEngine,
|
|
1497
1547
|
getEngineId,
|
|
1548
|
+
includeDiceType,
|
|
1498
1549
|
isNumber,
|
|
1499
1550
|
levenshteinDistance,
|
|
1500
1551
|
randomInt,
|
|
1501
1552
|
replaceExpByRandom,
|
|
1502
1553
|
replaceFormulaInDice,
|
|
1503
1554
|
replaceInFormula,
|
|
1555
|
+
replaceUnknown,
|
|
1504
1556
|
roll,
|
|
1505
1557
|
standardizeDice,
|
|
1506
1558
|
templateSchema,
|
|
1507
1559
|
testDiceRegistered,
|
|
1508
1560
|
testStatCombinaison,
|
|
1561
|
+
verifyStatMatcherPattern,
|
|
1509
1562
|
verifyTemplateValue
|
|
1510
1563
|
};
|
|
1511
1564
|
//# sourceMappingURL=index.mjs.map
|