diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2025-05-01 13:59:37 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-05-06 12:29:05 -0300 |
commit | 608f493d62ab788126fea7d53e2311a9d00e41fb (patch) | |
tree | 890c212aef234f8d29e52ff6369fe12e8732d705 /src/data | |
parent | aefa7862874e51c07d265e8505ead5662d2158e1 (diff) |
data: withLengthOfList
Diffstat (limited to 'src/data')
-rw-r--r-- | src/data/composite/data/index.js | 1 | ||||
-rw-r--r-- | src/data/composite/data/withLengthOfList.js | 55 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/data/composite/data/index.js b/src/data/composite/data/index.js index 46a3dc81..05b59445 100644 --- a/src/data/composite/data/index.js +++ b/src/data/composite/data/index.js @@ -20,6 +20,7 @@ export {default as withMappedList} from './withMappedList.js'; export {default as withSortedList} from './withSortedList.js'; export {default as withStretchedList} from './withStretchedList.js'; +export {default as withLengthOfList} from './withLengthOfList.js'; export {default as withPropertyFromList} from './withPropertyFromList.js'; export {default as withPropertiesFromList} from './withPropertiesFromList.js'; diff --git a/src/data/composite/data/withLengthOfList.js b/src/data/composite/data/withLengthOfList.js new file mode 100644 index 00000000..efc3ecae --- /dev/null +++ b/src/data/composite/data/withLengthOfList.js @@ -0,0 +1,55 @@ +import {input, templateCompositeFrom} from '#composite'; +import {stitchArrays} from '#sugar'; + +function getOutputName({ + [input.staticDependency('list')]: list, +}) { + if (list && list.startsWith('#')) { + return `${list}.length`; + } else if (list) { + return `#${list}.length`; + } else { + return '#length'; + } +} + +export default templateCompositeFrom({ + annotation: `withMappedList`, + + inputs: { + list: input({type: 'array'}), + }, + + outputs: inputs => [getOutputName(inputs)], + + steps: () => [ + { + dependencies: [input.staticDependency('list')], + compute: (continuation, inputs) => + continuation({'#output': getOutputName(inputs)}), + }, + + { + dependencies: [input('list')], + compute: (continuation, { + [input('list')]: list, + }) => continuation({ + ['#value']: + (list === null + ? null + : list.length), + }), + }, + + { + dependencies: ['#output', '#value'], + + compute: (continuation, { + ['#output']: output, + ['#value']: value, + }) => continuation({ + [output]: value, + }), + }, + ], +}); |