« get me outta code hell

data: update withPropertiesFromObject - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-09-21 14:20:19 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-09-21 14:20:19 -0300
commit6ca1e4b3ad691478e94f09cfe94683cb079f6bdf (patch)
tree92c84b42babaff9b128b4ef04a1390ac8ee00ce9
parentcd73f85962f542f9b44feb2a7616bc0d9aac797b (diff)
data: update withPropertiesFromObject
-rw-r--r--src/data/things/composite.js103
1 files changed, 77 insertions, 26 deletions
diff --git a/src/data/things/composite.js b/src/data/things/composite.js
index cfa557d..4be01a5 100644
--- a/src/data/things/composite.js
+++ b/src/data/things/composite.js
@@ -1764,34 +1764,85 @@ export const withPropertyFromObject = templateCompositeFrom({
 // as a dependency prefixed with the same name as the object (by default).
 // If the object itself is null, all provided dependencies will be null;
 // if it's missing only select properties, those will be provided as null.
-export function withPropertiesFromObject({
-  object,
-  properties,
-  prefix =
-    (object.startsWith('#')
-      ? object
-      : `#${object}`),
-}) {
-  return {
-    annotation: `withPropertiesFromObject`,
-    flags: {expose: true, compose: true},
+export const withPropertiesFromObject = templateCompositeFrom({
+  annotation: `withPropertiesFromObject`,
 
-    expose: {
-      mapDependencies: {object},
-      options: {prefix, properties},
+  inputs: {
+    object: input({
+      type: 'object',
+      null: true,
+    }),
 
-      compute: (continuation, {object, '#options': {prefix, properties}}) =>
-        continuation(
-          Object.fromEntries(
-            properties.map(property => [
-              `${prefix}.${property}`,
-              (object === null || object === undefined
-                ? null
-                : object[property] ?? null),
-            ]))),
+    properties: input({
+      validate: validateArrayItems(isString),
+    }),
+
+    prefix: input.staticValue({
+      type: 'string',
+      null: true,
+    }),
+  },
+
+  outputs: ({
+    [input.staticDependency('object')]: object,
+    [input.staticValue('properties')]: properties,
+    [input.staticValue('prefix')]: prefix,
+  }) =>
+    (properties
+      ? properties.map(property =>
+          (prefix
+            ? `${prefix}.${property}`
+         : object
+            ? `${object}.${property}`
+            : `#object.${property}`))
+      : '#object'),
+
+  steps: () => [
+    {
+      dependencies: [input('object'), input('properties')],
+      compute: (continuation, {
+        [input('object')]: object,
+        [input('properties')]: properties,
+      }) => continuation({
+        ['#entries']:
+          (object === null
+            ? properties.map(property => [property, null])
+            : properties.map(property => [property, object[property]])),
+      }),
     },
-  };
-}
+
+    {
+      dependencies: [
+        input.staticDependency('object'),
+        input.staticValue('properties'),
+        input.staticValue('prefix'),
+        '#entries',
+      ],
+
+      compute: ({
+        [input.staticDependency('object')]: object,
+        [input.staticValue('properties')]: properties,
+        [input.staticValue('prefix')]: prefix,
+        ['#entries']: entries,
+      }) =>
+        (properties
+          ? continuation(
+              Object.fromEntries(
+                entries.map(([property, value]) => [
+                  (prefix
+                    ? `${prefix}.${property}`
+                 : object
+                    ? `${object}.${property}`
+                    : `#object.${property}`),
+                  value ?? null,
+                ])))
+          : continuation({
+              ['#object']:
+                Object.fromEntries(entries),
+            })),
+    },
+  ],
+});
 
 // Gets a property from each of a list of objects (in a dependency) and
 // provides the results. This doesn't alter any list indices, so positions
@@ -1846,7 +1897,7 @@ export const withPropertiesFromList = templateCompositeFrom({
       validate: validateArrayItems(isString),
     }),
 
-    prefix: input({
+    prefix: input.staticValue({
       type: 'string',
       null: true,
     }),