quran-meta
Version:
Library with meta data and functionality related to Holy Quran
43 lines (41 loc) • 1.56 kB
JavaScript
const require_types = require('./types.cjs');
//#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 (!require_types.parts[name]) throw new Error(`Invalid part type: ${name}`);
const listName = require_types.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 (!require_types.parts[name]) throw new Error(`Invalid list name: ${name}`);
const listName = require_types.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
exports.generatePartBlocks = generatePartBlocks;
exports.getList = getList;
exports.getListNormalised = getListNormalised;