« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/composite')
-rw-r--r--src/data/composite/data/index.js1
-rw-r--r--src/data/composite/data/withUniqueItemsOnly.js40
-rw-r--r--src/data/composite/wiki-data/withParsedCommentaryEntries.js40
-rw-r--r--src/data/composite/wiki-properties/commentatorArtists.js28
4 files changed, 92 insertions, 17 deletions
diff --git a/src/data/composite/data/index.js b/src/data/composite/data/index.js
index db1c37cc..e2927afd 100644
--- a/src/data/composite/data/index.js
+++ b/src/data/composite/data/index.js
@@ -11,3 +11,4 @@ export {default as withPropertiesFromObject} from './withPropertiesFromObject.js
 export {default as withPropertyFromList} from './withPropertyFromList.js';
 export {default as withPropertyFromObject} from './withPropertyFromObject.js';
 export {default as withUnflattenedList} from './withUnflattenedList.js';
+export {default as withUniqueItemsOnly} from './withUniqueItemsOnly.js';
diff --git a/src/data/composite/data/withUniqueItemsOnly.js b/src/data/composite/data/withUniqueItemsOnly.js
new file mode 100644
index 00000000..7ee08b08
--- /dev/null
+++ b/src/data/composite/data/withUniqueItemsOnly.js
@@ -0,0 +1,40 @@
+// Excludes duplicate items from a list and provides the results, overwriting
+// the list in-place, if possible.
+
+import {input, templateCompositeFrom} from '#composite';
+import {unique} from '#sugar';
+
+export default templateCompositeFrom({
+  annotation: `withUniqueItemsOnly`,
+
+  inputs: {
+    list: input({type: 'array'}),
+  },
+
+  outputs: ({
+    [input.staticDependency('list')]: list,
+  }) => [list ?? '#uniqueItems'],
+
+  steps: () => [
+    {
+      dependencies: [input('list')],
+      compute: (continuation, {
+        [input('list')]: list,
+      }) => continuation({
+        ['#values']:
+          unique(list),
+      }),
+    },
+
+    {
+      dependencies: ['#values', input.staticDependency('list')],
+      compute: (continuation, {
+        '#values': values,
+        [input.staticDependency('list')]: list,
+      }) => continuation({
+        [list ?? '#uniqueItems']:
+          values,
+      }),
+    },
+  ],
+});
diff --git a/src/data/composite/wiki-data/withParsedCommentaryEntries.js b/src/data/composite/wiki-data/withParsedCommentaryEntries.js
index 7b1c9484..edfc9e3c 100644
--- a/src/data/composite/wiki-data/withParsedCommentaryEntries.js
+++ b/src/data/composite/wiki-data/withParsedCommentaryEntries.js
@@ -4,7 +4,12 @@ import {stitchArrays} from '#sugar';
 import {isCommentary} from '#validators';
 import {commentaryRegex} from '#wiki-data';
 
-import {fillMissingListItems, withPropertiesFromList} from '#composite/data';
+import {
+  fillMissingListItems,
+  withFlattenedList,
+  withPropertiesFromList,
+  withUnflattenedList,
+} from '#composite/data';
 
 import withResolvedReferenceList from './withResolvedReferenceList.js';
 
@@ -86,23 +91,42 @@ export default templateCompositeFrom({
       list: '#rawMatches.groups',
       prefix: input.value('#entries'),
       properties: input.value([
-        'artistReference',
+        'artistReferences',
         'artistDisplayText',
         'annotation',
         'date',
       ]),
     }),
 
-    // The artistReference group will always have a value, since it's required
+    // The artistReferences group will always have a value, since it's required
     // for the line to match in the first place.
 
+    {
+      dependencies: ['#entries.artistReferences'],
+      compute: (continuation, {
+        ['#entries.artistReferences']: artistReferenceTexts,
+      }) => continuation({
+        ['#entries.artistReferences']:
+          artistReferenceTexts
+            .map(text => text.split(',').map(ref => ref.trim())),
+      }),
+    },
+
+    withFlattenedList({
+      list: '#entries.artistReferences',
+    }),
+
     withResolvedReferenceList({
-      list: '#entries.artistReference',
+      list: '#flattenedList',
       data: 'artistData',
       find: input.value(find.artist),
       notFoundMode: input.value('null'),
+    }),
+
+    withUnflattenedList({
+      list: '#resolvedReferenceList',
     }).outputs({
-      '#resolvedReferenceList': '#entries.artist',
+      '#unflattenedList': '#entries.artists',
     }),
 
     fillMissingListItems({
@@ -127,7 +151,7 @@ export default templateCompositeFrom({
 
     {
       dependencies: [
-        '#entries.artist',
+        '#entries.artists',
         '#entries.artistDisplayText',
         '#entries.annotation',
         '#entries.date',
@@ -135,7 +159,7 @@ export default templateCompositeFrom({
       ],
 
       compute: (continuation, {
-        ['#entries.artist']: artist,
+        ['#entries.artists']: artists,
         ['#entries.artistDisplayText']: artistDisplayText,
         ['#entries.annotation']: annotation,
         ['#entries.date']: date,
@@ -143,7 +167,7 @@ export default templateCompositeFrom({
       }) => continuation({
         ['#parsedCommentaryEntries']:
           stitchArrays({
-            artist,
+            artists,
             artistDisplayText,
             annotation,
             date,
diff --git a/src/data/composite/wiki-properties/commentatorArtists.js b/src/data/composite/wiki-properties/commentatorArtists.js
index 8720e66d..f400bbfc 100644
--- a/src/data/composite/wiki-properties/commentatorArtists.js
+++ b/src/data/composite/wiki-properties/commentatorArtists.js
@@ -4,8 +4,10 @@
 import {input, templateCompositeFrom} from '#composite';
 import {unique} from '#sugar';
 
-import {exitWithoutDependency} from '#composite/control-flow';
-import {withPropertyFromList} from '#composite/data';
+import {exitWithoutDependency, exposeDependency}
+  from '#composite/control-flow';
+import {withFlattenedList, withPropertyFromList, withUniqueItemsOnly}
+  from '#composite/data';
 import {withParsedCommentaryEntries} from '#composite/wiki-data';
 
 export default templateCompositeFrom({
@@ -26,15 +28,23 @@ export default templateCompositeFrom({
 
     withPropertyFromList({
       list: '#parsedCommentaryEntries',
-      property: input.value('artist'),
+      property: input.value('artists'),
     }).outputs({
-      '#parsedCommentaryEntries.artist': '#artists',
+      '#parsedCommentaryEntries.artists': '#artistLists',
     }),
 
-    {
-      dependencies: ['#artists'],
-      compute: ({'#artists': artists}) =>
-        unique(artists.filter(artist => artist !== null)),
-    },
+    withFlattenedList({
+      list: '#artistLists',
+    }).outputs({
+      '#flattenedList': '#artists',
+    }),
+
+    withUniqueItemsOnly({
+      list: '#artists',
+    }),
+
+    exposeDependency({
+      dependency: '#artists',
+    }),
   ],
 });