UNPKG

quran-meta

Version:

Library with meta data and functionality related to Holy Quran

99 lines (97 loc) 3.13 kB
import { maxAyahsInSurah } from "./types.mjs"; import { getAyahCountInSurah } from "./getAyahCountInSurah.mjs"; //#region src/typeGuards.ts /** * Checks if the given value is a valid AyahId. * * @param x - The value to check. * @returns True if the value is a valid AyahId, otherwise false. */ function isValidAyahId(x, meta) { return Number.isInteger(x) && 1 <= x && x <= meta.numAyahs; } /** * Checks if the given value is a valid Ayah number. * * @param x - The value to check. * @returns True if the value is a valid Ayah number, otherwise false. */ function isValidAyahNo(x) { return Number.isInteger(x) && 1 <= x && x <= maxAyahsInSurah; } /** * Checks if the given value is a valid Surah number. * * @param x - The value to check. * @returns `true` if the value is a valid Surah number, otherwise `false`. */ function isValidSurah(x, meta) { return Number.isInteger(x) && 1 <= x && x <= meta.numSurahs; } /** * Type guard function that checks if a tuple of two numbers represents a valid Surah and Ayah combination. * * @param x - A tuple containing [surahNumber, ayahNumber] * @param lists - The Lists object for the riwaya * @returns True if the tuple represents a valid Surah-Ayah combination, false otherwise * * @example * ```ts * isValidSurahAyah([1, 7], HafsLists) // true - Al-Fatiha has 7 ayahs * isValidSurahAyah([1, 8], HafsLists) // false - Al-Fatiha only has 7 ayahs * isValidSurahAyah([115, 1], HafsLists) // false - there are only 114 surahs * ``` */ function isValidSurahAyah(x, data) { const [surah, ayah] = x; if (!isValidSurah(surah, data.meta)) return false; return Number.isInteger(ayah) && ayah >= 1 && ayah <= getAyahCountInSurah(surah, data); } /** * Type guard that checks if a number is a valid Juz number * @param x - The number to check * @returns True if the number is an integer between 1 and the total number of Juzs (inclusive) */ function isValidJuz(x, meta) { return Number.isInteger(x) && 1 <= x && x <= meta.numJuzs; } /** * Type guard to check if a number is a valid Quran page number * @param x - The number to check * @returns True if the number is an integer between 1 and the total number of pages (inclusive) */ function isValidPage(x, meta) { return Number.isInteger(x) && 1 <= x && x <= meta.numPages; } /** * Type guard that checks if a value is a valid Ruku number * @param x - The value to check * @returns True if x is an integer between 1 and the total number of Rukus * @example * ```ts * if (isValidRuku(5)) { * // 5 is a valid Ruku number * } * ``` */ function isValidRuku(x, meta) { return Number.isInteger(x) && 1 <= x && x <= meta.numRukus; } /** * Type guard to check if a value is a valid Manzil number * * @param x - The value to check * @returns True if the value is an integer between 1 and the total number of Manzils * * @example * ```ts * if (isValidManzil(3)) { * // value is a valid Manzil number * } * ``` */ function isValidManzil(x, meta) { return Number.isInteger(x) && 1 <= x && x <= meta.numManzils; } //#endregion export { isValidAyahId, isValidAyahNo, isValidJuz, isValidManzil, isValidPage, isValidRuku, isValidSurah, isValidSurahAyah };