UNPKG

quran-meta

Version:

Library with meta data and functionality related to Holy Quran

41 lines (39 loc) 1.43 kB
import { parts } from "./types.mjs"; //#region src/lists/getList.ts function toPartFormatter(type, list) { return type === "surah" ? ([startAyahId, ayahCount]) => ({ startAyahId, ayahCount }) : (ayahId, index) => { return { startAyahId: ayahId, ayahCount: list[index + 2] - ayahId }; }; } /** * Retrieves a formatted list of Quran parts based on the specified type. * @param name - The type of parts to retrieve (e.g., juz, hizb, rub) * @param data - The Lists object for the riwaya. * @returns An array of formatted part blocks, excluding the first and last elements */ function generatePartBlocks(name, data) { if (!parts[name]) throw new Error(`Invalid part type: ${name}`); const listName = parts[name]; const list = data[listName]; if (!list) return null; if (!Array.isArray(list)) throw new TypeError(`Expected array for ${String(listName)}`); return list.slice(1, -1).map(toPartFormatter(name, list)); } const getList = (name, lists) => { if (!parts[name]) throw new Error(`Invalid list name: ${name}`); const listName = parts[name]; if (listName in lists) return lists[listName]; throw new Error(`List ${String(listName)} not found in ${lists.meta.riwayaName} riwaya`); }; function getListNormalised(name, lists) { const list = getList(name, lists); return list.slice(1, list.length - 1).map(toPartFormatter(name, list)); } //#endregion export { generatePartBlocks, getList, getListNormalised };