@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.
63 lines (62 loc) • 2.3 kB
JavaScript
import { ROOTS, SHEET } from "./constants.js";
/**
* Gets all roots 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 roots.
*/
export async function getRoots(apiKey) {
const url = `https://sheets.googleapis.com/v4/spreadsheets/${SHEET}/values/${ROOTS}?key=${apiKey}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error("Failed to fetch roots.");
}
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.");
}
if (!entry.every((x) => x === void 0 || typeof x == "string")) {
throw new Error("Expected all elements of entry to be strings or undefined.");
}
const cr = entry[0];
if (!cr) {
continue;
}
output.push({
cr: cr.replace(/ẓ/g, "ż"),
stems: entry
.slice(1, 5)
.map((x) => x?.replace(/ẓ/g, "ż") || void 0),
CPT: [
void 0,
...entry.slice(6, 9).map((x) => x?.replace(/ẓ/g, "ż") || void 0),
],
BSC: entry[14]?.replace(/ẓ/g, "ż") || void 0,
CTE: entry[15]?.replace(/ẓ/g, "ż") || void 0,
CSV: entry[16]?.replace(/ẓ/g, "ż") || void 0,
OBJ: [
void 0,
...entry.slice(17, 20).map((x) => x?.replace(/ẓ/g, "ż") || void 0),
],
DYN: entry[21]?.replace(/ẓ/g, "ż") || void 0,
wikidata: [
void 0,
...entry.slice(22, 25).map((x) => x?.replace(/ẓ/g, "ż") || void 0),
],
});
}
return output;
}