diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-12-03 13:23:40 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-12-03 13:24:28 -0400 |
commit | db4698f9ce602227dc2c9aa79c409dd4ce508e7b (patch) | |
tree | 26838bdca100edb0b01b4007601c44c95cf8e9c2 /src/data/composite/data/withFilteredList.js | |
parent | 810acb84c379b76d3b8290bfd7d5971438999939 (diff) |
data: withFilteredList, withMappedList, withSortedList
God bless thine soul, these are not unit tested.
Diffstat (limited to 'src/data/composite/data/withFilteredList.js')
-rw-r--r-- | src/data/composite/data/withFilteredList.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/data/composite/data/withFilteredList.js b/src/data/composite/data/withFilteredList.js new file mode 100644 index 0000000..82e5690 --- /dev/null +++ b/src/data/composite/data/withFilteredList.js @@ -0,0 +1,50 @@ +// Applies a filter - an array of truthy and falsy values - to the index- +// corresponding items in a list. Items which correspond to a truthy value +// are kept, and the rest are excluded from the output list. +// +// TODO: It would be neat to apply an availability check here, e.g. to allow +// not providing a filter at all and performing the check on the contents of +// the list (though on the filter, if present, is fine too). But that's best +// done by some shmancy-fancy mapping support in composite.js, so a bit out +// of reach for now (apart from proving uses built on top of a more boring +// implementation). +// +// TODO: There should be two outputs - one for the items included according to +// the filter, and one for the items excluded. +// +// See also: +// - withMappedList +// - withSortedList +// +// More list utilities: +// - excludeFromList +// - fillMissingListItems +// - withFlattenedList, withUnflattenedList +// - withPropertyFromList, withPropertiesFromList +// + +import {input, templateCompositeFrom} from '#composite'; + +export default templateCompositeFrom({ + annotation: `withFilteredList`, + + inputs: { + list: input({type: 'array'}), + filter: input({type: 'array'}), + }, + + outputs: ['#filteredList'], + + steps: () => [ + { + dependencies: [input('list'), input('filter')], + compute: (continuation, { + [input('list')]: list, + [input('filter')]: filter, + }) => continuation({ + '#filteredList': + list.filter((item, index) => filter[index]), + }), + }, + ], +}); |