« get me outta code hell

data: amorphize annotated reference lists - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-12-25 17:40:09 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-12-25 17:53:26 -0400
commitf43c4ba77f5598d5234b21fcd03fa7788b17afe5 (patch)
tree825c60368502f9fb1809b252656bfd2e24863712
parentb65edcb7e61d2e0c4de6528bb8ee3e5d2032a500 (diff)
data: amorphize annotated reference lists
-rw-r--r--src/data/composite/wiki-data/withResolvedAnnotatedReferenceList.js49
-rw-r--r--src/data/composite/wiki-properties/annotatedReferenceList.js37
-rw-r--r--src/data/validators.js11
-rw-r--r--src/data/yaml.js26
4 files changed, 75 insertions, 48 deletions
diff --git a/src/data/composite/wiki-data/withResolvedAnnotatedReferenceList.js b/src/data/composite/wiki-data/withResolvedAnnotatedReferenceList.js
index ac6b15fa..e2b8a0a1 100644
--- a/src/data/composite/wiki-data/withResolvedAnnotatedReferenceList.js
+++ b/src/data/composite/wiki-data/withResolvedAnnotatedReferenceList.js
@@ -1,9 +1,8 @@
 import {input, templateCompositeFrom} from '#composite';
 import {stitchArrays} from '#sugar';
-import {isString, optional, validateArrayItems, validateProperties}
-  from '#validators';
+import {isObject, validateArrayItems} from '#validators';
 
-import {withPropertiesFromList} from '#composite/data';
+import {withPropertyFromList} from '#composite/data';
 
 import {
   exitWithoutDependency,
@@ -21,16 +20,14 @@ export default templateCompositeFrom({
 
   inputs: {
     list: input({
-      validate:
-        validateArrayItems(
-          validateProperties({
-            reference: isString,
-            annotation: optional(isString),
-          })),
-
+      validate: validateArrayItems(isObject),
       acceptsNull: true,
     }),
 
+    reference: input({type: 'string', defaultValue: 'reference'}),
+    annotation: input({type: 'string', defaultValue: 'annotation'}),
+    thing: input({type: 'string', defaultValue: 'thing'}),
+
     data: inputWikiData({allowMixedTypes: true}),
     find: input({type: 'function'}),
 
@@ -53,16 +50,22 @@ export default templateCompositeFrom({
       }),
     }),
 
-    withPropertiesFromList({
+    withPropertyFromList({
+      list: input('list'),
+      property: input('reference'),
+    }).outputs({
+      ['#values']: '#references',
+    }),
+
+    withPropertyFromList({
       list: input('list'),
-      properties: input.value([
-        'reference',
-        'annotation',
-      ]),
+      property: input('annotation'),
+    }).outputs({
+      ['#values']: '#annotations',
     }),
 
     withResolvedReferenceList({
-      list: '#list.reference',
+      list: '#references',
       data: input('data'),
       find: input('find'),
       notFoundMode: input.value('null'),
@@ -70,18 +73,22 @@ export default templateCompositeFrom({
 
     {
       dependencies: [
+        input('thing'),
+        input('annotation'),
         '#resolvedReferenceList',
-        '#list.annotation',
+        '#annotations',
       ],
 
       compute: (continuation, {
-        ['#resolvedReferenceList']: thing,
-        ['#list.annotation']: annotation,
+        [input('thing')]: thingProperty,
+        [input('annotation')]: annotationProperty,
+        ['#resolvedReferenceList']: things,
+        ['#annotations']: annotations,
       }) => continuation({
         ['#matches']:
           stitchArrays({
-            thing,
-            annotation,
+            [thingProperty]: things,
+            [annotationProperty]: annotations,
           }),
       }),
     },
diff --git a/src/data/composite/wiki-properties/annotatedReferenceList.js b/src/data/composite/wiki-properties/annotatedReferenceList.js
index e8e5ac8c..a5e10c82 100644
--- a/src/data/composite/wiki-properties/annotatedReferenceList.js
+++ b/src/data/composite/wiki-properties/annotatedReferenceList.js
@@ -1,8 +1,15 @@
 import {input, templateCompositeFrom} from '#composite';
 import find from '#find';
-import {validateAnnotatedReferenceList} from '#validators';
 import {combineWikiDataArrays} from '#wiki-data';
 
+import {
+  isContentString,
+  optional,
+  validateArrayItems,
+  validateProperties,
+  validateReference,
+} from '#validators';
+
 import {exposeDependency} from '#composite/control-flow';
 import {inputWikiData, withResolvedAnnotatedReferenceList}
   from '#composite/wiki-data';
@@ -20,16 +27,36 @@ export default templateCompositeFrom({
 
     data: inputWikiData({allowMixedTypes: true}),
     find: input({type: 'function'}),
+
+    reference: input.staticValue({type: 'string', defaultValue: 'reference'}),
+    annotation: input.staticValue({type: 'string', defaultValue: 'annotation'}),
+    thing: input.staticValue({type: 'string', defaultValue: 'thing'}),
   },
 
-  update:
-    referenceListUpdateDescription({
-      validateReferenceList: validateAnnotatedReferenceList,
-    }),
+  update(staticInputs) {
+    const {
+      [input.staticValue('reference')]: referenceProperty,
+      [input.staticValue('annotation')]: annotationProperty,
+    } = staticInputs;
+
+    return referenceListUpdateDescription({
+      validateReferenceList: type =>
+        validateArrayItems(
+          validateProperties({
+            [referenceProperty]: validateReference(type),
+            [annotationProperty]: optional(isContentString),
+          })),
+    })(staticInputs);
+  },
 
   steps: () => [
     withResolvedAnnotatedReferenceList({
       list: input.updateValue(),
+
+      reference: input('reference'),
+      annotation: input('annotation'),
+      thing: input('thing'),
+
       data: input('data'),
       find: input('find'),
     }),
diff --git a/src/data/validators.js b/src/data/validators.js
index d2d1aa25..94d628c5 100644
--- a/src/data/validators.js
+++ b/src/data/validators.js
@@ -828,17 +828,6 @@ export function validateReferenceList(type) {
   return validateArrayItems(validateReference(type));
 }
 
-export function validateAnnotatedReference(type) {
-  return validateProperties({
-    reference: validateReference(type),
-    annotation: optional(isContentString),
-  });
-}
-
-export function validateAnnotatedReferenceList(type) {
-  return validateArrayItems(validateAnnotatedReference(type));
-}
-
 export function validateThing({
   referenceType: expectedReferenceType = '',
 } = {}) {
diff --git a/src/data/yaml.js b/src/data/yaml.js
index f7c36b89..0bc2e298 100644
--- a/src/data/yaml.js
+++ b/src/data/yaml.js
@@ -569,27 +569,31 @@ export function parseContributionPresets(list) {
   });
 }
 
-export function parseAnnotatedReferences(entries) {
+export function parseAnnotatedReferences(entries, {
+  referenceField = 'References',
+  annotationField = 'Annotation',
+  referenceProperty = 'reference',
+  annotationProperty = 'annotation',
+} = {}) {
   return parseArrayEntries(entries, item => {
-    if (typeof item === 'object' && item['References'])
+    if (typeof item === 'object' && item[referenceField])
       return {
-        reference: item['References'],
-        annotation: item['Annotation'] ?? null,
+        [referenceProperty]: item[referenceField],
+        [annotationProperty]: item[annotationField] ?? null,
       };
 
     if (typeof item !== 'string') return item;
 
     const match = item.match(extractAccentRegex);
-    if (!match) {
+    if (!match)
       return {
-        reference: item,
-        annotation: null,
-      }
-    }
+        [referenceProperty]: item,
+        [annotationProperty]: null,
+      };
 
     return {
-      reference: match.groups.main,
-      annotation: match.groups.accent,
+      [referenceProperty]: match.groups.main,
+      [annotationProperty]: match.groups.accent ?? null,
     };
   });
 }