quran-meta
Version:
Library with meta data and functionality related to Holy Quran
64 lines (62 loc) • 2.32 kB
JavaScript
import { checkValidAyahId } from "./validation.mjs";
import { binarySearch } from "./utils.mjs";
import { findSurahAyahByAyahId } from "./findSurahAyahByAyahId.mjs";
import { findPagebyAyahId } from "./findPagebyAyahId.mjs";
import { getRubAlHizbByAyahId } from "./getRubAlHizbByAyahId.mjs";
//#region src/getAyahMeta.ts
/**
* Retrieves metadata for a specific ayah of the Quran.
*
* @param ayahId - The ayahId number to retrieve metadata for (1-6236)
* @param data - The Lists object for the riwaya.
* @returns An object containing the ayah related meta, including information about the surah, juz, and quarter the ayah is in.
* @throws RangeError If the ayahId number is not between 1 and 6236
*/
function getAyahMeta(ayahId, data) {
checkValidAyahId(ayahId, data.meta);
const { SurahList, SajdaList, PageList, RukuList, JuzList, HizbQuarterList } = data;
const getThumunData = () => {
if ("HizbEighthList" in data && data.HizbEighthList) {
const jj = binarySearch(data.HizbEighthList, ayahId);
return { thumunAlHizbId: jj < 0 ? -jj - 2 : jj };
}
return {};
};
const quarterData = getRubAlHizbByAyahId(ayahId, data);
const [surah, ayah] = findSurahAyahByAyahId(ayahId, data);
const page = findPagebyAyahId(ayahId, data);
const isSajdahAyah = binarySearch(SajdaList, ayahId, (a, b) => a - b) >= 0;
const rk = binarySearch(RukuList, ayahId);
const isStartOfRuku = rk > 0;
const ruku = isStartOfRuku ? rk : -rk - 2;
const isStartOfSurah = SurahList[surah][0] === ayahId;
const isStartOfPage = PageList[page] === ayahId;
const isStartOfJuz = JuzList[quarterData.juz] === ayahId;
const isStartOfQuarter = HizbQuarterList[quarterData.rubAlHizbId] === ayahId;
const isEndOfSurah = SurahList[surah + 1][0] - 1 === ayahId;
const isEndOfPage = PageList[page + 1] - 1 === ayahId;
const isEndOfJuz = JuzList[quarterData.juz + 1] - 1 === ayahId;
const isEndOfRuku = binarySearch(RukuList, ayahId + 1) > 0;
const isEndOfQuarter = HizbQuarterList[quarterData.rubAlHizbId + 1] - 1 === ayahId;
return {
...quarterData,
...getThumunData(),
surah,
ayah,
page,
isStartOfQuarter,
isEndOfQuarter,
isSajdahAyah,
isStartOfPage,
isEndOfPage,
ruku,
isStartOfJuz,
isEndOfJuz,
isStartOfSurah,
isEndOfSurah,
isStartOfRuku,
isEndOfRuku
};
}
//#endregion
export { getAyahMeta };