quran-meta
Version:
Library with meta data and functionality related to Holy Quran
23 lines (16 loc) • 795 B
JavaScript
const require_typeGuards = require('./typeGuards.cjs');
//#region src/surahStringParser.ts
/**
* Parses a string representation of a Surah number and converts it to a valid Surah type
* @param str - The string containing the Surah number to parse
* @returns The parsed Surah number as a Surah type
* @throws {@link Error} If the string cannot be parsed as a number or if the number is not a valid Surah number
*/
function surahStringParser(str, isStrict = false) {
const surahX = isStrict ? Number(str.trim()) : Number.parseInt(str.trim(), 10);
if (isNaN(surahX)) throw new Error("Error in surah format " + str);
if (!require_typeGuards.isValidSurah(surahX)) throw new Error("Error in surah number " + str);
return surahX;
}
//#endregion
exports.surahStringParser = surahStringParser;