« get me outta code hell

data: withStretchedList - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-11-16 15:38:43 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-11-17 13:40:00 -0400
commit3528fb20886fd0f0490f7cf3c26ae51fb3017727 (patch)
treec3c0ec16725b8adc9a5bc2887f41493a69d54c47 /src/data/composite
parent618004adc31322f6dec3a8608d45854d734fbfe3 (diff)
data: withStretchedList
Diffstat (limited to 'src/data/composite')
-rw-r--r--src/data/composite/data/index.js1
-rw-r--r--src/data/composite/data/withStretchedList.js36
2 files changed, 37 insertions, 0 deletions
diff --git a/src/data/composite/data/index.js b/src/data/composite/data/index.js
index c80bb350..46a3dc81 100644
--- a/src/data/composite/data/index.js
+++ b/src/data/composite/data/index.js
@@ -18,6 +18,7 @@ export {default as withUniqueItemsOnly} from './withUniqueItemsOnly.js';
 export {default as withFilteredList} from './withFilteredList.js';
 export {default as withMappedList} from './withMappedList.js';
 export {default as withSortedList} from './withSortedList.js';
+export {default as withStretchedList} from './withStretchedList.js';
 
 export {default as withPropertyFromList} from './withPropertyFromList.js';
 export {default as withPropertiesFromList} from './withPropertiesFromList.js';
diff --git a/src/data/composite/data/withStretchedList.js b/src/data/composite/data/withStretchedList.js
new file mode 100644
index 00000000..46733064
--- /dev/null
+++ b/src/data/composite/data/withStretchedList.js
@@ -0,0 +1,36 @@
+// Repeats each item in a list in-place by a corresponding length.
+
+import {input, templateCompositeFrom} from '#composite';
+import {repeat, stitchArrays} from '#sugar';
+import {isNumber, validateArrayItems} from '#validators';
+
+export default templateCompositeFrom({
+  annotation: `withStretchedList`,
+
+  inputs: {
+    list: input({type: 'array'}),
+
+    lengths: input({
+      validate: validateArrayItems(isNumber),
+    }),
+  },
+
+  outputs: ['#stretchedList'],
+
+  steps: () => [
+    {
+      dependencies: [input('list'), input('lengths')],
+      compute: (continuation, {
+        [input('list')]: list,
+        [input('lengths')]: lengths,
+      }) => continuation({
+        ['#stretchedList']:
+          stitchArrays({
+            item: list,
+            length: lengths,
+          }).map(({item, length}) => repeat(length, [item]))
+            .flat(),
+      }),
+    },
+  ],
+});