From 0768953f9538f0bbd65835b0a4293e2ba438ce52 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 1 Nov 2023 09:00:55 -0300 Subject: data: tidy language loading code, add processLanguageSpec --- src/data/language.js | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'src/data') 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); +} -- cgit 1.3.0-6-gf8a5