« get me outta code hell

data: reverseSingleReferenceList - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite/wiki-data
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-09-04 12:45:11 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-09-04 12:46:21 -0300
commitb0444ca365a7f1165a3aef95a85ad1f380ffc591 (patch)
tree7a27261b4b6dfab418e12b03261ec86aacbefbd9 /src/data/composite/wiki-data
parenta46db43a47928bfa9bb48ee31b521f115b754566 (diff)
data: reverseSingleReferenceList
Diffstat (limited to 'src/data/composite/wiki-data')
-rw-r--r--src/data/composite/wiki-data/index.js1
-rw-r--r--src/data/composite/wiki-data/withReverseSingleReferenceList.js50
2 files changed, 51 insertions, 0 deletions
diff --git a/src/data/composite/wiki-data/index.js b/src/data/composite/wiki-data/index.js
index 5f17ca3a..d9112467 100644
--- a/src/data/composite/wiki-data/index.js
+++ b/src/data/composite/wiki-data/index.js
@@ -19,5 +19,6 @@ export {default as withResolvedReference} from './withResolvedReference.js';
 export {default as withResolvedReferenceList} from './withResolvedReferenceList.js';
 export {default as withReverseContributionList} from './withReverseContributionList.js';
 export {default as withReverseReferenceList} from './withReverseReferenceList.js';
+export {default as withReverseSingleReferenceList} from './withReverseSingleReferenceList.js';
 export {default as withThingsSortedAlphabetically} from './withThingsSortedAlphabetically.js';
 export {default as withUniqueReferencingThing} from './withUniqueReferencingThing.js';
diff --git a/src/data/composite/wiki-data/withReverseSingleReferenceList.js b/src/data/composite/wiki-data/withReverseSingleReferenceList.js
new file mode 100644
index 00000000..dd97dc66
--- /dev/null
+++ b/src/data/composite/wiki-data/withReverseSingleReferenceList.js
@@ -0,0 +1,50 @@
+// Like withReverseReferenceList, but for finding all things which reference
+// the current thing by a property that contains a single reference, rather
+// than within a reference list.
+
+import withReverseList_template from './helpers/withReverseList-template.js';
+
+import {input} from '#composite';
+
+import {withMappedList} from '#composite/data';
+
+export default withReverseList_template({
+  annotation: `withReverseSingleReferenceList`,
+
+  propertyInputName: 'ref',
+  outputName: '#reverseSingleReferenceList',
+
+  customCompositionSteps: () => [
+    {
+      dependencies: [input('data')],
+      compute: (continuation, {
+        [input('data')]: data,
+      }) => continuation({
+        ['#referencingThings']:
+          data,
+      }),
+    },
+
+    // This map wraps each referenced thing in a single-item array.
+    // Each referencing thing references exactly one thing, if any.
+    {
+      dependencies: [input('ref')],
+      compute: (continuation, {
+        [input('ref')]: ref,
+      }) => continuation({
+        ['#singleReferenceMap']:
+          thing =>
+            (thing[ref]
+              ? [thing[ref]]
+              : []),
+      }),
+    },
+
+    withMappedList({
+      list: '#referencingThings',
+      map: '#singleReferenceMap',
+    }).outputs({
+      '#mappedList': '#referencedThings',
+    }),
+  ],
+});