« get me outta code hell

find: decompose processAllAvailableMatches - 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:24:25 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-11-15 20:40:12 -0400
commitbcdd272af749f18f94ef4da022342e899909c097 (patch)
treed984533c0cd59c72b75169a43b89e9df494fe316 /src/find.js
parent6c27ca606997b37740dc2b79a9a0f9fb1b16b079 (diff)
find: decompose processAllAvailableMatches
Diffstat (limited to 'src/find.js')
-rw-r--r--src/find.js68
1 files changed, 44 insertions, 24 deletions
diff --git a/src/find.js b/src/find.js
index 995f7368..b7465b9c 100644
--- a/src/find.js
+++ b/src/find.js
@@ -17,7 +17,7 @@ function warnOrThrow(mode, message) {
   return null;
 }
 
-export function processAllAvailableMatches(data, {
+export function processAvailableMatchesByName(data, {
   include = _thing => true,
 
   getMatchableNames = thing =>
@@ -25,27 +25,12 @@ export function processAllAvailableMatches(data, {
       ? [thing.name]
       : []),
 
-  getMatchableDirectories = thing =>
-    (Object.hasOwn(thing, 'directory')
-      ? [thing.directory]
-      : [null]),
-} = {}) {
-  const byName = Object.create(null);
-  const byDirectory = Object.create(null);
-  const multipleNameMatches = Object.create(null);
-
+  results = Object.create(null),
+  multipleNameMatches = Object.create(null),
+}) {
   for (const thing of data) {
     if (!include(thing)) continue;
 
-    for (const directory of getMatchableDirectories(thing)) {
-      if (typeof directory !== 'string') {
-        logWarn`Unexpected ${typeAppearance(directory)} returned in directories for ${inspect(thing)}`;
-        continue;
-      }
-
-      byDirectory[directory] = thing;
-    }
-
     for (const name of getMatchableNames(thing)) {
       if (typeof name !== 'string') {
         logWarn`Unexpected ${typeAppearance(name)} returned in names for ${inspect(thing)}`;
@@ -54,20 +39,55 @@ export function processAllAvailableMatches(data, {
 
       const normalizedName = name.toLowerCase();
 
-      if (normalizedName in byName) {
-        const alreadyMatchesByName = byName[normalizedName];
-        byName[normalizedName] = null;
+      if (normalizedName in results) {
         if (normalizedName in multipleNameMatches) {
           multipleNameMatches[normalizedName].push(thing);
         } else {
-          multipleNameMatches[normalizedName] = [alreadyMatchesByName, thing];
+          multipleNameMatches[normalizedName] = [results[normalizedName], thing];
+          results[normalizedName] = null;
         }
       } else {
-        byName[normalizedName] = thing;
+        results[normalizedName] = thing;
+      }
+    }
+  }
+
+  return {results, multipleNameMatches};
+}
+
+export function processAvailableMatchesByDirectory(data, {
+  include = _thing => true,
+
+  getMatchableDirectories = thing =>
+    (Object.hasOwn(thing, 'directory')
+      ? [thing.directory]
+      : [null]),
+
+  results = Object.create(null),
+}) {
+  for (const thing of data) {
+    if (!include(thing)) continue;
+
+    for (const directory of getMatchableDirectories(thing)) {
+      if (typeof directory !== 'string') {
+        logWarn`Unexpected ${typeAppearance(directory)} returned in directories for ${inspect(thing)}`;
+        continue;
       }
+
+      results[directory] = thing;
     }
   }
 
+  return {results};
+}
+
+export function processAllAvailableMatches(data, spec) {
+  const {results: byName, multipleNameMatches} =
+    processAvailableMatchesByName(data, spec);
+
+  const {results: byDirectory} =
+    processAvailableMatchesByDirectory(data, spec);
+
   return {byName, byDirectory, multipleNameMatches};
 }