« get me outta code hell

html: simplify attributes validation and add() 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>2024-01-03 20:20:13 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-01-03 20:20:13 -0400
commitd83f4834af5ac18759c2e2c3ad4bb2c7e4900bee (patch)
tree692b717a7a6ae9b6cb4a8331d4af2c48fc73bf6f /src/util
parent1864ecf0c1375b8115e4bc2bad8b1dc41150444c (diff)
html: simplify attributes validation and add() logic
Diffstat (limited to 'src/util')
-rw-r--r--src/util/html.js49
1 files changed, 17 insertions, 32 deletions
diff --git a/src/util/html.js b/src/util/html.js
index b5f6e70..9dfd03e 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -122,7 +122,7 @@ export const validators = {
   },
 
   isAttributes(value) {
-    return isAttributesAdditionSingletValue(value);
+    return isAttributesAdditionSinglet(value);
   },
 };
 
@@ -645,18 +645,18 @@ export class Attributes {
   }
 
   add(...args) {
-    isAttributesAddition(args);
-    return this.#addHelper(...args);
-  }
+    switch (args.length) {
+      case 1:
+        isAttributesAdditionSinglet(args[0]);
+        return this.#addMultipleAttributes(args[0]);
 
-  #addHelper(...args) {
-    if (args.length === 1) {
-      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`);
+      case 2:
+        isAttributesAdditionPair(args);
+        return this.#addOneAttribute(args[0], args[1]);
+
+      default:
+        throw new Error(
+          `Expected array or object, or attribute and value`);
     }
   }
 
@@ -690,7 +690,7 @@ export class Attributes {
 
     if (attributes instanceof Template) {
       const resolved = Template.resolve(attributes);
-      isAttributesAdditionSingletValue(resolved);
+      isAttributesAdditionSinglet(resolved);
       return resolved;
     }
 
@@ -1177,7 +1177,7 @@ export class Template {
         }
 
         case 'attributes': {
-          return isAttributesAdditionSingletValue(value);
+          return isAttributesAdditionSinglet(value);
         }
 
         case 'string': {
@@ -1411,7 +1411,7 @@ export const isAttributesAdditionPair = pair => {
     throw new TypeError(`Expected attributes pair to have two items`);
   }
 
-  withAggregate(({push}) => {
+  withAggregate({message: `Error validating attributes pair`}, ({push}) => {
     try {
       isAttributeKey(pair[0]);
     } catch (caughtError) {
@@ -1428,24 +1428,9 @@ export const isAttributesAdditionPair = pair => {
   return true;
 };
 
-export const isAttributesAdditionSingletValue =
+export const isAttributesAdditionSinglet =
   oneOf(
     validateInstanceOf(Template),
     validateInstanceOf(Attributes),
     validateAllPropertyValues(isAttributeValue),
-    looseArrayOf(value => isAttributesAdditionSingletValue(value)));
-
-export const isAttributesAdditionSinglet = singlet => {
-  isArray(singlet);
-
-  if (singlet.length !== 1) {
-    throw new TypeError(`Expected attributes singlet to have one item`);
-  }
-
-  isAttributesAdditionSingletValue(singlet[0]);
-
-  return true;
-}
-
-export const isAttributesAddition =
-  oneOf(isAttributesAdditionSinglet, isAttributesAdditionPair);
+    looseArrayOf(value => isAttributesAdditionSinglet(value)));