Skip to content

Commit a696010

Browse files
committed
feat: added getAyahMetasForSurah
1 parent 81bfd5d commit a696010

File tree

9 files changed

+136
-6
lines changed

9 files changed

+136
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Answering Questions like:
4343
* Find
4444
* next or previous ayah (`nextAyah`/`prevAyah`)
4545
* juz `findJuz` and `findJuzByAyahId`
46-
* hizb `findRubAlHizb`, `getRubAlHizbMetaByAyahId`
46+
* maqra/rub-el-hizb `findRubAlHizb`, `getRubAlHizbMetaByAyahId`
4747
* page `findPage` by surah/ayah
4848
* AyahId of a given surah/ayah (`findAyahIdBySurah`)
4949
* find range around ayah (`findRangeAroundAyah`)

src/findRubAlHizbByAyahId.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HizbQuarterList } from "./lists/hizbList"
1+
import { HizbQuarterList } from "./lists/hizbQuarterList"
22
import { AyahId, RubAlHizbId } from "./types"
33
import { checkValidAyahId } from "./validation"
44

src/getAyahMeta.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { findPagebyAyahId } from "./findPagebyAyahId"
22
import { findSurahAyahByAyahId } from "./findSurahAyahByAyahId"
33
import { getRubAlHizbMetaByAyahId } from "./getRubAlHizbMetaByAyahId"
4-
import { HizbQuarterList } from "./lists/hizbList"
4+
import { HizbQuarterList } from "./lists/hizbQuarterList"
55
import { JuzList } from "./lists/juzList"
66
import { PageList } from "./lists/pageList"
77
import { SajdaList } from "./lists/sajdaList"
@@ -33,7 +33,7 @@ export function getAyahMeta(ayahId: AyahId): AyahMeta {
3333
const isEndOfQuarter = HizbQuarterList[quarterData.rubAlHizbId + 1] - 1 === ayahId
3434

3535
return {
36-
...quarterData, surah, ayah, isStartOfQuarter,
36+
...quarterData, surah, ayah, page, isStartOfQuarter,
3737
isEndOfQuarter, isSajdahAyah, isStartOfPage, isEndOfPage,
3838
isStartOfJuz, isEndOfJuz, isStartOfSurah, isEndOfSurah
3939
}

src/getAyahMetasForSurah.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { getAyahMeta } from "./getAyahMeta"
2+
import { HizbQuarterList } from "./lists/hizbQuarterList"
3+
import { JuzList } from "./lists/juzList"
4+
import { PageList } from "./lists/pageList"
5+
import { SajdaList } from "./lists/sajdaList"
6+
import { SurahList } from "./lists/surahList"
7+
import { Surah, AyahMeta, AyahId } from "./types"
8+
import { checkValidSurah } from "./validation"
9+
10+
/**
11+
* Retrieves metadata for all ayahs in a specific surah.
12+
*
13+
* @param surahNumber - The surah number (1-114)
14+
* @returns Array of AyahMeta objects for each ayah in the surah
15+
* @throws RangeError If the surah number is not between 1 and 114
16+
*/
17+
export function getAyahMetasForSurah(surahNumber: Surah): AyahMeta[] {
18+
checkValidSurah(surahNumber)
19+
const [
20+
startAyahId, ayahCount, surahOrder, rukuCount, name, isMeccan, page
21+
] = SurahList[surahNumber]
22+
const endAyahId = startAyahId + ayahCount - 1
23+
const result: AyahMeta[] = []
24+
25+
// const rubAlHizbMeta = getRubAlHizbMetaByAyahId(startAyahId as AyahId)
26+
let meta = getAyahMeta(startAyahId as AyahId)
27+
for (let ayahId = startAyahId; ayahId <= endAyahId; ayahId++) {
28+
// Most properties will be the same as previous ayah except for specific positions
29+
if (ayahId > startAyahId) {
30+
meta = structuredClone(meta)
31+
meta.ayah += 1
32+
// console.log(ayahId, meta.ayah, meta.page, PageList[meta.page + 1])
33+
meta.isStartOfSurah = false
34+
meta.isEndOfSurah = endAyahId === ayahId
35+
36+
if (PageList[meta.page + 1] === ayahId) {
37+
meta.page += 1
38+
meta.isStartOfPage = true
39+
}
40+
else { meta.isStartOfPage = false }
41+
meta.isEndOfPage = PageList[meta.page + 1] === ayahId + 1
42+
43+
meta.isEndOfJuz = JuzList[meta.juz + 1] === ayahId + 1
44+
if (JuzList[meta.juz + 1] === ayahId) {
45+
meta.juz += 1
46+
meta.juzPart = 0
47+
meta.hizbId += 1
48+
meta.isStartOfJuz = true
49+
}
50+
else { meta.isStartOfJuz = false }
51+
52+
meta.isEndOfQuarter = HizbQuarterList[meta.rubAlHizbId + 1] === ayahId + 1
53+
if (HizbQuarterList[meta.rubAlHizbId + 1] === ayahId) {
54+
meta.rubAlHizbId += 1
55+
meta.juzPart += 1
56+
meta.isStartOfQuarter = true
57+
if (meta.juzPart === 5) meta.hizbId += 1
58+
}
59+
else { meta.isStartOfQuarter = false }
60+
61+
meta.isSajdahAyah = SajdaList.some(x => x[0] === ayahId)
62+
}
63+
result.push(meta)
64+
}
65+
66+
return result
67+
}

src/getList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HizbQuarterList } from "./lists/hizbList"
1+
import { HizbQuarterList } from "./lists/hizbQuarterList"
22
import { JuzList } from "./lists/juzList"
33
import { ManzilList } from "./lists/manzilList"
44
import { PageList } from "./lists/pageList"

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export { findSurahAyahByAyahId } from "./findSurahAyahByAyahId"
2121
export { findSurahByAyahId } from "./findSurahByAyahId"
2222
export { getAyahCountInSurah } from "./getAyahCountInSurah"
2323
export { getAyahMeta } from "./getAyahMeta"
24+
export { getAyahMetasForSurah } from "./getAyahMetasForSurah"
2425
export { getList } from "./getList"
2526
export { getPageMeta } from "./getPageMeta"
2627
export { getRubAlHizbMeta } from "./getRubAlHizbMeta"
@@ -30,7 +31,7 @@ export { isAyahJuzFirst } from "./isAyahJuzFirst"
3031
export { isAyahPageFirst } from "./isAyahPageFirst"
3132
export { isSurahAyahJuzFirst } from "./isSurahAyahJuzFirst"
3233
export { isSurahAyahPageFirst } from "./isSurahAyahPageFirst"
33-
export { HizbQuarterList } from "./lists/hizbList"
34+
export { HizbQuarterList } from "./lists/hizbQuarterList"
3435
export { JuzList } from "./lists/juzList"
3536
export { ManzilList } from "./lists/manzilList"
3637
export { PageList } from "./lists/pageList"
File renamed without changes.

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export type AyahMeta = {
135135
juzPart: JuzPart // 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
136136
hizbId: HizbId
137137
rubAlHizbId: RubAlHizbId
138+
page: Page
138139
// rub: number
139140
surah: Surah
140141
ayah: AyahNo

tests/getAyahMetasForSurah.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { describe, it, expect } from "vitest"
2+
import { getAyahMetasForSurah } from "../src/getAyahMetasForSurah"
3+
import { Surah } from "../src/types"
4+
import { getAyahMeta } from "../src/getAyahMeta"
5+
import { SurahList } from "../src"
6+
7+
describe("getAyahMetasForSurah", () => {
8+
it("should return correct number of ayahs for Al-Fatiha (surah 1)", () => {
9+
const result = getAyahMetasForSurah(1)
10+
expect(result).toHaveLength(7)
11+
expect(result).toBeInstanceOf(Array)
12+
expect(result[0]).toBeInstanceOf(Object)
13+
})
14+
15+
it("should return correct number of ayahs for Al-Fatiha (surah 1)", () => {
16+
const result = getAyahMetasForSurah(1)
17+
18+
expect(result).toHaveLength(7)
19+
expect(result).toBeInstanceOf(Array)
20+
expect(result[0]).toBeInstanceOf(Object)
21+
for (let id = 1; id <= 7; id++) {
22+
expect(result[id - 1]).toEqual(getAyahMeta(id))
23+
}
24+
})
25+
26+
it("should return correct number of ayahs for Al-Fatiha (surah 1)", () => {
27+
for (let i: Surah = 1; i <= 114; i++) {
28+
const result = getAyahMetasForSurah(i as Surah)
29+
const [
30+
startAyahId, ayahCount
31+
] = SurahList[i]
32+
for (let id = 1; id <= ayahCount; id++) {
33+
expect(result[id - 1]).toEqual(getAyahMeta(id + startAyahId - 1))
34+
}
35+
}
36+
})
37+
38+
it("should return array of AyahMeta objects with required properties", () => {
39+
const result = getAyahMetasForSurah(1)
40+
const firstAyah = result[0]
41+
// console.log(firstAyah)
42+
expect(firstAyah).toHaveProperty("ayah")
43+
expect(firstAyah).toHaveProperty("surah")
44+
expect(firstAyah).toHaveProperty("page")
45+
expect(firstAyah).toHaveProperty("juz")
46+
})
47+
48+
it("should throw RangeError for invalid surah number 0", () => {
49+
expect(() => getAyahMetasForSurah(0 as any)).toThrow(RangeError)
50+
})
51+
52+
it("should throw RangeError for invalid surah number 115", () => {
53+
expect(() => getAyahMetasForSurah(115 as any)).toThrow(RangeError)
54+
})
55+
56+
it("should return correct metadata for last surah (114)", () => {
57+
const result = getAyahMetasForSurah(114)
58+
expect(result).toHaveLength(6)
59+
expect(result[0].surah).toBe(114)
60+
})
61+
})

0 commit comments

Comments
 (0)