« get me outta code hell

find: factor out keyRefRegex & tidy usage - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-11-14 17:45:32 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-11-15 20:40:12 -0400
commit08c46ad7d853534b8dba9e56159f05899944d0e6 (patch)
treeb2d5d127e3a692486c4d73e4c428bbf7d782750d /src
parentbcdd272af749f18f94ef4da022342e899909c097 (diff)
find: factor out keyRefRegex & tidy usage
Diffstat (limited to 'src')
-rw-r--r--src/find.js21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/find.js b/src/find.js
index b7465b9c..fa5f6904 100644
--- a/src/find.js
+++ b/src/find.js
@@ -17,6 +17,9 @@ function warnOrThrow(mode, message) {
   return null;
 }
 
+export const keyRefRegex =
+  new RegExp(String.raw`^(?:(?<key>[a-z-]*):(?=\S))?(?<ref>.*)$`);
+
 export function processAvailableMatchesByName(data, {
   include = _thing => true,
 
@@ -98,9 +101,6 @@ function findHelper({
   getMatchableNames = undefined,
   getMatchableDirectories = undefined,
 }) {
-  const keyRefRegex =
-    new RegExp(String.raw`^(?:(${referenceTypes.join('|')}):(?=\S))?(.*)$`);
-
   // Note: This cache explicitly *doesn't* support mutable data arrays. If the
   // data array is modified, make sure it's actually a new array object, not
   // the original, or the cache here will break and act as though the data
@@ -139,20 +139,25 @@ function findHelper({
         `Malformed link reference: "${fullRef}"`);
     }
 
-    const typePart = regexMatch[1];
-    const refPart = regexMatch[2];
+    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(', '));
+    }
 
     const normalizedName =
-      (typePart
+      (keyPart
         ? null
         : refPart.toLowerCase());
 
     const match =
-      (typePart
+      (keyPart
         ? subcache.byDirectory[refPart]
         : subcache.byName[normalizedName]);
 
-    if (!match && !typePart) {
+    if (!match && !keyPart) {
       if (subcache.multipleNameMatches[normalizedName]) {
         return warnOrThrow(mode,
           `Multiple matches for reference "${fullRef}". Please resolve:\n` +