« get me outta code hell

sugar, checks: withNestedProp+filterReferenceErrors: enter arrays - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/common-util/sugar.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-02-11 09:45:49 -0400
committer(quasar) nebula <qznebula@protonmail.com>2025-02-12 07:24:09 -0400
commit0eb70fd321af7bd2a52ea5b8a614321397f794c0 (patch)
treeae0f0169b97ce54abf89b746a68c0f05cb503f9b /src/common-util/sugar.js
parentd77095e5b559610b57690505fe1c467664782422 (diff)
sugar, checks: withNestedProp+filterReferenceErrors: enter arrays
Diffstat (limited to 'src/common-util/sugar.js')
-rw-r--r--src/common-util/sugar.js10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js
index 90d47b7c..89699f60 100644
--- a/src/common-util/sugar.js
+++ b/src/common-util/sugar.js
@@ -356,11 +356,19 @@ export function splitKeys(key) {
 }
 
 // Follows a key path like 'foo.bar.baz' to get an item nested deeply inside
-// an object.
+// an object. If a value partway through the chain is an array, the values
+// down the rest of the chain are gotten for each item in the array.
+//
+// obj: {x: [{y: ['a']}, {y: ['b', 'c']}]}
+// key: 'x.y'
+//   -> [['a'], ['b', 'c']]
+//
 export function getNestedProp(obj, key) {
   const recursive = (o, k) =>
     (k.length === 1
       ? o[k[0]]
+   : Array.isArray(o[k[0]])
+      ? o[k[0]].map(v => recursive(v, k.slice(1)))
       : recursive(o[k[0]], k.slice(1)));
 
   return recursive(obj, splitKeys(key));