UNPKG

quran-meta

Version:

Library with meta data and functionality related to Holy Quran

58 lines (56 loc) 2.36 kB
import { checkValidAyahId } from "./validation.mjs"; import { findAyahIdBySurah } from "./findAyahIdBySurah.mjs"; import { findJuzByAyahId } from "./findJuzByAyahId.mjs"; import { findSurahByAyahId } from "./findSurahByAyahId.mjs"; //#region src/findJuzAndShift.ts /** * Finds the juz (section) that contains the specified ayah (verse) and calculates the number of ayahs between the start of the juz and the start of the surah (chapter) that contains the ayah. * * @param surah - The surah (chapter) that contains the ayah. * @param ayah - The ayah (verse) number. * @param lists - The Lists object for the riwaya. * @returns An object containing the following properties: * - `juz`: The juz (section) that contains the ayah. * - `leftAyahId`: The ayah ID of the first ayah in the juz. * - `ayahsBetweenJuzSurah`: The number of ayahs between the start of the juz and the start of the surah (positive if the surah starts is in the juz, negative if the surah starts before the juz). */ function findJuzAndShift(surah, ayah, lists) { const ayahId = findAyahIdBySurah(surah, ayah, lists); const JuzList = lists.JuzList; const SurahList = lists.SurahList; const juz = findJuzByAyahId(ayahId, lists); const juzLeftAyahId = JuzList[juz]; const [surahStartAyahId] = SurahList[surah]; return { juz, ayahsBetweenJuzSurah: surahStartAyahId - juzLeftAyahId, leftAyahId: juzLeftAyahId }; } /** * Finds the Juz number and calculates the shift between Juz start and Surah start for a given Ayah ID * * @param ayahId - The unique identifier of an Ayah (verse) in the Quran * @param data - The Lists object for the riwaya. * @returns An object containing: * - juz: The Juz number where the Ayah is located * - leftAyahId: The starting Ayah ID of the Juz * - ayahsBetweenJuzSurah: The number of Ayahs between the Juz start and the Surah start * * @throws Error If the provided Ayah ID is invalid */ function findJuzAndShiftByAyahId(ayahId, data) { checkValidAyahId(ayahId, data.meta); const JuzList = data.JuzList; const SurahList = data.SurahList; const juz = findJuzByAyahId(ayahId, data); const leftAyahId = JuzList[juz]; const [surahStartAyahId] = SurahList[findSurahByAyahId(ayahId, data)]; return { juz, ayahsBetweenJuzSurah: surahStartAyahId - leftAyahId, leftAyahId }; } //#endregion export { findJuzAndShift, findJuzAndShiftByAyahId };