« get me outta code hell

language: encapsulate - 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>2024-06-13 17:51:03 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-06-18 22:56:12 -0300
commit5e7a0113594fdd42eb3a265de009eb0b0e823b9e (patch)
treef118275369cf0408eb63121661d9c0985ff03647
parent1ed9a32db8207b533281feb43cff7996790dbcb0 (diff)
language: encapsulate
-rw-r--r--src/data/things/language.js31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/data/things/language.js b/src/data/things/language.js
index f20927a4..88f16ecb 100644
--- a/src/data/things/language.js
+++ b/src/data/things/language.js
@@ -208,9 +208,7 @@ export class Language extends Thing {
       args.at(-1) !== null;
 
     const key =
-      (hasOptions ? args.slice(0, -1) : args)
-        .filter(Boolean)
-        .join('.');
+      this.#joinKeyParts(hasOptions ? args.slice(0, -1) : args);
 
     const options =
       (hasOptions
@@ -843,6 +841,33 @@ export class Language extends Thing {
       return this.formatString('count.fileSize.bytes', {bytes});
     }
   }
+
+  // Utility function to quickly provide a useful string key
+  // (generally a prefix) to stuff nested beneath it.
+  encapsulate(...args) {
+    const fn =
+      (typeof args.at(-1) === 'function'
+        ? args.at(-1)
+        : null);
+
+    const parts =
+      (fn
+        ? args.slice(0, -1)
+        : args);
+
+    const capsule =
+      this.#joinKeyParts(parts);
+
+    if (fn) {
+      return fn(capsule);
+    } else {
+      return capsule;
+    }
+  }
+
+  #joinKeyParts(parts) {
+    return parts.filter(Boolean).join('.');
+  }
 }
 
 const countHelper = (stringKey, optionName = stringKey) =>