« get me outta code hell

rename "search" fns to "find", move to utils - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <towerofnix@gmail.com>2021-05-14 13:44:40 -0300
committer(quasar) nebula <towerofnix@gmail.com>2021-05-14 13:44:40 -0300
commit245ce84ef7361334f244d91627b8b7cf201df4f7 (patch)
tree2a944189b724767a4463d7cd51135f5b66d472d1 /src/util
parent16bea3a53372692ebf4b47e59123e0e11f2f8db2 (diff)
rename "search" fns to "find", move to utils
Diffstat (limited to 'src/util')
-rw-r--r--src/util/find.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/util/find.js b/src/util/find.js
new file mode 100644
index 00000000..aef3d3bc
--- /dev/null
+++ b/src/util/find.js
@@ -0,0 +1,50 @@
+function findHelper(keys, dataProp, findFn) {
+    return (ref, {wikiData}) => {
+        if (!ref) return null;
+        ref = ref.replace(new RegExp(`^(${keys.join('|')}):`), '');
+
+        const found = findFn(ref, wikiData[dataProp]);
+        if (!found) {
+            logWarn`Didn't match anything for ${ref}! (${keys.join(', ')})`;
+        }
+
+        return found;
+    };
+}
+
+function matchDirectory(ref, data) {
+    return data.find(({ directory }) => directory === ref);
+}
+
+function matchDirectoryOrName(ref, data) {
+    let thing;
+
+    thing = matchDirectory(ref, data);
+    if (thing) return thing;
+
+    thing = data.find(({ name }) => name === ref);
+    if (thing) return thing;
+
+    thing = data.find(({ name }) => name.toLowerCase() === ref.toLowerCase());
+    if (thing) {
+        logWarn`Bad capitalization: ${'\x1b[31m' + ref} -> ${'\x1b[32m' + thing.name}`;
+        return thing;
+    }
+
+    return null;
+}
+
+const find = {
+    album: findHelper(['album', 'album-commentary'], 'albumData', matchDirectoryOrName),
+    artist: findHelper(['artist', 'artist-gallery'], 'artistData', matchDirectoryOrName),
+    flash: findHelper(['flash'], 'flashData', matchDirectory),
+    group: findHelper(['group', 'group-gallery'], 'groupData', matchDirectoryOrName),
+    listing: findHelper(['listing'], 'listingSpec', matchDirectory),
+    newsEntry: findHelper(['news-entry'], 'newsData', matchDirectory),
+    staticPage: findHelper(['static'], 'staticPageData', matchDirectory),
+    tag: findHelper(['tag'], 'tagData', (ref, data) =>
+        matchDirectoryOrName(ref.startsWith('cw: ') ? ref.slice(4) : ref, data)),
+    track: findHelper(['track'], 'trackData', matchDirectoryOrName)
+};
+
+export default find;