@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.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
|
|
@@ -318,7 +331,7 @@ declare function calculateSimilarity(str1: string, str2: string): number;
|
|
|
318
331
|
* Calculates the Levenshtein distance between two strings.
|
|
319
332
|
*/
|
|
320
333
|
declare function levenshteinDistance(str1: string, str2: string): number;
|
|
321
|
-
declare function findBestStatMatch<T>(searchTerm: string, normalizedStats: Map<string, T>, similarityThreshold?: number
|
|
334
|
+
declare function findBestStatMatch<T>(searchTerm: string, normalizedStats: Map<string, T>, similarityThreshold?: number): T | undefined;
|
|
322
335
|
/**
|
|
323
336
|
* Find the snippet name with the highest similarity to `macroName`.
|
|
324
337
|
* Single-pass O(n) algorithm: keeps the best (name, similarity) seen so far.
|
|
@@ -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
|
|
@@ -318,7 +331,7 @@ declare function calculateSimilarity(str1: string, str2: string): number;
|
|
|
318
331
|
* Calculates the Levenshtein distance between two strings.
|
|
319
332
|
*/
|
|
320
333
|
declare function levenshteinDistance(str1: string, str2: string): number;
|
|
321
|
-
declare function findBestStatMatch<T>(searchTerm: string, normalizedStats: Map<string, T>, similarityThreshold?: number
|
|
334
|
+
declare function findBestStatMatch<T>(searchTerm: string, normalizedStats: Map<string, T>, similarityThreshold?: number): T | undefined;
|
|
322
335
|
/**
|
|
323
336
|
* Find the snippet name with the highest similarity to `macroName`.
|
|
324
337
|
* Single-pass O(n) algorithm: keeps the best (name, similarity) seen so far.
|
|
@@ -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;
|
|
@@ -275,23 +321,18 @@ function levenshteinDistance(str1, str2) {
|
|
|
275
321
|
}
|
|
276
322
|
return matrix[str2.length][str1.length];
|
|
277
323
|
}
|
|
278
|
-
function findBestStatMatch(searchTerm, normalizedStats, similarityThreshold = MIN_THRESHOLD_MATCH
|
|
324
|
+
function findBestStatMatch(searchTerm, normalizedStats, similarityThreshold = MIN_THRESHOLD_MATCH) {
|
|
279
325
|
const exact = normalizedStats.get(searchTerm);
|
|
280
326
|
if (exact) return exact;
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
}
|
|
291
|
-
if (candidates.length > 0) {
|
|
292
|
-
candidates.sort((a, b) => a[1] - b[1]);
|
|
293
|
-
return candidates[0][0];
|
|
294
|
-
}
|
|
327
|
+
const candidates = [];
|
|
328
|
+
for (const [normalizedKey, original] of normalizedStats) {
|
|
329
|
+
if (normalizedKey.startsWith(searchTerm))
|
|
330
|
+
candidates.push([original, calculateSimilarity(searchTerm, normalizedKey)]);
|
|
331
|
+
}
|
|
332
|
+
if (candidates.length === 1) return candidates[0][0];
|
|
333
|
+
if (candidates.length > 0) {
|
|
334
|
+
candidates.sort((a, b) => b[1] - a[1]);
|
|
335
|
+
if (candidates[0][1] >= similarityThreshold) return candidates[0][0];
|
|
295
336
|
}
|
|
296
337
|
let bestMatch;
|
|
297
338
|
let bestSimilarity = 0;
|
|
@@ -313,10 +354,22 @@ function findBestRecord(record, searchTerm, similarityThreshold = MIN_THRESHOLD_
|
|
|
313
354
|
return findBestStatMatch(
|
|
314
355
|
searchTerm.standardize(),
|
|
315
356
|
normalizeRecord,
|
|
316
|
-
similarityThreshold
|
|
317
|
-
false
|
|
357
|
+
similarityThreshold
|
|
318
358
|
) || null;
|
|
319
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
|
+
}
|
|
320
373
|
|
|
321
374
|
// src/utils.ts
|
|
322
375
|
function escapeRegex(string) {
|
|
@@ -333,7 +386,7 @@ function handleDiceAfterD(tokenStd, normalizedStats) {
|
|
|
333
386
|
if (!diceMatch) return null;
|
|
334
387
|
const diceCount = diceMatch[1] || "";
|
|
335
388
|
const afterD = diceMatch[2];
|
|
336
|
-
const bestMatch = findBestStatMatch(afterD, normalizedStats, 1
|
|
389
|
+
const bestMatch = findBestStatMatch(afterD, normalizedStats, 1);
|
|
337
390
|
if (bestMatch) {
|
|
338
391
|
const [, value] = bestMatch;
|
|
339
392
|
return `${diceCount}d${value.toString()}`;
|
|
@@ -341,7 +394,7 @@ function handleDiceAfterD(tokenStd, normalizedStats) {
|
|
|
341
394
|
return null;
|
|
342
395
|
}
|
|
343
396
|
function handleSimpleToken(tokenStd, token, normalizedStats, minThreshold) {
|
|
344
|
-
const bestMatch = findBestStatMatch(tokenStd, normalizedStats, minThreshold
|
|
397
|
+
const bestMatch = findBestStatMatch(tokenStd, normalizedStats, minThreshold);
|
|
345
398
|
if (bestMatch) {
|
|
346
399
|
const [, value] = bestMatch;
|
|
347
400
|
return value.toString();
|
|
@@ -1538,9 +1591,12 @@ function generateRandomStat(total = 100, max, min, engine = import_rpg_dice_roll
|
|
|
1538
1591
|
DiceTypeError,
|
|
1539
1592
|
EmptyObjectError,
|
|
1540
1593
|
FormulaError,
|
|
1594
|
+
MIN_THRESHOLD_MATCH,
|
|
1541
1595
|
MaxGreater,
|
|
1596
|
+
NORMALIZE_SINGLE_DICE,
|
|
1542
1597
|
NoStatisticsError,
|
|
1543
1598
|
OPTIONAL_COMMENT,
|
|
1599
|
+
REMOVER_PATTERN,
|
|
1544
1600
|
SIGN_REGEX,
|
|
1545
1601
|
SIGN_REGEX_SPACE,
|
|
1546
1602
|
SYMBOL_DICE,
|
|
@@ -1559,19 +1615,23 @@ function generateRandomStat(total = 100, max, min, engine = import_rpg_dice_roll
|
|
|
1559
1615
|
findBestStatMatch,
|
|
1560
1616
|
generateRandomStat,
|
|
1561
1617
|
generateStatsDice,
|
|
1618
|
+
getCachedRegex,
|
|
1562
1619
|
getEngine,
|
|
1563
1620
|
getEngineId,
|
|
1621
|
+
includeDiceType,
|
|
1564
1622
|
isNumber,
|
|
1565
1623
|
levenshteinDistance,
|
|
1566
1624
|
randomInt,
|
|
1567
1625
|
replaceExpByRandom,
|
|
1568
1626
|
replaceFormulaInDice,
|
|
1569
1627
|
replaceInFormula,
|
|
1628
|
+
replaceUnknown,
|
|
1570
1629
|
roll,
|
|
1571
1630
|
standardizeDice,
|
|
1572
1631
|
templateSchema,
|
|
1573
1632
|
testDiceRegistered,
|
|
1574
1633
|
testStatCombinaison,
|
|
1634
|
+
verifyStatMatcherPattern,
|
|
1575
1635
|
verifyTemplateValue
|
|
1576
1636
|
});
|
|
1577
1637
|
//# sourceMappingURL=index.js.map
|