Skip to content

Commit 45569fd

Browse files
committed
feat: add getRukuMeta
1 parent f00a67e commit 45569fd

File tree

8 files changed

+102
-14
lines changed

8 files changed

+102
-14
lines changed

src/findRangeAroundAyah.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { meta } from "./const"
22
import { findJuzByAyahId } from "./findJuzByAyahId"
33
import { findPagebyAyahId } from "./findPagebyAyahId"
4+
import { findRukuByAyahId } from "./findRukuByAyahId"
45
import { findSurahByAyahId } from "./findSurahByAyahId"
56
import { JuzList } from "./lists/juzList"
67
import { PageList } from "./lists/pageList"
8+
import { RukuList } from "./lists/rukuList"
79
import { SurahList } from "./lists/surahList"
8-
import { AyahId, AyahRange, Juz, Page, Surah } from "./types"
10+
import { AyahId, AyahRange, Juz, Page, Ruku, Surah } from "./types"
911

1012
/**
1113
* Finds the range of ayahs surrounding a given ayah based on specified mode
@@ -15,12 +17,13 @@ import { AyahId, AyahRange, Juz, Page, Surah } from "./types"
1517
* - "surah": Returns range of ayahs in the same surah
1618
* - "ayah": Returns the single ayah as both start and end of range
1719
* - "page": Returns range of ayahs on the same page
20+
* - "ruku": Returns range of ayahs on the same ruku
1821
* - "all": Returns range covering all ayahs (1 to total number of ayahs)
1922
* @returns An array of two numbers representing the start and end ayah IDs of the range [startAyahId, endAyahId]
2023
*/
2124
export function findRangeAroundAyah(
2225
ayahId: AyahId,
23-
mode: "juz" | "surah" | "ayah" | "page" | "all"
26+
mode: "juz" | "surah" | "ayah" | "page" | "ruku" | "all"
2427
): AyahRange {
2528
switch (mode) {
2629
case "juz": {
@@ -42,6 +45,11 @@ export function findRangeAroundAyah(
4245
return [PageList[page], PageList[page + 1] - 1]
4346
}
4447

48+
case "ruku": {
49+
const ruku: Ruku = findRukuByAyahId(ayahId)
50+
return [RukuList[ruku], RukuList[ruku + 1] - 1]
51+
}
52+
4553
case "all":
4654
default:
4755
return [1, meta.numAyahs]

src/getJuzMeta.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ import { checkValidJuz } from "./validation"
1313
export function getJuzMeta(juzNum: number): JuzMeta {
1414
checkValidJuz(juzNum)
1515

16-
const [curJuzAyahId, nextJuzAyahId]: [AyahId, AyahId] = [
16+
const [firstAyahId, nextJuzAyahId]: [AyahId, AyahId] = [
1717
JuzList[juzNum],
1818
JuzList[juzNum + 1]
1919
]
20-
20+
const lastAyahId = nextJuzAyahId - 1
2121
return {
2222
juzNum,
23-
first: findSurahAyahByAyahId(curJuzAyahId),
24-
last: findSurahAyahByAyahId(nextJuzAyahId - 1)
23+
firstAyahId,
24+
lastAyahId,
25+
first: findSurahAyahByAyahId(firstAyahId),
26+
last: findSurahAyahByAyahId(lastAyahId)
2527
}
2628
}

src/getPageMeta.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ import { checkValidPage } from "./validation"
1313
export function getPageMeta(pageNum: Page): PageMeta {
1414
checkValidPage(pageNum)
1515

16-
const [curPage, nextPage]: [AyahId, AyahId] = [
16+
const [firstAyahId, nextPage]: [AyahId, AyahId] = [
1717
PageList[pageNum],
1818
PageList[pageNum + 1]
1919
]
20+
const lastAyahId = nextPage - 1
2021

2122
return {
2223
pageNum,
23-
first: findSurahAyahByAyahId(curPage),
24-
last: findSurahAyahByAyahId(nextPage - 1)
24+
firstAyahId,
25+
lastAyahId,
26+
first: findSurahAyahByAyahId(firstAyahId),
27+
last: findSurahAyahByAyahId(lastAyahId)
2528
}
2629
}

src/getRukuMeta.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { findSurahAyahByAyahId } from "./findSurahAyahByAyahId"
2+
import { RukuList } from "./lists/rukuList"
3+
import { AyahId, RukuMeta } from "./types"
4+
import { checkValidRuku } from "./validation"
5+
6+
/**
7+
* Retrieves metadata for a specific Ruku (section) of the Quran.
8+
* @param rukuNum - The number of the Ruku to retrieve metadata for
9+
* @returns {RukuMeta} An object containing metadata about the Ruku including:
10+
* - rukuNum: The Ruku number
11+
* - firstAyahId: The global Ayah ID of the first verse in this Ruku
12+
* - lastAyahId: The global Ayah ID of the last verse in this Ruku
13+
* - first: The Surah and Ayah numbers for the first verse
14+
* - last: The Surah and Ayah numbers for the last verse
15+
* @throws Will throw an error if the provided Ruku number is invalid
16+
*/
17+
export function getRukuMeta(rukuNum: number): RukuMeta {
18+
checkValidRuku(rukuNum)
19+
20+
const [firstAyahId, nextRukuAyahId]: [AyahId, AyahId] = [
21+
RukuList[rukuNum],
22+
RukuList[rukuNum + 1]
23+
]
24+
const lastAyahId = nextRukuAyahId - 1
25+
return {
26+
rukuNum,
27+
firstAyahId,
28+
lastAyahId,
29+
first: findSurahAyahByAyahId(firstAyahId),
30+
last: findSurahAyahByAyahId(lastAyahId)
31+
}
32+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export { getAyahMeta } from "./getAyahMeta"
2525
export { getAyahMetasForSurah } from "./getAyahMetasForSurah"
2626
export { getList } from "./getList"
2727
export { getPageMeta } from "./getPageMeta"
28+
export { getRukuMeta } from "./getRukuMeta"
2829
export { getRubAlHizbMeta } from "./getRubAlHizbMeta"
2930
export { getRubAlHizbMetaByAyahId } from "./getRubAlHizbMetaByAyahId"
3031
export { getSurahMeta } from "./getSurahMeta"

src/types.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,26 @@ export type JuzHizb = {
9595
export type SurahAyah = [Surah, AyahNo]
9696
export type AyahRange = [AyahId, AyahId]
9797
export type SurahAyahSegment = [Surah, AyahNo | [AyahNo, AyahNo]]
98-
export type PageMeta = {
99-
pageNum: Page
98+
99+
export type RangeMeta = {
100+
firstAyahId: AyahId
101+
lastAyahId: AyahId
100102
first: SurahAyah
101103
last: SurahAyah
102104
}
103105

106+
export type PageMeta = {
107+
pageNum: Page
108+
} & RangeMeta
109+
104110
export type JuzMeta = {
105111
juzNum: Juz
106-
first: SurahAyah
107-
last: SurahAyah
108-
}
112+
} & RangeMeta
113+
114+
export type RukuMeta = {
115+
rukuNum: Ruku
116+
} & RangeMeta
117+
109118
// [leftjuz, ayahsFromStartOfJuz, rightJuz, ayahsinJuz]
110119
export type AyahCountBetweenJuzSurah = NumericRange<0, typeof maxAyahsInSurah>
111120
export type SurahJuzMeta = {

tests/getJuzMeta.test.ts renamed to tests/getJuzMeta.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe("getJuzMeta", () => {
77

88
expect(result).toEqual({
99
juzNum: 1,
10+
firstAyahId: 1,
11+
lastAyahId: 148,
1012
first: [1, 1],
1113
last: [2, 141]
1214
})

tests/getRukuMeta.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, expect, it } from "vitest"
2+
import { getRukuMeta } from "../src/getRukuMeta"
3+
4+
describe("getRukuMeta", () => {
5+
it("should return correct metadata for first ruku", () => {
6+
const result = getRukuMeta(1)
7+
expect(result).toEqual({
8+
rukuNum: 1,
9+
firstAyahId: 1,
10+
lastAyahId: 7,
11+
first: [1, 1],
12+
last: [1, 7]
13+
})
14+
})
15+
16+
it("should return correct metadata for a middle ruku", () => {
17+
const result = getRukuMeta(100)
18+
expect(result).toMatchObject({
19+
rukuNum: 100,
20+
first: expect.any(Object),
21+
last: expect.any(Object)
22+
})
23+
expect(result.firstAyahId).toBeLessThan(result.lastAyahId)
24+
})
25+
26+
it("should throw error for invalid ruku number", () => {
27+
expect(() => getRukuMeta(0)).toThrow()
28+
expect(() => getRukuMeta(-1)).toThrow()
29+
expect(() => getRukuMeta(557)).toThrow()
30+
})
31+
})

0 commit comments

Comments
 (0)