« get me outta code hell

language.js « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/language.js
blob: 34de8779d758bad3aaec09f93b25e722c35c8fea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {readFile} from 'node:fs/promises';

// It stands for "HTML Entities", apparently. Cursed.
import he from 'he';

import T from '#things';

export function processLanguageSpec(spec) {
  const {
    'meta.languageCode': code,
    'meta.languageName': name,

    'meta.languageIntlCode': intlCode = null,
    'meta.hidden': hidden = false,

    ...strings
  } = spec;

  if (!code) {
    throw new Error(`Missing language code (file: ${file})`);
  }

  if (!name) {
    throw new Error(`Missing language name (${code})`);
  }

  const language = new T.Language();

  Object.assign(language, {
    code,
    intlCode,
    name,
    hidden,
    strings,
  });

  language.escapeHTML = string =>
    he.encode(string, {useNamedReferences: true});

  return language;
}

export async function processLanguageFile(file) {
  const contents = await readFile(file, 'utf-8');
  const spec = JSON.parse(contents);
  return processLanguageSpec(spec);
}