diff options
-rw-r--r-- | src/data/composite/wiki-data/index.js | 2 | ||||
-rw-r--r-- | src/data/composite/wiki-data/withDirectory.js | 55 | ||||
-rw-r--r-- | src/data/composite/wiki-data/withDirectoryFromName.js | 42 | ||||
-rw-r--r-- | src/data/composite/wiki-properties/directory.js | 48 |
4 files changed, 128 insertions, 19 deletions
diff --git a/src/data/composite/wiki-data/index.js b/src/data/composite/wiki-data/index.js index b4cf6d13..15ebaffa 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'; diff --git a/src/data/composite/wiki-data/withDirectory.js b/src/data/composite/wiki-data/withDirectory.js new file mode 100644 index 00000000..b08b6153 --- /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 00000000..034464e4 --- /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-properties/directory.js b/src/data/composite/wiki-properties/directory.js index 0b2181c9..41ce4b27 100644 --- a/src/data/composite/wiki-properties/directory.js +++ b/src/data/composite/wiki-properties/directory.js @@ -2,22 +2,32 @@ // almost any data object. Also corresponds to a part of the URL which pages of // such objects are visited at. -import {isDirectory} from '#validators'; -import {getKebabCase} from '#wiki-data'; - -// TODO: Not templateCompositeFrom. - -export default function() { - return { - flags: {update: true, expose: true}, - update: {validate: isDirectory}, - expose: { - dependencies: ['name'], - transform(directory, {name}) { - if (directory === null && name === null) return null; - else if (directory === null) return getKebabCase(name); - else return directory; - }, - }, - }; -} +import {input, templateCompositeFrom} from '#composite'; + +import {isDirectory, isName} from '#validators'; + +import {exposeDependency} from '#composite/control-flow'; +import {withDirectory} from '#composite/wiki-data'; + +export default templateCompositeFrom({ + annotation: `directory`, + + compose: false, + + inputs: { + name: input({ + validate: isName, + defaultDependency: 'name', + }), + }, + + steps: () => [ + withDirectory({ + directory: input.updateValue({validate: isDirectory}), + }), + + exposeDependency({ + dependency: '#directory', + }), + ], +}); |