@dicelette/core 1.26.0 → 1.27.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.mjs CHANGED
@@ -210,23 +210,18 @@ function levenshteinDistance(str1, str2) {
210
210
  }
211
211
  return matrix[str2.length][str1.length];
212
212
  }
213
- function findBestStatMatch(searchTerm, normalizedStats, similarityThreshold = MIN_THRESHOLD_MATCH, partialSearch = true) {
213
+ function findBestStatMatch(searchTerm, normalizedStats, similarityThreshold = MIN_THRESHOLD_MATCH) {
214
214
  const exact = normalizedStats.get(searchTerm);
215
215
  if (exact) return exact;
216
- if (partialSearch) {
217
- const candidates = [];
218
- for (const [normalizedKey, original] of normalizedStats) {
219
- if (normalizedKey.startsWith(searchTerm))
220
- candidates.push([original, normalizedKey.length]);
221
- else if (normalizedKey.endsWith(searchTerm))
222
- candidates.push([original, normalizedKey.length]);
223
- else if (normalizedKey.includes(searchTerm))
224
- candidates.push([original, normalizedKey.length]);
225
- }
226
- if (candidates.length > 0) {
227
- candidates.sort((a, b) => a[1] - b[1]);
228
- return candidates[0][0];
229
- }
216
+ const candidates = [];
217
+ for (const [normalizedKey, original] of normalizedStats) {
218
+ if (normalizedKey.startsWith(searchTerm))
219
+ candidates.push([original, calculateSimilarity(searchTerm, normalizedKey)]);
220
+ }
221
+ if (candidates.length === 1) return candidates[0][0];
222
+ if (candidates.length > 0) {
223
+ candidates.sort((a, b) => b[1] - a[1]);
224
+ if (candidates[0][1] >= similarityThreshold) return candidates[0][0];
230
225
  }
231
226
  let bestMatch;
232
227
  let bestSimilarity = 0;
@@ -248,8 +243,7 @@ function findBestRecord(record, searchTerm, similarityThreshold = MIN_THRESHOLD_
248
243
  return findBestStatMatch(
249
244
  searchTerm.standardize(),
250
245
  normalizeRecord,
251
- similarityThreshold,
252
- false
246
+ similarityThreshold
253
247
  ) || null;
254
248
  }
255
249
 
@@ -268,7 +262,7 @@ function handleDiceAfterD(tokenStd, normalizedStats) {
268
262
  if (!diceMatch) return null;
269
263
  const diceCount = diceMatch[1] || "";
270
264
  const afterD = diceMatch[2];
271
- const bestMatch = findBestStatMatch(afterD, normalizedStats, 1, false);
265
+ const bestMatch = findBestStatMatch(afterD, normalizedStats, 1);
272
266
  if (bestMatch) {
273
267
  const [, value] = bestMatch;
274
268
  return `${diceCount}d${value.toString()}`;
@@ -276,7 +270,7 @@ function handleDiceAfterD(tokenStd, normalizedStats) {
276
270
  return null;
277
271
  }
278
272
  function handleSimpleToken(tokenStd, token, normalizedStats, minThreshold) {
279
- const bestMatch = findBestStatMatch(tokenStd, normalizedStats, minThreshold, false);
273
+ const bestMatch = findBestStatMatch(tokenStd, normalizedStats, minThreshold);
280
274
  if (bestMatch) {
281
275
  const [, value] = bestMatch;
282
276
  return value.toString();