Skip to content

Commit 0845054

Browse files
committed
feat: Add rukus to meta
1 parent 3b02669 commit 0845054

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

src/getAyahMeta.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,27 @@ export function getAyahMeta(ayahId: AyahId): AyahMeta {
2424
const quarterData = getRubAlHizbMetaByAyahId(ayahId)
2525
const [surah, ayah] = findSurahAyahByAyahId(ayahId)
2626
const page: Page = findPagebyAyahId(ayahId)
27-
const isSajdahAyah = SajdaList.some(([sajdaAyahId]) => sajdaAyahId === ayahId)
27+
28+
// const isSajdahAyah = SajdaList.some(([sajdaAyahId]) => sajdaAyahId === ayahId)
29+
const isSajdahAyah = binarySearch(SajdaList, ayahId, (a, b) => a - b[0]) >= 0
30+
31+
const rk = binarySearch(RukuList, ayahId)
32+
const isStartOfRuku = rk > 0
33+
const ruku = isStartOfRuku ? rk : -rk - 2
34+
2835
const isStartOfSurah = SurahList[surah][0] === ayahId
2936
const isStartOfPage = PageList[page] === ayahId
30-
// const isStartOfRuku = RukuList[page] === ayahId
3137
const isStartOfJuz = JuzList[quarterData.juz] === ayahId
3238
const isStartOfQuarter = HizbQuarterList[quarterData.rubAlHizbId] === ayahId
3339
const isEndOfSurah = SurahList[surah + 1][0] - 1 === ayahId
3440
const isEndOfPage = PageList[page + 1] - 1 === ayahId
3541
const isEndOfJuz = JuzList[quarterData.juz + 1] - 1 === ayahId
36-
const isEndOfRuku = JuzList[quarterData.juz + 1] - 1 === ayahId
42+
const isEndOfRuku = binarySearch(RukuList, ayahId + 1) > 0
3743
const isEndOfQuarter = HizbQuarterList[quarterData.rubAlHizbId + 1] - 1 === ayahId
3844

3945
return {
4046
...quarterData, surah, ayah, page, isStartOfQuarter,
41-
isEndOfQuarter, isSajdahAyah, isStartOfPage, isEndOfPage,
42-
isStartOfJuz, isEndOfJuz, isStartOfSurah, isEndOfSurah
47+
isEndOfQuarter, isSajdahAyah, isStartOfPage, isEndOfPage, ruku,
48+
isStartOfJuz, isEndOfJuz, isStartOfSurah, isEndOfSurah, isStartOfRuku, isEndOfRuku
4349
}
4450
}

src/getAyahMetasForSurah.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getAyahMeta } from "./getAyahMeta"
22
import { HizbQuarterList } from "./lists/hizbQuarterList"
33
import { JuzList } from "./lists/juzList"
44
import { PageList } from "./lists/pageList"
5+
import { RukuList } from "./lists/rukuList"
56
import { SajdaList } from "./lists/sajdaList"
67
import { SurahList } from "./lists/surahList"
78
import { Surah, AyahMeta, AyahId, JuzPart } from "./types"
@@ -40,6 +41,13 @@ export function getAyahMetasForSurah(surahNumber: Surah): AyahMeta[] {
4041
else { meta.isStartOfPage = false }
4142
meta.isEndOfPage = PageList[meta.page + 1] === ayahId + 1
4243

44+
if (RukuList[meta.ruku + 1] === ayahId) {
45+
meta.ruku += 1
46+
meta.isStartOfRuku = true
47+
}
48+
else { meta.isStartOfRuku = false }
49+
meta.isEndOfRuku = RukuList[meta.ruku + 1] === ayahId + 1
50+
4351
meta.isEndOfJuz = JuzList[meta.juz + 1] === ayahId + 1
4452
if (JuzList[meta.juz + 1] === ayahId) {
4553
meta.juz += 1

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export type AyahMeta = {
136136
hizbId: HizbId
137137
rubAlHizbId: RubAlHizbId
138138
page: Page
139-
// rub: number
139+
ruku: number
140140
surah: Surah
141141
ayah: AyahNo
142142
isStartOfQuarter: boolean
@@ -148,4 +148,6 @@ export type AyahMeta = {
148148
isEndOfJuz: boolean
149149
isStartOfSurah: boolean
150150
isEndOfSurah: boolean
151+
isStartOfRuku: boolean
152+
isEndOfRuku: boolean
151153
}

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
export function binarySearch<T, X>(
1010
ar: Array<T>,
1111
el: X,
12-
compare_fn: (a: X, b: T) => number = (a, b) => a as number - (b as number)
13-
): number | -1 {
12+
compare_fn: (a: X, b: T) => number = (a, b) => (a as number) - (b as number)
13+
): number {
1414
let m = 0
1515
let n = ar.length - 1
1616
while (m <= n) {

tests/getAyahMeta.spec.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getAyahMeta, meta } from "../src"
1+
import { getAyahMeta, meta, RukuList } from "../src"
22

33
describe("ayahMeta", () => {
44
it("should return correct metadata for the first ayah", () => {
@@ -12,6 +12,9 @@ describe("ayahMeta", () => {
1212
expect(result.isEndOfJuz).toBeFalsy()
1313
expect(result.isEndOfQuarter).toBeFalsy()
1414
expect(result.isSajdahAyah).toBeFalsy()
15+
expect(result.isEndOfRuku).toBeFalsy()
16+
expect(result.isStartOfRuku).toBeTruthy()
17+
expect(result.ruku).toBe(1)
1518
})
1619

1720
it("should return correct metadata for the 7th ayah", () => {
@@ -23,6 +26,9 @@ describe("ayahMeta", () => {
2326
expect(result.isEndOfPage).toBeTruthy()
2427
expect(result.isEndOfJuz).toBeFalsy()
2528
expect(result.isSajdahAyah).toBeFalsy()
29+
expect(result.isStartOfRuku).toBeFalsy()
30+
expect(result.isEndOfRuku).toBeTruthy()
31+
expect(result.ruku).toBe(1)
2632
})
2733

2834
it("should return correct metadata for the 8th ayah", () => {
@@ -33,7 +39,36 @@ describe("ayahMeta", () => {
3339
expect(result.isEndOfSurah).toBeFalsy()
3440
expect(result.isEndOfPage).toBeFalsy()
3541
expect(result.isEndOfJuz).toBeFalsy()
42+
expect(result.isEndOfRuku).toBeFalsy()
43+
expect(result.isStartOfRuku).toBeTruthy()
44+
expect(result.ruku).toBe(2)
45+
expect(result.isSajdahAyah).toBeFalsy()
46+
})
3647

48+
it("should return correct metadata for the 14th ayah", () => {
49+
const result = getAyahMeta(14)
50+
expect(result.isStartOfSurah).toBeFalsy()
51+
// expect(result.isStartOfPage).toBeTruthy()
52+
expect(result.isStartOfJuz).toBeFalsy()
53+
expect(result.isEndOfSurah).toBeFalsy()
54+
expect(result.isEndOfPage).toBeFalsy()
55+
expect(result.isEndOfJuz).toBeFalsy()
56+
expect(result.isEndOfRuku).toBeTruthy()
57+
expect(result.isStartOfRuku).toBeFalsy()
58+
expect(result.ruku).toBe(2)
59+
expect(result.isSajdahAyah).toBeFalsy()
60+
})
61+
it("should return correct metadata for the 15th ayah", () => {
62+
const result = getAyahMeta(15)
63+
expect(result.isStartOfSurah).toBeFalsy()
64+
// expect(result.isStartOfPage).toBeTruthy()
65+
expect(result.isStartOfJuz).toBeFalsy()
66+
expect(result.isEndOfSurah).toBeFalsy()
67+
expect(result.isEndOfPage).toBeFalsy()
68+
expect(result.isEndOfJuz).toBeFalsy()
69+
expect(result.isEndOfRuku).toBeFalsy()
70+
expect(result.isStartOfRuku).toBeTruthy()
71+
expect(result.ruku).toBe(3)
3772
expect(result.isSajdahAyah).toBeFalsy()
3873
})
3974

@@ -46,16 +81,19 @@ describe("ayahMeta", () => {
4681
expect(result.isEndOfPage).toBeTruthy()
4782
expect(result.isEndOfJuz).toBeTruthy()
4883
expect(result.isSajdahAyah).toBeFalsy()
84+
expect(result.isEndOfRuku).toBeTruthy()
85+
expect(result.isEndOfQuarter).toBeTruthy()
86+
expect(result.ruku).toBe(meta.numRukus)
4987
})
5088

5189
it("1160 is SajdahAyah", () => {
5290
const result = getAyahMeta(1160)
5391
expect(result.isSajdahAyah).toBeTruthy()
5492
})
5593

56-
it("1160 is SajdahAyah", () => {
57-
const result = getAyahMeta(1160)
58-
expect(result.isSajdahAyah).toBeTruthy()
94+
it("1161 is not SajdahAyah", () => {
95+
const result = getAyahMeta(1161)
96+
expect(result.isSajdahAyah).toBeFalsy()
5997
})
6098

6199
it("should throw RangeError for ayah number below 1", () => {
@@ -65,4 +103,13 @@ describe("ayahMeta", () => {
65103
it("should throw RangeError for ayah number above numAyahs", () => {
66104
expect(() => getAyahMeta(meta.numAyahs + 1)).toThrow(RangeError)
67105
})
106+
107+
108+
it("should return correct metadata for ayah 6235", () => {
109+
const result = getAyahMeta(6235)
110+
expect(result.isStartOfRuku).toBeFalsy()
111+
expect(result.isEndOfRuku).toBeFalsy()
112+
expect(result.isEndOfQuarter).toBeFalsy()
113+
expect(result.ruku).toBe(meta.numRukus)
114+
})
68115
})

0 commit comments

Comments
 (0)