« get me outta code hell

data: fix many validation errors - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/composite.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-09-20 17:33:50 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-09-20 17:33:50 -0300
commitcc4bf401f4d1df63ce33191ae82af6327c7da568 (patch)
treef66797c86a8d3470295463ef5e060fdd18c5c726 /src/data/things/composite.js
parente0cec3ff368175341526ff1b3c849f82e377b286 (diff)
data: fix many validation errors
Diffstat (limited to 'src/data/things/composite.js')
-rw-r--r--src/data/things/composite.js113
1 files changed, 70 insertions, 43 deletions
diff --git a/src/data/things/composite.js b/src/data/things/composite.js
index fbdc52f5..83879c54 100644
--- a/src/data/things/composite.js
+++ b/src/data/things/composite.js
@@ -1,10 +1,16 @@
 import {inspect} from 'node:util';
 
 import {colors} from '#cli';
-import {isArray, oneOf} from '#validators';
 import {TupleMap} from '#wiki-data';
 
 import {
+  isArray,
+  isWholeNumber,
+  oneOf,
+  validateArrayItems,
+} from '#validators';
+
+import {
   decorateErrorWithIndex,
   empty,
   filterProperties,
@@ -1876,72 +1882,93 @@ export const excludeFromList = templateCompositeFrom({
 // Flattens an array with one level of nested arrays, providing as dependencies
 // both the flattened array as well as the original starting indices of each
 // successive source array.
-export function withFlattenedArray({
-  from,
-  into = '#flattenedArray',
-  intoIndices = '#flattenedIndices',
-}) {
-  return {
-    annotation: `withFlattenedArray`,
-    flags: {expose: true, compose: true},
+export const withFlattenedList = templateCompositeFrom({
+  annotation: `withFlattenedList`,
 
-    expose: {
-      mapDependencies: {from},
-      mapContinuation: {into, intoIndices},
+  inputs: {
+    list: input({type: 'array'}),
+  },
 
-      compute({from: sourceArray}, continuation) {
-        const into = sourceArray.flat();
-        const intoIndices = [];
+  outputs: ['#flattenedList', '#flattenedIndices'],
 
+  steps: () => [
+    {
+      dependencies: [input('list')],
+      compute(continuation, {
+        [input('list')]: sourceList,
+      }) {
+        const flattenedList = sourceList.flat();
+        const indices = [];
         let lastEndIndex = 0;
         for (const {length} of sourceArray) {
-          intoIndices.push(lastEndIndex);
+          indices.push(lastEndIndex);
           lastEndIndex += length;
         }
 
-        return continuation({into, intoIndices});
+        return continuation({
+          ['#flattenedList']: flattenedList,
+          ['#flattenedIndices']: indices,
+        });
       },
     },
-  };
-}
+  ],
+});
 
 // After mapping the contents of a flattened array in-place (being careful to
 // retain the original indices by replacing unmatched results with null instead
 // of filtering them out), this function allows for recombining them. It will
 // filter out null and undefined items by default (pass {filter: false} to
 // disable this).
-export function withUnflattenedArray({
-  from,
-  fromIndices = '#flattenedIndices',
-  into = '#unflattenedArray',
-  filter = true,
-}) {
-  return {
-    annotation: `withUnflattenedArray`,
-    flags: {expose: true, compose: true},
+export const withUnflattenedList = templateCompositeFrom({
+  annotation: `withUnflattenedList`,
 
-    expose: {
-      mapDependencies: {from, fromIndices},
-      mapContinuation: {into},
-      compute({from, fromIndices}, continuation) {
-        const arrays = [];
+  inputs: {
+    list: input({
+      type: 'array',
+      defaultDependency: '#flattenedList',
+    }),
+
+    indices: input({
+      validate: validateArrayItems(isWholeNumber),
+      defaultDependency: '#flattenedIndices',
+    }),
+
+    filter: input({
+      type: 'boolean',
+      defaultValue: true,
+    }),
+  },
 
-        for (let i = 0; i < fromIndices.length; i++) {
-          const startIndex = fromIndices[i];
+  outputs: ['#unflattenedList'],
+
+  steps: () => [
+    {
+      dependencies: [input('list'), input('indices')],
+      compute({
+        [input('list')]: list,
+        [input('indices')]: indices,
+        [input('filter')]: filter,
+      }) {
+        const unflattenedList = [];
+
+        for (let i = 0; i < indices.length; i++) {
+          const startIndex = indices[i];
           const endIndex =
-            (i === fromIndices.length - 1
-              ? from.length
-              : fromIndices[i + 1]);
+            (i === indices.length - 1
+              ? list.length
+              : indices[i + 1]);
 
-          const values = from.slice(startIndex, endIndex);
-          arrays.push(
+          const values = list.slice(startIndex, endIndex);
+          unflattenedList.push(
             (filter
               ? values.filter(value => value !== null && value !== undefined)
               : values));
         }
 
-        return continuation({into: arrays});
+        return continuation({
+          ['#unflattenedList']: unflattenedList,
+        });
       },
     },
-  };
-}
+  ],
+});