« get me outta code hell

data, yaml: split yaml loading specs into src/data/files - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/files/group.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2026-01-26 15:14:38 -0400
committer(quasar) nebula <qznebula@protonmail.com>2026-01-26 15:14:38 -0400
commita074fd54107c51c4fcbfedbbf6df6eca539d19d3 (patch)
tree4fec08106aa3054c1954c5fa4ade0fb880c5eeb3 /src/data/files/group.js
parent796e4bc1452b918bbf50a2e802b308f6ac20f2c2 (diff)
data, yaml: split yaml loading specs into src/data/files
Diffstat (limited to 'src/data/files/group.js')
-rw-r--r--src/data/files/group.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/data/files/group.js b/src/data/files/group.js
new file mode 100644
index 00000000..c10cbf98
--- /dev/null
+++ b/src/data/files/group.js
@@ -0,0 +1,45 @@
+import Thing from '#thing';
+
+export default ({
+  documentModes: {allInOne},
+  thingConstructors: {Group, GroupCategory},
+}) => ({
+  title: `Process groups file`,
+  file: 'groups.yaml',
+
+  documentMode: allInOne,
+  documentThing: document =>
+    ('Category' in document
+      ? GroupCategory
+      : Group),
+
+  connect(results) {
+    let groupCategory;
+    let groupRefs = [];
+
+    if (results[0] && !(results[0] instanceof GroupCategory)) {
+      throw new Error(`Expected a category at top of group data file`);
+    }
+
+    for (const thing of results) {
+      if (thing instanceof GroupCategory) {
+        if (groupCategory) {
+          Object.assign(groupCategory, {groups: groupRefs});
+        }
+
+        groupCategory = thing;
+        groupRefs = [];
+      } else {
+        groupRefs.push(Thing.getReference(thing));
+      }
+    }
+
+    if (groupCategory) {
+      Object.assign(groupCategory, {groups: groupRefs});
+    }
+  },
+
+  // Groups aren't sorted at all, always preserving the order in the data
+  // file as-is.
+  sort: null,
+});