« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/data/composite/data/withMappedList.js20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/data/composite/data/withMappedList.js b/src/data/composite/data/withMappedList.js
index 0bc63a92..cd32058e 100644
--- a/src/data/composite/data/withMappedList.js
+++ b/src/data/composite/data/withMappedList.js
@@ -1,12 +1,16 @@
 // Applies a map function to each item in a list, just like a normal JavaScript
 // map.
 //
+// Pass a filter (e.g. from withAvailabilityFilter) to process only items
+// kept by the filter. Other items will be left as-is.
+//
 // See also:
 //  - withFilteredList
 //  - withSortedList
 //
 
 import {input, templateCompositeFrom} from '#composite';
+import {stitchArrays} from '#sugar';
 
 export default templateCompositeFrom({
   annotation: `withMappedList`,
@@ -14,19 +18,31 @@ export default templateCompositeFrom({
   inputs: {
     list: input({type: 'array'}),
     map: input({type: 'function'}),
+
+    filter: input({
+      type: 'array',
+      defaultValue: null,
+    }),
   },
 
   outputs: ['#mappedList'],
 
   steps: () => [
     {
-      dependencies: [input('list'), input('map')],
+      dependencies: [input('list'), input('map'), input('filter')],
       compute: (continuation, {
         [input('list')]: list,
         [input('map')]: mapFn,
+        [input('filter')]: filter,
       }) => continuation({
         ['#mappedList']:
-          list.map(mapFn),
+          stitchArrays({
+            item: list,
+            keep: filter ?? Array.from(list, () => true),
+          }).map(({item, keep}, index) =>
+              (keep
+                ? mapFn(item, index, list)
+                : item)),
       }),
     },
   ],