« get me outta code hell

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:
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;