From 0792908781a1956cd44038498bbd68f2bfdb8396 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 3 Apr 2024 13:16:16 -0300 Subject: data: withUniqueReferencingThing --- src/data/composite/wiki-data/index.js | 1 + .../wiki-data/withUniqueReferencingThing.js | 52 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/data/composite/wiki-data/withUniqueReferencingThing.js (limited to 'src/data/composite/wiki-data') diff --git a/src/data/composite/wiki-data/index.js b/src/data/composite/wiki-data/index.js index 3ccfa75d..b4cf6d13 100644 --- a/src/data/composite/wiki-data/index.js +++ b/src/data/composite/wiki-data/index.js @@ -13,3 +13,4 @@ export {default as withResolvedReferenceList} from './withResolvedReferenceList. export {default as withReverseContributionList} from './withReverseContributionList.js'; export {default as withReverseReferenceList} from './withReverseReferenceList.js'; export {default as withThingsSortedAlphabetically} from './withThingsSortedAlphabetically.js'; +export {default as withUniqueReferencingThing} from './withUniqueReferencingThing.js'; diff --git a/src/data/composite/wiki-data/withUniqueReferencingThing.js b/src/data/composite/wiki-data/withUniqueReferencingThing.js new file mode 100644 index 00000000..ce04f838 --- /dev/null +++ b/src/data/composite/wiki-data/withUniqueReferencingThing.js @@ -0,0 +1,52 @@ +// Like withReverseReferenceList, but this is specifically for special "unique" +// references, meaning this thing is referenced by exactly one or zero things +// in the data list. + +import {input, templateCompositeFrom} from '#composite'; + +import {exitWithoutDependency, raiseOutputWithoutDependency} + from '#composite/control-flow'; + +import inputWikiData from './inputWikiData.js'; +import withReverseReferenceList from './withReverseReferenceList.js'; + +export default templateCompositeFrom({ + annotation: `withUniqueReferencingThing`, + + inputs: { + data: inputWikiData({allowMixedTypes: false}), + list: input({type: 'string'}), + }, + + outputs: ['#uniqueReferencingThing'], + + steps: () => [ + // withReverseRefernceList does this check too, but it early exits with + // an empty array. That's no good here! + exitWithoutDependency({ + dependency: input('data'), + mode: input.value('empty'), + }), + + withReverseReferenceList({ + data: input('data'), + list: input('list'), + }), + + raiseOutputWithoutDependency({ + dependency: '#reverseReferenceList', + mode: input.value('empty'), + output: input.value({'#uniqueReferencingThing': null}), + }), + + { + dependencies: ['#reverseReferenceList'], + compute: (continuation, { + ['#reverseReferenceList']: reverseReferenceList, + }) => continuation({ + ['#uniqueReferencingThing']: + reverseReferenceList[0], + }), + }, + ], +}); -- cgit 1.3.0-6-gf8a5