Skip to content

Commit b3faf12

Browse files
committed
full refactor of the library
1 parent 44f82e5 commit b3faf12

35 files changed

+257
-272
lines changed

src/ayaStringSplitter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { checkValidSurahAyah } from "./checkValidSurahAyah"
21
import { AyahNo, SurahAyahSegment } from "./types"
2+
import { checkValidSurahAyah } from "./validation"
33

44
/**
55
* Turns String of type "x:y" or "x:y1-y2" to array [x,y] or [x,[y1,y2]] respectively
@@ -20,22 +20,22 @@ export function ayaStringSplitter(str: string): SurahAyahSegment {
2020

2121
let ayahs: AyahNo | [AyahNo, AyahNo]
2222
if (ayahsStr.includes("-")) {
23-
ayahs = ayahsStr.split("-").map(a => {
23+
ayahs = ayahsStr.split("-").map((a) => {
2424
const ayah = parseInt(a, 10)
2525
if (isNaN(ayah) || ayah === 0) {
2626
throw "Error in ayah " + a
2727
}
2828
return ayah
2929
}) as [AyahNo, AyahNo]
3030
if (ayahs[0] > ayahs[1]) throw "Error in ayah range " + str
31-
} else {
31+
}
32+
else {
3233
ayahs = parseInt(ayahsStr, 10)
33-
if (isNaN(ayahs)|| ayahs === 0) {
34+
if (isNaN(ayahs) || ayahs === 0) {
3435
throw "Error in data " + str
3536
}
3637
checkValidSurahAyah(surah, ayahs)
3738
}
3839

39-
4040
return [surah, ayahs]
4141
}

src/checkValidAyahId.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/checkValidSurah.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/checkValidSurahAyah.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export const meta: Readonly<QuranMeta> = Object.freeze({
1111
numSuras: 114,
1212
numPages: 604,
1313
numJuzs: 30,
14-
manzilCount: 7,
14+
manzilCount: 7
1515
})

src/findAyaidBySurah.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { checkValidSurahAyah } from "./checkValidSurahAyah";
2-
import { getSurahMeta } from "./getSurahMeta";
3-
import { AyahId, AyahNo, Surah } from "./types";
1+
import { getSurahMeta } from "./getSurahMeta"
2+
import { AyahId, AyahNo, Surah } from "./types"
3+
import { checkValidSurahAyah } from "./validation"
44

55
/**
66
* Get the ayah ID for the given surah and ayah number.
@@ -9,7 +9,7 @@ import { AyahId, AyahNo, Surah } from "./types";
99
* @returns The ayah ID for the given surah and ayah number.
1010
*/
1111
export function findAyaidBySurah(surah: Surah, ayah: AyahNo): AyahId {
12-
checkValidSurahAyah(surah, ayah);
13-
const [startAyahId] = getSurahMeta(surah);
14-
return startAyahId + ayah;
12+
checkValidSurahAyah(surah, ayah)
13+
const [startAyahId] = getSurahMeta(surah)
14+
return startAyahId + ayah
1515
}

src/findJuz.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { checkValidSurahAyah } from "./checkValidSurahAyah";
2-
import { findAyaidBySurah } from "./findAyaidBySurah";
3-
import { findJuzByAyaid } from "./findJuzByAyaid";
4-
import { AyahId, AyahNo, Juz, Surah } from "./types";
1+
import { findAyaidBySurah } from "./findAyaidBySurah"
2+
import { findJuzByAyaid } from "./findJuzByAyaid"
3+
import { AyahId, AyahNo, Juz, Surah } from "./types"
4+
import { checkValidSurahAyah } from "./validation"
55

66
/**
77
* Finds the Juz (part) of the Quran that the given Ayah (verse) belongs to.
@@ -14,7 +14,8 @@ import { AyahId, AyahNo, Juz, Surah } from "./types";
1414
export function findJuz(surah: Surah, ayah: AyahNo = 1, ayahMode = false): Juz {
1515
const ayahId: AyahId = ayahMode
1616
? ayah
17-
: ((checkValidSurahAyah(surah, ayah) && findAyaidBySurah(surah, ayah)) as AyahId);
17+
: ((checkValidSurahAyah(surah, ayah)
18+
&& findAyaidBySurah(surah, ayah)) as AyahId)
1819

19-
return findJuzByAyaid(ayahId);
20+
return findJuzByAyaid(ayahId)
2021
}

src/findJuzAndShift.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { checkValidAyahId } from "./checkValidAyahId";
2-
import { checkValidSurah } from "./checkValidSurah";
3-
import { findAyaidBySurah } from "./findAyaidBySurah";
4-
import { findJuzByAyaid } from "./findJuzByAyaid";
5-
import { findSurahByAyaid } from "./findSurahByAyaid";
6-
import { JuzList } from "./lists/juzList";
7-
import { SuraList } from "./lists/surahList";
8-
import { AyahId, AyahNo, Juz, Surah } from "./types";
9-
1+
import { findAyaidBySurah } from "./findAyaidBySurah"
2+
import { findJuzByAyaid } from "./findJuzByAyaid"
3+
import { findSurahByAyaid } from "./findSurahByAyaid"
4+
import { JuzList } from "./lists/juzList"
5+
import { SuraList } from "./lists/surahList"
6+
import { AyahId, AyahNo, Juz, Surah } from "./types"
7+
import { checkValidAyahId, checkValidSurah } from "./validation"
108

119
/**
1210
* 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.
@@ -24,21 +22,21 @@ export function findJuzAndShift(
2422
ayah: AyahNo,
2523
ayahMode = false
2624
): {
27-
juz: Juz;
28-
leftAyahId: AyahId;
29-
ayahsBetweenJuzSurah: number;
30-
} {
25+
juz: Juz
26+
leftAyahId: AyahId
27+
ayahsBetweenJuzSurah: number
28+
} {
3129
const ayahId: AyahId = ayahMode
3230
? ((checkValidAyahId(ayah) && ayah) as AyahId)
33-
: ((checkValidSurah(surah) && findAyaidBySurah(surah, ayah)) as AyahId);
31+
: ((checkValidSurah(surah) && findAyaidBySurah(surah, ayah)) as AyahId)
3432

35-
const juz = findJuzByAyaid(ayahId);
36-
const leftAyahId = JuzList[juz];
37-
if (ayahMode) [surah] = findSurahByAyaid(ayahId);
38-
const [surahStartAyahId] = SuraList[surah];
33+
const juz = findJuzByAyaid(ayahId)
34+
const leftAyahId = JuzList[juz]
35+
if (ayahMode) [surah] = findSurahByAyaid(ayahId)
36+
const [surahStartAyahId] = SuraList[surah]
3937
return {
4038
juz,
4139
ayahsBetweenJuzSurah: surahStartAyahId - leftAyahId + 1,
4240
leftAyahId
43-
};
41+
}
4442
}

src/findJuzByAyaid.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { checkValidAyahId } from "./checkValidAyahId";
2-
import { JuzList } from "./lists/juzList";
3-
import { AyahId, Juz } from "./types";
4-
1+
import { JuzList } from "./lists/juzList"
2+
import { AyahId, Juz } from "./types"
3+
import { checkValidAyahId } from "./validation"
54

65
/**
76
* Finds the Juz (part) of the Quran that contains the given Ayah (verse) ID.
@@ -10,7 +9,7 @@ import { AyahId, Juz } from "./types";
109
* @returns The Juz (part) of the Quran that contains the given Ayah ID.
1110
*/
1211
export function findJuzByAyaid(ayaId: AyahId): Juz {
13-
checkValidAyahId(ayaId);
12+
checkValidAyahId(ayaId)
1413

15-
return JuzList.findIndex(x => x > ayaId) - 1;
14+
return JuzList.findIndex(x => x > ayaId) - 1
1615
}

src/findJuzHizb.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { checkValidSurah } from "./checkValidSurah";
2-
import { findAyaidBySurah } from "./findAyaidBySurah";
3-
import { findJuzHizbByAyaid } from "./findJuzHizbByAyaid";
4-
import { AyahId, AyahNo, JuzHizb, Surah } from "./types";
1+
import { findAyaidBySurah } from "./findAyaidBySurah"
2+
import { findJuzHizbByAyaid } from "./findJuzHizbByAyaid"
3+
import { AyahId, AyahNo, JuzHizb, Surah } from "./types"
4+
import { checkValidSurah } from "./validation"
55

66
/**
77
* Finds the Juz (part) and Hizb (section) of the Quran that the given Ayah (verse) belongs to.
@@ -18,7 +18,7 @@ export function findJuzHizb(
1818
): JuzHizb {
1919
const ayahId: AyahId = ayahMode
2020
? ayah
21-
: ((checkValidSurah(surah) && findAyaidBySurah(surah, ayah)) as AyahId);
21+
: ((checkValidSurah(surah) && findAyaidBySurah(surah, ayah)) as AyahId)
2222

23-
return findJuzHizbByAyaid(ayahId);
23+
return findJuzHizbByAyaid(ayahId)
2424
}

src/findJuzHizbByAyaid.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { checkValidAyahId } from "./checkValidAyahId";
2-
import { findJuzByAyaid } from "./findJuzByAyaid";
3-
import { HizbQuarterList } from "./lists/hizbList";
4-
import { AyahId, JuzHizb } from "./types";
5-
1+
import { findJuzByAyaid } from "./findJuzByAyaid"
2+
import { HizbQuarterList } from "./lists/hizbList"
3+
import { AyahId, JuzHizb } from "./types"
4+
import { checkValidAyahId } from "./validation"
65

76
/**
87
* Finds the Juz, Hizb, and Hizb ID for the given Ayah ID.
@@ -11,10 +10,13 @@ import { AyahId, JuzHizb } from "./types";
1110
* @returns An object containing the Juz, Hizb, and Hizb ID for the given Ayah ID.
1211
*/
1312
export function findJuzHizbByAyaid(ayaId: AyahId): JuzHizb {
14-
checkValidAyahId(ayaId);
15-
16-
const juz = findJuzByAyaid(ayaId);
13+
checkValidAyahId(ayaId)
1714

18-
const id = HizbQuarterList.findIndex(x => x > ayaId) - 1;
19-
return { juz, hizb: id % 8 || 8, id };
15+
const juz = findJuzByAyaid(ayaId)
16+
const quarterIndex = HizbQuarterList.findIndex(x => x > ayaId) - 1
17+
if (quarterIndex < 0) {
18+
throw new Error("Invalid Ayah ID: No corresponding Hizb found.")
19+
}
20+
const hizb = quarterIndex % 8 || 8
21+
return { juz, hizb, id: quarterIndex }
2022
}

src/findJuzMetaBySurah.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
1-
import { meta } from "./const";
2-
import { findJuzAndShift } from "./findJuzAndShift";
3-
import { findSurahByAyaid } from "./findSurahByAyaid";
4-
import { JuzList } from "./lists/juzList";
5-
import { AyahNo, Juz, JuzMeta, Surah } from "./types";
1+
import { meta } from "./const"
2+
import { findJuzAndShift } from "./findJuzAndShift"
3+
import { findSurahByAyaid } from "./findSurahByAyaid"
4+
import { JuzList } from "./lists/juzList"
5+
import { AyahNo, Juz, JuzMeta, Surah } from "./types"
66

77
/**
88
* for given ayah return [starting juz, number of ayahsFrom beginning of that juz, right juz, number of ayahs in surah
9-
* @param {*} suraNumber
10-
* @param {*} ayaNumber
9+
* @param suraNumber
10+
* @param ayaNumber
1111
* @returns [leftjuz, ayahsFromStartOfJuz, rightJuz, ayahsinSurah,leftAyahId,rightAyahId]
1212
*/
1313
export function findJuzMetaBySurah(surah: Surah, ayah: AyahNo = 1): JuzMeta {
1414
const {
15-
juz: leftjuz, ayahsBetweenJuzSurah, leftAyahId,
16-
} = findJuzAndShift(surah, ayah);
15+
juz: leftjuz,
16+
ayahsBetweenJuzSurah,
17+
leftAyahId
18+
} = findJuzAndShift(surah, ayah)
1719

18-
let rightJuz: Juz = leftjuz;
19-
while (rightJuz < meta.numJuzs &&
20-
findSurahByAyaid(JuzList[rightJuz + 1])[0] == surah)
21-
rightJuz++;
20+
let rightJuz: Juz = leftjuz
21+
while (
22+
rightJuz < meta.numJuzs
23+
&& findSurahByAyaid(JuzList[rightJuz + 1])[0] === surah
24+
) {
25+
rightJuz++
26+
}
2227

2328
return {
2429
leftjuz,
2530
ayahsBetweenJuzSurah,
2631
rightJuz,
2732
leftAyahId,
28-
rightAyahId: JuzList[rightJuz + 1],
29-
};
33+
rightAyahId: JuzList[rightJuz + 1]
34+
}
3035
}

src/findPage.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { checkValidAyahId } from "./checkValidAyahId";
2-
import { checkValidSurah } from "./checkValidSurah";
3-
import { findAyaidBySurah } from "./findAyaidBySurah";
4-
import { PageList } from "./lists/pageList";
5-
import { AyahId, AyahNo, Page, Surah } from "./types";
6-
1+
import { findAyaidBySurah } from "./findAyaidBySurah"
2+
import { PageList } from "./lists/pageList"
3+
import { AyahId, AyahNo, Page, Surah } from "./types"
4+
import { checkValidAyahId, checkValidSurah } from "./validation"
75

86
/**
97
* Finds the page number for the given Surah and Ayah number.
@@ -16,7 +14,7 @@ import { AyahId, AyahNo, Page, Surah } from "./types";
1614
export function findPage(surah: Surah, ayah: AyahNo, ayahMode = false): Page {
1715
const ayahId: AyahId = ayahMode
1816
? ((checkValidAyahId(ayah) && ayah) as AyahId)
19-
: ((checkValidSurah(surah) && findAyaidBySurah(surah, ayah)) as AyahId);
17+
: ((checkValidSurah(surah) && findAyaidBySurah(surah, ayah)) as AyahId)
2018

21-
return PageList.findIndex(x => x > ayahId) - 1;
19+
return PageList.findIndex(x => x > ayahId) - 1
2220
}

0 commit comments

Comments
 (0)