« get me outta code hell

html: clean up #addHelper-related logic - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-12-30 08:26:06 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-12-30 16:26:35 -0400
commit22e074bad12a25993c884b53f0d460a408efa814 (patch)
tree388210aecf2189508a5226a07fa17ea9f0f86bd8 /src/util
parent07a1bfe53ba583e25512fce1e678e9ef41908119 (diff)
html: clean up #addHelper-related logic
Diffstat (limited to 'src/util')
-rw-r--r--src/util/html.js58
1 files changed, 36 insertions, 22 deletions
diff --git a/src/util/html.js b/src/util/html.js
index 30960ca6..7ba49ae2 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -708,27 +708,44 @@ export class Attributes {
 
   #addHelper(...args) {
     if (args.length === 1) {
-      const arg = args[0];
-      if (arg === null || arg === undefined || arg === false) {
-        return;
-      } else if (Array.isArray(arg)) {
-        return arg.map(item => this.#addHelper(item));
-      } else if (arg instanceof Template) {
-        return this.#addHelper(Template.resolve(arg));
-      } else if (typeof arg === 'object') {
-        const results = {};
-        for (const key of Reflect.ownKeys(arg)) {
-          results[key] = this.#addHelper(key, arg[key]);
-        }
-        return results;
-      } else {
-        throw new Error(`Expected an array, object, or template, got ${typeAppearance(args[0])}`);
-      }
+      return this.#addMultipleAttributes(args[0]);
     } else if (args.length === 2) {
       return this.#addOneAttribute(args[0], args[1]);
     } else {
-      throw new Error(`Expected array or object, or attribute and value`);
+      throw new Error(
+        `Expected array or object, or attribute and value`);
+    }
+  }
+
+  #addMultipleAttributes(attributes) {
+    if (attributes === null) return;
+    if (attributes === undefined) return;
+    if (attributes === false) return;
+
+    if (Array.isArray(attributes)) {
+      return attributes.map(item => this.#addMultipleAttributes(item));
+    }
+
+    if (attributes instanceof Template) {
+      const resolved = Template.resolve(attributes);
+      isAttributesAdditionSingletValue(resolved);
+      return this.#addMultipleAttributes(resolved);
     }
+
+    if (typeof attributes === 'object') {
+      const results = {};
+
+      for (const key of Reflect.ownKeys(attributes)) {
+        const value = attributes[key];
+        results[key] = this.#addOneAttribute(key, value);
+      }
+
+      return results;
+    }
+
+    throw new Error(
+      `Expected an array, object, or template, ` +
+      `got ${typeAppearance(attribute)}`);
   }
 
   #addOneAttribute(attribute, value) {
@@ -741,8 +758,7 @@ export class Attributes {
     }
 
     if (!this.has(attribute)) {
-      this.set(attribute, value);
-      return value;
+      return this.set(attribute, value);
     }
 
     const descriptor = attributeSpec[attribute];
@@ -772,9 +788,7 @@ export class Attributes {
       }
     }
 
-    this.set(attribute, newValue);
-
-    return newValue;
+    return this.set(attribute, newValue);
   }
 
   get(attribute) {