From b0444ca365a7f1165a3aef95a85ad1f380ffc591 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 4 Sep 2024 12:45:11 -0300 Subject: data: reverseSingleReferenceList --- src/data/composite/wiki-data/index.js | 1 + .../wiki-data/withReverseSingleReferenceList.js | 50 ++++++++++++++++++++++ src/data/composite/wiki-properties/index.js | 1 + .../wiki-properties/reverseSingleReferenceList.js | 24 +++++++++++ 4 files changed, 76 insertions(+) create mode 100644 src/data/composite/wiki-data/withReverseSingleReferenceList.js create mode 100644 src/data/composite/wiki-properties/reverseSingleReferenceList.js 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', + }), + ], +}); diff --git a/src/data/composite/wiki-properties/index.js b/src/data/composite/wiki-properties/index.js index e35948e7..adfe4a9b 100644 --- a/src/data/composite/wiki-properties/index.js +++ b/src/data/composite/wiki-properties/index.js @@ -21,6 +21,7 @@ export {default as name} from './name.js'; export {default as referenceList} from './referenceList.js'; export {default as reverseContributionList} from './reverseContributionList.js'; export {default as reverseReferenceList} from './reverseReferenceList.js'; +export {default as reverseSingleReferenceList} from './reverseSingleReferenceList.js'; export {default as simpleDate} from './simpleDate.js'; export {default as simpleString} from './simpleString.js'; export {default as singleReference} from './singleReference.js'; diff --git a/src/data/composite/wiki-properties/reverseSingleReferenceList.js b/src/data/composite/wiki-properties/reverseSingleReferenceList.js new file mode 100644 index 00000000..d180b12d --- /dev/null +++ b/src/data/composite/wiki-properties/reverseSingleReferenceList.js @@ -0,0 +1,24 @@ +import {input, templateCompositeFrom} from '#composite'; + +import {exposeDependency} from '#composite/control-flow'; +import {inputWikiData, withReverseSingleReferenceList} from '#composite/wiki-data'; + +export default templateCompositeFrom({ + annotation: `reverseSingleReferenceList`, + + compose: false, + + inputs: { + data: inputWikiData({allowMixedTypes: false}), + ref: input({type: 'string'}), + }, + + steps: () => [ + withReverseSingleReferenceList({ + data: input('data'), + ref: input('ref'), + }), + + exposeDependency({dependency: '#reverseSingleReferenceList'}), + ], +}); -- cgit 1.3.0-6-gf8a5