« get me outta code hell

content: getContentEntryTotals - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2026-01-01 15:13:51 -0400
committer(quasar) nebula <qznebula@protonmail.com>2026-01-01 15:16:33 -0400
commit07059614fb0e5b64950b21abdd46b99deb481dfd (patch)
treed46473073ebfccff8fd10eb98c57309a7e776688 /src/content/dependencies
parent22c69347998f1ca076f9131567903cd983cb5960 (diff)
content: getContentEntryTotals
Diffstat (limited to 'src/content/dependencies')
-rw-r--r--src/content/dependencies/generateAlbumCommentaryPage.js52
-rw-r--r--src/content/dependencies/generateCommentaryIndexPage.js54
-rw-r--r--src/content/dependencies/getContentEntryTotals.js29
3 files changed, 67 insertions, 68 deletions
diff --git a/src/content/dependencies/generateAlbumCommentaryPage.js b/src/content/dependencies/generateAlbumCommentaryPage.js
index febaba19..ffa70dcf 100644
--- a/src/content/dependencies/generateAlbumCommentaryPage.js
+++ b/src/content/dependencies/generateAlbumCommentaryPage.js
@@ -1,6 +1,4 @@
-import multilingualWordCount from 'word-count';
-
-import {accumulateSum, empty, stitchArrays} from '#sugar';
+import {empty, stitchArrays} from '#sugar';
 
 export default {
   query(album) {
@@ -39,10 +37,11 @@ export default {
     relations.albumNavAccent =
       relation('generateAlbumNavAccent', album, null);
 
-    relations.bodiesForWordCount =
-      [album.commentary, ...album.tracks.map(t => t.commentary)]
-        .flat()
-        .map(entry => relation('transformContent', entry.body));
+    relations.totals =
+      relation('getContentEntryTotals',
+        ([album.commentary,
+          ...album.tracks.map(t => t.commentary)])
+          .flat());
 
     if (!empty(album.commentary)) {
       relations.albumCommentaryHeading =
@@ -100,19 +99,6 @@ export default {
     data.color = album.color;
     data.date = album.date;
 
-    data.entryCount =
-      query.thingsWithCommentary
-        .flatMap(({commentary}) => commentary)
-        .length;
-
-    data.wordCount =
-      query.thingsWithCommentary
-        .flatMap(({commentary}) => commentary)
-        .map(({body}) => body)
-        .join(' ')
-        .split(' ')
-        .length;
-
     data.trackCommentaryTrackDates =
       query.tracksWithCommentary
         .map(track => track.dateFirstReleased);
@@ -151,7 +137,7 @@ export default {
 
             [
               data.date &&
-              data.entryCount >= 1 &&
+              relations.totals.entryCount >= 1 &&
                 language.$('releaseInfo.albumReleased', {
                   date:
                     html.tag('b',
@@ -161,27 +147,19 @@ export default {
               language.encapsulate(pageCapsule, 'infoLine', workingCapsule => {
                 const workingOptions = {};
 
-                if (data.entryCount >= 1) {
-                  const wordCount =
-                    accumulateSum(
-                      relations.bodiesForWordCount.flatMap(body =>
-                        multilingualWordCount(
-                          html.resolve(
-                            body.slot('mode', 'multiline'),
-                            {normalize: 'plain'}))));
-
-                  const {entryCount} = data;
-
+                if (relations.totals.entryCount >= 1) {
                   workingOptions.words =
                     html.tag('b',
-                      language.formatWordCount(wordCount, {unit: true}));
+                      language.formatWordCount(
+                        relations.totals.wordCount,
+                        {unit: true}));
 
                   workingOptions.entries =
                     html.tag('b',
-                      language.countCommentaryEntries(entryCount, {unit: true}));
-                }
-
-                if (data.entryCount === 0) {
+                      language.countCommentaryEntries(
+                        relations.totals.entryCount,
+                        {unit: true}));
+                } else {
                   workingCapsule += '.withoutCommentary';
                 }
 
diff --git a/src/content/dependencies/generateCommentaryIndexPage.js b/src/content/dependencies/generateCommentaryIndexPage.js
index 8cc30913..ced5dddd 100644
--- a/src/content/dependencies/generateCommentaryIndexPage.js
+++ b/src/content/dependencies/generateCommentaryIndexPage.js
@@ -1,7 +1,12 @@
-import multilingualWordCount from 'word-count';
-
 import {sortChronologically} from '#sort';
-import {accumulateSum, filterMultipleArrays, stitchArrays} from '#sugar';
+
+import {
+  accumulateSum,
+  empty,
+  filterMultipleArrays,
+  stitchArrays,
+  transposeArrays,
+} from '#sugar';
 
 export default {
   sprawl: ({albumData}) =>
@@ -13,20 +18,13 @@ export default {
     query.albums =
       sortChronologically(sprawl.albumData.slice());
 
-    const entries =
+    query.entries =
       query.albums.map(album =>
         [album, ...album.tracks]
-          .filter(({commentary}) => commentary)
           .flatMap(({commentary}) => commentary));
 
-    query.bodies =
-      entries.map(entries => entries.map(entry => entry.body));
-
-    query.entryCounts =
-      entries.map(entries => entries.length);
-
-    filterMultipleArrays(query.albums, query.bodies, query.entryCounts,
-      (album, bodies, entryCount) => entryCount >= 1);
+    filterMultipleArrays(query.albums, query.entries,
+      (_album, entries) => !empty(entries));
 
     return query;
   },
@@ -39,30 +37,24 @@ export default {
       query.albums
         .map(album => relation('linkAlbumCommentary', album)),
 
-    albumBodies:
-      query.bodies
-        .map(bodies => bodies
-          .map(body => relation('transformContent', body))),
-  }),
-
-  data: (query) => ({
-    entryCounts: query.entryCounts,
-    totalEntryCount: accumulateSum(query.entryCounts),
+    albumTotals:
+      query.entries
+        .map(entries => relation('getContentEntryTotals', entries)),
   }),
 
-  generate(data, relations, {html, language}) {
-    const wordCounts =
-      relations.albumBodies.map(bodies =>
-        accumulateSum(bodies, body =>
-          multilingualWordCount(
-            html.resolve(
-              body.slot('mode', 'multiline'),
-              {normalize: 'plain'}))));
+  generate(relations, {html, language}) {
+    const [wordCounts, entryCounts] =
+      transposeArrays(
+        relations.albumTotals.map(totals => [
+          totals.wordCount,
+          totals.entryCount,
+        ]));
 
     const totalWordCount =
       accumulateSum(wordCounts);
 
-    const {entryCounts, totalEntryCount} = data;
+    const totalEntryCount =
+      accumulateSum(entryCounts);
 
     return language.encapsulate('commentaryIndex', pageCapsule =>
       relations.layout.slots({
diff --git a/src/content/dependencies/getContentEntryTotals.js b/src/content/dependencies/getContentEntryTotals.js
new file mode 100644
index 00000000..ce590887
--- /dev/null
+++ b/src/content/dependencies/getContentEntryTotals.js
@@ -0,0 +1,29 @@
+import multilingualWordCount from 'word-count';
+
+import {accumulateSum} from '#sugar';
+
+export default {
+  relations: (relation, entries) => ({
+    bodies:
+      entries
+        .map(entry => relation('transformContent', entry.body)),
+  }),
+
+  data: (entries) => ({
+    entries:
+      entries.length,
+  }),
+
+  generate: (data, relations, {html}) => ({
+    entryCount:
+      data.entries,
+
+    wordCount:
+      accumulateSum(
+        relations.bodies.flatMap(body =>
+          multilingualWordCount(
+            html.resolve(
+              body.slot('mode', 'multiline'),
+              {normalize: 'plain'})))),
+  }),
+};
\ No newline at end of file