diff options
Diffstat (limited to 'src/data/composite/wiki-data')
7 files changed, 183 insertions, 14 deletions
diff --git a/src/data/composite/wiki-data/index.js b/src/data/composite/wiki-data/index.js index 3ccfa75..15ebaff 100644 --- a/src/data/composite/wiki-data/index.js +++ b/src/data/composite/wiki-data/index.js @@ -6,6 +6,8 @@ export {default as exitWithoutContribs} from './exitWithoutContribs.js'; export {default as inputWikiData} from './inputWikiData.js'; +export {default as withDirectory} from './withDirectory.js'; +export {default as withDirectoryFromName} from './withDirectoryFromName.js'; export {default as withParsedCommentaryEntries} from './withParsedCommentaryEntries.js'; export {default as withResolvedContribs} from './withResolvedContribs.js'; export {default as withResolvedReference} from './withResolvedReference.js'; @@ -13,3 +15,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/withDirectory.js b/src/data/composite/wiki-data/withDirectory.js new file mode 100644 index 0000000..b08b615 --- /dev/null +++ b/src/data/composite/wiki-data/withDirectory.js @@ -0,0 +1,55 @@ +// Select a directory, either using a manually specified directory, or +// computing it from a name. By default these values are the current thing's +// 'directory' and 'name' properties, so it can be used without any options +// to get the current thing's effective directory (assuming no custom rules). + +import {input, templateCompositeFrom} from '#composite'; + +import {isDirectory, isName} from '#validators'; + +import {withResultOfAvailabilityCheck} from '#composite/control-flow'; + +import withDirectoryFromName from './withDirectoryFromName.js'; + +export default templateCompositeFrom({ + annotation: `withDirectory`, + + inputs: { + directory: input({ + validate: isDirectory, + defaultDependency: 'directory', + acceptsNull: true, + }), + + name: input({ + validate: isName, + defaultDependency: 'name', + acceptsNull: true, + }), + }, + + outputs: ['#directory'], + + steps: () => [ + withResultOfAvailabilityCheck({ + from: input('directory'), + }), + + { + dependencies: ['#availability', input('directory')], + compute: (continuation, { + ['#availability']: availability, + [input('directory')]: directory, + }) => + (availability + ? continuation.raiseOutput({ + ['#directory']: directory + }) + : continuation()), + }, + + withDirectoryFromName({ + name: input('name'), + }), + ], +}); diff --git a/src/data/composite/wiki-data/withDirectoryFromName.js b/src/data/composite/wiki-data/withDirectoryFromName.js new file mode 100644 index 0000000..034464e --- /dev/null +++ b/src/data/composite/wiki-data/withDirectoryFromName.js @@ -0,0 +1,42 @@ +// Compute a directory from a name - by default the current thing's own name. + +import {input, templateCompositeFrom} from '#composite'; + +import {isName} from '#validators'; +import {getKebabCase} from '#wiki-data'; + +import {raiseOutputWithoutDependency} from '#composite/control-flow'; + +export default templateCompositeFrom({ + annotation: `withDirectoryFromName`, + + inputs: { + name: input({ + validate: isName, + defaultDependency: 'name', + acceptsNull: true, + }), + }, + + outputs: ['#directory'], + + steps: () => [ + raiseOutputWithoutDependency({ + dependency: input('name'), + mode: input.value('falsy'), + output: input.value({ + ['#directory']: null, + }), + }), + + { + dependencies: [input('name')], + compute: (continuation, { + [input('name')]: name, + }) => continuation({ + ['#directory']: + getKebabCase(name), + }), + }, + ], +}); diff --git a/src/data/composite/wiki-data/withResolvedContribs.js b/src/data/composite/wiki-data/withResolvedContribs.js index 77b0f96..9526638 100644 --- a/src/data/composite/wiki-data/withResolvedContribs.js +++ b/src/data/composite/wiki-data/withResolvedContribs.js @@ -1,7 +1,8 @@ // Resolves the contribsByRef contained in the provided dependency, // providing (named by the second argument) the result. "Resolving" -// means mapping the "who" reference of each contribution to an artist -// object, and filtering out those whose "who" doesn't match any artist. +// means mapping the artist reference of each contribution to an artist +// object, and filtering out those whose artist reference doesn't match +// any artist. import {input, templateCompositeFrom} from '#composite'; import find from '#find'; @@ -46,29 +47,30 @@ export default templateCompositeFrom({ withPropertiesFromList({ list: input('from'), - properties: input.value(['who', 'what']), + properties: input.value(['artist', 'annotation']), prefix: input.value('#contribs'), }), withResolvedReferenceList({ - list: '#contribs.who', + list: '#contribs.artist', data: 'artistData', find: input.value(find.artist), notFoundMode: input('notFoundMode'), }).outputs({ - ['#resolvedReferenceList']: '#contribs.who', + ['#resolvedReferenceList']: '#contribs.artist', }), { - dependencies: ['#contribs.who', '#contribs.what'], + dependencies: ['#contribs.artist', '#contribs.annotation'], compute(continuation, { - ['#contribs.who']: who, - ['#contribs.what']: what, + ['#contribs.artist']: artist, + ['#contribs.annotation']: annotation, }) { - filterMultipleArrays(who, what, (who, _what) => who); + filterMultipleArrays(artist, annotation, (artist, _annotation) => artist); return continuation({ - ['#resolvedContribs']: stitchArrays({who, what}), + ['#resolvedContribs']: + stitchArrays({artist, annotation}), }); }, }, diff --git a/src/data/composite/wiki-data/withReverseContributionList.js b/src/data/composite/wiki-data/withReverseContributionList.js index eccb58b..91e125e 100644 --- a/src/data/composite/wiki-data/withReverseContributionList.js +++ b/src/data/composite/wiki-data/withReverseContributionList.js @@ -11,7 +11,8 @@ import {input, templateCompositeFrom} from '#composite'; -import {exitWithoutDependency} from '#composite/control-flow'; +import {exitWithoutDependency, raiseOutputWithoutDependency} + from '#composite/control-flow'; import inputWikiData from './inputWikiData.js'; @@ -32,10 +33,17 @@ export default templateCompositeFrom({ outputs: ['#reverseContributionList'], steps: () => [ + // Early exit with an empty array if the data list isn't available. exitWithoutDependency({ dependency: input('data'), value: input.value([]), + }), + + // Raise an empty array (don't early exit) if the data list is empty. + raiseOutputWithoutDependency({ + dependency: input('data'), mode: input.value('empty'), + output: input.value({'#reverseContributionList': []}), }), { @@ -58,10 +66,10 @@ export default templateCompositeFrom({ for (const referencingThing of data) { const referenceList = referencingThing[list]; - // Destructuring {who} is the only unique part of the + // Destructuring {artist} is the only unique part of the // withReverseContributionList implementation, compared to // withReverseReferneceList. - for (const {who: referencedThing} of referenceList) { + for (const {artist: referencedThing} of referenceList) { if (cacheRecord.has(referencedThing)) { cacheRecord.get(referencedThing).push(referencingThing); } else { diff --git a/src/data/composite/wiki-data/withReverseReferenceList.js b/src/data/composite/wiki-data/withReverseReferenceList.js index 2d7a421..8cd540a 100644 --- a/src/data/composite/wiki-data/withReverseReferenceList.js +++ b/src/data/composite/wiki-data/withReverseReferenceList.js @@ -13,7 +13,8 @@ import {input, templateCompositeFrom} from '#composite'; -import {exitWithoutDependency} from '#composite/control-flow'; +import {exitWithoutDependency, raiseOutputWithoutDependency} + from '#composite/control-flow'; import inputWikiData from './inputWikiData.js'; @@ -34,10 +35,17 @@ export default templateCompositeFrom({ outputs: ['#reverseReferenceList'], steps: () => [ + // Early exit with an empty array if the data list isn't available. exitWithoutDependency({ dependency: input('data'), value: input.value([]), + }), + + // Raise an empty array (don't early exit) if the data list is empty. + raiseOutputWithoutDependency({ + dependency: input('data'), mode: input.value('empty'), + output: input.value({'#reverseReferenceList': []}), }), { diff --git a/src/data/composite/wiki-data/withUniqueReferencingThing.js b/src/data/composite/wiki-data/withUniqueReferencingThing.js new file mode 100644 index 0000000..61c1061 --- /dev/null +++ b/src/data/composite/wiki-data/withUniqueReferencingThing.js @@ -0,0 +1,51 @@ +// 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: () => [ + // Early exit with null (not an empty array) if the data list + // isn't available. + exitWithoutDependency({ + dependency: input('data'), + }), + + 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], + }), + }, + ], +}); |