« get me outta code hell

find: move miscapitalization responsibility to findHelper - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2026-06-05 20:50:04 -0300
committer(quasar) nebula <qznebula@protonmail.com>2026-06-05 20:52:40 -0300
commit1d5fb208d75b69774c21557b5f5ec8d2d94a8ada (patch)
tree0eff92dc51dfb10cd2357b5313625e0b52bdae2b
parent268360876b53998df93666d31307aa901a93bddb (diff)
find: move miscapitalization responsibility to findHelper preview
repeat errored by-name references as though capitalization-fuzzy;
if there's then a match, this is a capitalization error

interacts with the same fuzzy cache as when quering this data
capitalization-fuzzy from any other external entry. this probably
consumes extra long-lasting memory (cache) in cases where no fuzzy
search would be performed - though only if there are failed refs
to double-check in the first place

(however, there ARE expected failed refs in mixed-find uses, so
 real normal memory footprint is possibly increased there.)
-rw-r--r--src/find.js108
1 files changed, 71 insertions, 37 deletions
diff --git a/src/find.js b/src/find.js
index 6436532a..1db46c31 100644
--- a/src/find.js
+++ b/src/find.js
@@ -207,18 +207,7 @@ export function prepareMatchByName(mode, fuzz, {byName, multipleNameMatches}) {
     const match = byName[normalizedName];
 
     if (match) {
-      if (
-        !fuzz?.capitalization &&
-        name !== match.name &&
-        name.toLowerCase === match.name.toLowerCase()
-      ) {
-        return oopsNameCapitalizationMismatch(mode, {
-          matchingName: name,
-          matchedName: match.name,
-        });
-      } else {
-        return match.thing;
-      }
+      return match.thing;
     } else if (multipleNameMatches[normalizedName]) {
       return oopsMultipleNameMatches(mode, {
         name,
@@ -299,21 +288,23 @@ function findHelper({
   // hasn't changed!
   const cache = new WeakMap();
 
-  return (fullRef, data, {
-    // The mode argument here may be 'warn', 'error', or 'quiet'. 'error' throws
-    // errors for null matches (with details about the error), while 'warn' and
-    // 'quiet' both return null, with 'warn' logging details directly to the
-    // console.
-    mode = 'warn',
+  const entry = (fullRef, data, opts = {}) => {
+    if (!fullRef) return null;
 
-    from = null,
+    const {
+      // The mode argument here may be 'warn', 'error', or 'quiet'. 'error' throws
+      // errors for null matches (with details about the error), while 'warn' and
+      // 'quiet' both return null, with 'warn' logging details directly to the
+      // console.
+      mode = 'warn',
 
-    fuzz = {
-      capitalization: false,
-      kebab: false,
-    },
-  } = {}) => {
-    if (!fullRef) return null;
+      from = null,
+
+      fuzz = {
+        capitalization: false,
+        kebab: false,
+      },
+    } = opts;
 
     if (typeof fullRef !== 'string') {
       throw new TypeError(`Expected a string, got ${typeAppearance(fullRef)}`);
@@ -362,20 +353,63 @@ function findHelper({
 
     const {byDirectory, byName, multipleNameMatches} = fuzzSubcache;
 
-    return matchHelper(fullRef, mode, {
-      matchByDirectory:
-        prepareMatchByDirectory(mode, {
-          referenceTypes,
-          byDirectory,
-        }),
+    let match, matchError;
+    try {
+      match =
+        matchHelper(fullRef, mode, {
+          matchByDirectory:
+            prepareMatchByDirectory(mode, {
+              referenceTypes,
+              byDirectory,
+            }),
+
+          matchByName:
+            prepareMatchByName(mode, fuzz, {
+              byName,
+              multipleNameMatches,
+            }),
+        });
+    } catch (caughtError) {
+      match = null;
+      matchError = caughtError;
+    }
 
-      matchByName:
-        prepareMatchByName(mode, fuzz, {
-          byName,
-          multipleNameMatches,
-        }),
-    });
+    if (match) {
+      return match;
+    }
+
+    if (!fuzz?.capitalization && !fullRef.match(keyRefRegex)?.groups.key) {
+      let miscapitalizedMatch;
+
+      try {
+        miscapitalizedMatch =
+          entry(fullRef, data, {
+            ...opts,
+            fuzz: {
+              ...fuzz ?? {},
+              capitalization: true,
+            },
+          });
+      } catch {
+        miscapitalizedMatch = null;
+      }
+
+      if (miscapitalizedMatch) {
+        return oopsNameCapitalizationMismatch(mode, {
+          matchingName: fullRef,
+          matchedName: miscapitalizedMatch.name,
+        });
+      }
+    }
+
+    if (matchError) {
+      throw matchError;
+    } else {
+      return null;
+    }
   };
+
+  return entry;
 }
 
 const hardcodedFindSpecs = {