@sahabaplus/mushaf-engine
Version:
TypeScript implementation of a Quran Mushaf navigation engine
106 lines (105 loc) • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VersePosition = void 0;
/**
* Represents a position in the Quran by sura and verse number.
*
* This class provides a type-safe way to represent verse positions
* and enables better API design for navigation operations.
*/
class VersePosition {
/**
* Create a new VersePosition
*
* @param sura - Sura number (1-114)
* @param verse - Verse number within the sura (1-286)
*/
constructor(sura, verse) {
this.sura = sura;
this.verse = verse;
}
/**
* Create a new VersePosition for the start of the Mushaf
*
* @returns VersePosition for (1, 1) - Al-Fatiha, verse 1
*/
static start() {
return new VersePosition(1, 1);
}
/**
* Create a new VersePosition for the end of the Mushaf
*
* @returns VersePosition for (114, 6) - An-Nas, verse 6
*/
static end() {
return new VersePosition(114, 6);
}
/**
* Get the sura number
*
* @returns The sura number
*/
getSura() {
return this.sura;
}
/**
* Get the verse number
*
* @returns The verse number
*/
getVerse() {
return this.verse;
}
/**
* Get the position as a tuple
*
* @returns Tuple of [sura, verse]
*/
getTuple() {
return [this.sura, this.verse];
}
/**
* Convert to the start of the same sura (verse 1)
*
* @returns A new VersePosition at the start of the same sura
*/
toStartOfSura() {
return new VersePosition(this.sura, 1);
}
/**
* Check if this position equals another VersePosition
*
* @param other - Other VersePosition to compare
* @returns True if positions are equal
*/
equals(other) {
return this.sura === other.sura && this.verse === other.verse;
}
/**
* Check if this position equals a Verse object
*
* @param verse - Verse object to compare
* @returns True if position matches the verse
*/
equalsVerse(verse) {
return this.sura === verse.sura && this.verse === verse.number;
}
/**
* Convert to string representation
*
* @returns String representation of the position
*/
toString() {
return `(${this.sura}, ${this.verse})`;
}
/**
* Create a VersePosition from a Verse object
*
* @param verse - Verse object to convert
* @returns New VersePosition
*/
static fromVerse(verse) {
return new VersePosition(verse.sura, verse.number);
}
}
exports.VersePosition = VersePosition;