« get me outta code hell

data: refactor {missing} out of withPropertyFrom{Object,List} - 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-09 08:26:33 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-09-09 08:26:33 -0300
commit726118e7e8eefa9002562ca2dd0a4f6deb8a05b9 (patch)
tree30ac5f60bc8a1e3ab944e6ec64dcbf513d2d1b1a
parent57d07a308dfee6d16b49f7c009853b1789597e82 (diff)
data: refactor {missing} out of withPropertyFrom{Object,List}
-rw-r--r--src/data/things/composite.js28
1 files changed, 11 insertions, 17 deletions
diff --git a/src/data/things/composite.js b/src/data/things/composite.js
index a5adc3e9..b37b8e31 100644
--- a/src/data/things/composite.js
+++ b/src/data/things/composite.js
@@ -1090,10 +1090,8 @@ export function withUpdateValueAsDependency({
 }
 
 // Gets a property of some object (in a dependency) and provides that value.
-// If the object itself is null, the provided dependency will also always be
-// null. By default, it'll also be null if the object doesn't have the listed
-// property (or its value is undefined/null); provide a value on {missing} to
-// default to something else here.
+// If the object itself is null, or the object doesn't have the listed property,
+// the provided dependency will also be null.
 export function withPropertyFromObject({
   object,
   property,
@@ -1113,11 +1111,10 @@ export function withPropertyFromObject({
       mapContinuation: {into},
       options: {property},
 
-      compute({object, '#options': {property}}, continuation) {
-        if (object === null || object === undefined) return continuation({into: null});
-        if (!Object.hasOwn(object, property)) return continuation({into: null});
-        return continuation({into: object[property] ?? null});
-      },
+      compute: ({object, '#options': {property}}, continuation) =>
+        (object === null || object === undefined
+          ? continuation({into: null})
+          : continuation({into: object[property] ?? null})),
     },
   };
 }
@@ -1125,13 +1122,11 @@ export function withPropertyFromObject({
 // 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
 // which were null in the original list are kept null here. Objects which don't
-// have the specified property are also included in-place as null, by default;
-// provide a value on {missing} to default to something else here.
+// have the specified property are retained in-place as null.
 export function withPropertyFromList({
   list,
   property,
   into = null,
-  missing = null,
 }) {
   into ??=
     (list.startsWith('#')
@@ -1154,11 +1149,10 @@ export function withPropertyFromList({
 
         return continuation({
           into:
-            list.map(item => {
-              if (item === null || item === undefined) return null;
-              if (!Object.hasOwn(item, property)) return missing;
-              return item[property] ?? missing;
-            }),
+            list.map(item =>
+              (item === null || item === undefined
+                ? null
+                : item[property] ?? null)),
         });
       },
     },