« get me outta code hell

data: stub real results for find.mixed - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/find.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-11-14 17:51:48 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-11-15 20:40:12 -0400
commit1effe97c83585e4ba7bab68bafdbee733a6a25b4 (patch)
treef4a446103d8bc29b712ae7b0fd0628b90799c5ad /src/find.js
parent08c46ad7d853534b8dba9e56159f05899944d0e6 (diff)
data: stub real results for find.mixed
Works by name, not by directory. Also no caching, and some stuff
common with findHelper should be factored out.
Diffstat (limited to 'src/find.js')
-rw-r--r--src/find.js71
1 files changed, 63 insertions, 8 deletions
diff --git a/src/find.js b/src/find.js
index fa5f6904..b2d32f80 100644
--- a/src/find.js
+++ b/src/find.js
@@ -262,15 +262,70 @@ export const boundFindOptions = Symbol.for('find.boundFindOptions');
 const mixedFindStore = new Map();
 
 function findMixedHelper(config) {
-  const keys = Object.keys(config);
-  const tokens = Object.values(config);
-  const specKeys = tokens.map(token => token[findTokenKey]);
-  const specs = specKeys.map(specKey => findFindSpec(specKey));
+  const
+    keys = Object.keys(config), referenceTypes = keys,
+    tokens = Object.values(config),
+    specKeys = tokens.map(token => token[findTokenKey]),
+    specs = specKeys.map(specKey => findFindSpec(specKey));
+
+  return (fullRef, data, {mode = 'warn'} = {}) => {
+    // TODO: Cache stuff below by identity of data
+
+    const byName = Object.create(null);
+    const multipleNameMatches = Object.create(null);
+
+    for (const spec of specs) {
+      processAvailableMatchesByName(data, {
+        ...spec,
+
+        results: byName,
+        multipleNameMatches,
+      });
+    }
+
+    // TODO: Factor out this common behavior w/ findHelper
+
+    const regexMatch = fullRef.match(keyRefRegex);
+    if (!regexMatch) {
+      return warnOrThrow(mode,
+        `Malformed link reference: "${fullRef}"`);
+    }
+
+    const {key: keyPart, ref: refPart} = regexMatch.groups;
+
+    if (keyPart && !referenceTypes.includes(keyPart)) {
+      return warnOrThrow(mode,
+        `Reference starts with "${keyPart}:", expected ` +
+        referenceTypes.map(type => `"${type}:"`).join(', '));
+    }
 
-  return () => {
-    console.log(`I would do something with:`);
-    console.log(specs);
-    return null;
+    const normalizedName =
+      (keyPart
+        ? null
+        : refPart.toLowerCase());
+
+    const match =
+      (keyPart
+        ? null /* TODO: Do something */
+        : byName[normalizedName]);
+
+    if (!match && !keyPart) {
+      if (multipleNameMatches[normalizedName]) {
+        return warnOrThrow(mode,
+          `Multiple matches for reference "${fullRef}". Please resolve:\n` +
+          multipleNameMatches[normalizedName]
+            .map(match => `- ${inspect(match)}\n`)
+            .join('') +
+          `Returning null for this reference.`);
+      }
+    }
+
+    if (!match) {
+      return warnOrThrow(mode,
+        `Didn't match anything for ${colors.bright(fullRef)}`);
+    }
+
+    return match;
   };
 }