@zsnout/ithkuil
Version:
A set of tools which can generate and parse romanized Ithkuil text and which can generate Ithkuil script from text and JSON data.
59 lines (58 loc) • 2.26 kB
JavaScript
import { AFFIXES, SHEET } from "./constants.js";
/**
* Gets all affixes described in the Collaborative Ithkuil IV Roots and Affixes
* Spreadsheet.
*
* @param apiKey The Google Sheets API key to use when fetching data.
* @returns An array of affixes.
*/
export async function getAffixes(apiKey) {
const url = `https://sheets.googleapis.com/v4/spreadsheets/${SHEET}/values/${AFFIXES}?key=${apiKey}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error("Failed to fetch affixes.");
}
const data = await response.json();
if (data == null || typeof data != "object") {
throw new Error("Unexpected result type: " + data == null ? "null" : typeof data + ".");
}
if (!("values" in data)) {
throw new Error("No 'values' key was found in the returned data.");
}
const values = data.values;
if (!Array.isArray(values)) {
throw new Error("Expected 'values' to be an array.");
}
const output = [];
for (const entry of values) {
if (!Array.isArray(entry)) {
throw new Error("Expected all elements of 'values' to be arrays.");
}
const cs = entry[0];
if (typeof cs != "string") {
throw new Error("Expected Cs form of affix to be a string.");
}
const abbreviation = entry[1];
if (typeof abbreviation != "string") {
throw new Error("Expected abbreviation of affix to be a string.");
}
const degrees = entry.slice(2, 11);
if (!degrees.every((x) => typeof x == "string" || x == null)) {
throw new Error("Expected degrees of affix to be strings or null.");
}
const description = entry[12];
if (typeof description != "string" && description != null) {
throw new Error("Expected description of affix to be a string or null.");
}
output.push({
cs: cs.replace(/ẓ/g, "ż"),
abbreviation: abbreviation.replace(/ẓ/g, "ż"),
degrees: [
void 0,
...degrees.map((x) => x?.replace(/ẓ/g, "ż") || void 0),
],
description: description?.replace(/ẓ/g, "ż") || void 0,
});
}
return output;
}