« get me outta code hell

data: tidy language loading code, add processLanguageSpec - 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-11-01 09:00:55 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-11-06 17:56:06 -0400
commit0768953f9538f0bbd65835b0a4293e2ba438ce52 (patch)
tree8e3351912bb73b9a50d5c59ba93dcf3b905b29c1
parent682b62b33aa6e5a4c512343d0355d32cb1c67c17 (diff)
data: tidy language loading code, add processLanguageSpec
-rw-r--r--src/data/language.js46
1 files changed, 27 insertions, 19 deletions
diff --git a/src/data/language.js b/src/data/language.js
index 09466907..34de8779 100644
--- a/src/data/language.js
+++ b/src/data/language.js
@@ -5,35 +5,43 @@ import he from 'he';
 
 import T from '#things';
 
-export async function processLanguageFile(file) {
-  const contents = await readFile(file, 'utf-8');
-  const json = JSON.parse(contents);
+export function processLanguageSpec(spec) {
+  const {
+    'meta.languageCode': code,
+    'meta.languageName': name,
+
+    'meta.languageIntlCode': intlCode = null,
+    'meta.hidden': hidden = false,
+
+    ...strings
+  } = spec;
 
-  const code = json['meta.languageCode'];
   if (!code) {
     throw new Error(`Missing language code (file: ${file})`);
   }
-  delete json['meta.languageCode'];
 
-  const intlCode = json['meta.languageIntlCode'] ?? null;
-  delete json['meta.languageIntlCode'];
-
-  const name = json['meta.languageName'];
   if (!name) {
     throw new Error(`Missing language name (${code})`);
   }
-  delete json['meta.languageName'];
-
-  const hidden = json['meta.hidden'] ?? false;
-  delete json['meta.hidden'];
 
   const language = new T.Language();
-  language.code = code;
-  language.intlCode = intlCode;
-  language.name = name;
-  language.hidden = hidden;
-  language.escapeHTML = (string) =>
+
+  Object.assign(language, {
+    code,
+    intlCode,
+    name,
+    hidden,
+    strings,
+  });
+
+  language.escapeHTML = string =>
     he.encode(string, {useNamedReferences: true});
-  language.strings = json;
+
   return language;
 }
+
+export async function processLanguageFile(file) {
+  const contents = await readFile(file, 'utf-8');
+  const spec = JSON.parse(contents);
+  return processLanguageSpec(spec);
+}