« get me outta code hell

data: language: flatten language spec, allow for structuring - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/language.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-11-06 20:08:15 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-11-06 20:08:33 -0400
commit8d24f17f729c7da550824ab4134b89757754fb9c (patch)
tree8bb3869990d82027d537259042d403cc9f513866 /src/data/language.js
parentfade91f5f20a68ea8ffad6f151ff9c9bd8b19736 (diff)
data: language: flatten language spec, allow for structuring
Diffstat (limited to 'src/data/language.js')
-rw-r--r--src/data/language.js28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/data/language.js b/src/data/language.js
index aed1605..99efc03 100644
--- a/src/data/language.js
+++ b/src/data/language.js
@@ -46,8 +46,24 @@ export function processLanguageSpec(spec, {existingCode = null}) {
   return {code, intlCode, name, hidden, strings};
 }
 
+function flattenLanguageSpec(spec) {
+  const recursive = (keyPath, value) =>
+    (typeof value === 'object'
+      ? Object.assign({}, ...
+          Object.entries(value)
+            .map(([key, value]) =>
+              (key === '_'
+                ? {[keyPath]: value}
+                : recursive(
+                    (keyPath ? `${keyPath}.${key}` : key),
+                    value))))
+      : {[keyPath]: value});
+
+  return recursive('', spec);
+}
+
 async function processLanguageSpecFromFile(file, processLanguageSpecOpts) {
-  let contents, spec;
+  let contents;
 
   try {
     contents = await readFile(file, 'utf-8');
@@ -57,14 +73,16 @@ async function processLanguageSpecFromFile(file, processLanguageSpecOpts) {
       error => annotateErrorWithFile(error, file));
   }
 
+  let rawSpec;
   let parseLanguage;
+
   try {
     if (path.extname(file) === '.yaml') {
       parseLanguage = 'YAML';
-      spec = yaml.load(contents);
+      rawSpec = yaml.load(contents);
     } else {
       parseLanguage = 'JSON';
-      spec = JSON.parse(contents);
+      rawSpec = JSON.parse(contents);
     }
   } catch (caughtError) {
     throw annotateError(
@@ -72,8 +90,10 @@ async function processLanguageSpecFromFile(file, processLanguageSpecOpts) {
       error => annotateErrorWithFile(error, file));
   }
 
+  const flattenedSpec = flattenLanguageSpec(rawSpec);
+
   try {
-    return processLanguageSpec(spec, processLanguageSpecOpts);
+    return processLanguageSpec(flattenedSpec, processLanguageSpecOpts);
   } catch (caughtError) {
     throw annotateErrorWithFile(caughtError, file);
   }