Skip to content

Commit bfb6e6c

Browse files
committed
full 100% coverage
1 parent 01b3ec9 commit bfb6e6c

File tree

5 files changed

+67
-18
lines changed

5 files changed

+67
-18
lines changed

src/findJuzHizbByAyaid.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export function findJuzHizbByAyaid(ayaId: AyahId): JuzHizb {
1414

1515
const juz = findJuzByAyaid(ayaId)
1616
const quarterIndex = HizbQuarterList.findIndex(x => x > ayaId) - 1
17-
if (quarterIndex < 0) {
18-
throw new Error("Invalid Ayah ID: No corresponding Hizb found.")
19-
}
17+
2018
const hizb = quarterIndex % 8 || 8
2119
return { juz, hizb, id: quarterIndex }
2220
}

src/validation.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import { getAyaCountinSura } from "./getAyaCountinSura"
99
* @returns True if the Surah number is valid, false otherwise.
1010
*/
1111
export function checkValidSurah(surah: number, checkOnly = false): boolean {
12+
if (typeof surah !== "number" || !Number.isInteger(surah)) {
13+
if (checkOnly) return false
14+
throw new TypeError("Ayah ID must be an integer")
15+
}
16+
1217
if (surah < 1 || surah > meta.numSuras) {
1318
if (checkOnly) return false
1419
throw new RangeError("Surah must be between 1 and " + meta.numSuras)
@@ -41,6 +46,10 @@ export function checkValidSurahAyah(surah: number, ayah: number, checkOnly = fal
4146
* @returns True if the Ayah ID is valid, otherwise throws a RangeError.
4247
*/
4348
export function checkValidAyahId(ayahId: number, checkOnly = false): boolean {
49+
if (typeof ayahId !== "number" || !Number.isInteger(ayahId)) {
50+
if (checkOnly) return false
51+
throw new TypeError("Ayah ID must be an integer")
52+
}
4453
if (ayahId < 1 || ayahId > meta.numAyas) {
4554
if (checkOnly) return false
4655
throw new RangeError("Ayah ID must be between 1 and " + meta.numAyas)

tests/checkValidAyahId.spec.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { checkValidAyahId, meta } from "../src"
22

33
describe("checkValidAyahId", () => {
4-
it("should return true for valid ayah id", () => {
5-
expect(checkValidAyahId(1)).toBe(true)
6-
expect(checkValidAyahId(meta.numAyas)).toBe(true)
7-
expect(checkValidAyahId(Math.floor(meta.numAyas / 2))).toBe(true)
8-
})
9-
104
it("should throw RangeError for ayah id less than 1", () => {
115
expect(() => checkValidAyahId(0)).toThrow(RangeError)
126
expect(() => checkValidAyahId(-1)).toThrow(RangeError)
@@ -17,14 +11,42 @@ describe("checkValidAyahId", () => {
1711
expect(() => checkValidAyahId(Number.MAX_SAFE_INTEGER)).toThrow(RangeError)
1812
})
1913

20-
// it("should throw RangeError for non-integer ayah id", () => {
21-
// expect(() => checkValidAyahId(1.5)).toThrow(RangeError);
22-
// expect(() => checkValidAyahId(2.99)).toThrow(RangeError);
23-
// });
14+
it("should throw TypeError for non-integer ayah id", () => {
15+
expect(() => checkValidAyahId(1.5)).toThrow(TypeError)
16+
expect(() => checkValidAyahId(2.99)).toThrow(TypeError)
17+
expect(() => checkValidAyahId(Math.PI)).toThrow(TypeError)
18+
})
19+
20+
it("should throw TypeError for NaN", () => {
21+
expect(() => checkValidAyahId(NaN)).toThrow(TypeError)
22+
})
23+
24+
it("should throw TypeError for Infinity", () => {
25+
expect(() => checkValidAyahId(Infinity)).toThrow(TypeError)
26+
expect(() => checkValidAyahId(-Infinity)).toThrow(TypeError)
27+
})
28+
29+
it("should handle checkOnly", () => {
30+
expect(checkValidAyahId(1, true)).toBe(true)
31+
expect(checkValidAyahId(meta.numAyas, true)).toBe(true)
32+
expect(checkValidAyahId(Math.floor(meta.numAyas / 2), true)).toBe(true)
33+
expect(checkValidAyahId(0, true)).toBe(false)
34+
expect(checkValidAyahId(meta.numAyas + 1, true)).toBe(false)
35+
expect(checkValidAyahId(1.5, true)).toBe(false)
36+
expect(checkValidAyahId(NaN, true)).toBe(false)
37+
expect(checkValidAyahId(Infinity, true)).toBe(false)
38+
})
2439

25-
// it("should throw TypeError for non-number ayah id", () => {
26-
// expect(() => checkValidAyahId("1" as any)).toThrow(TypeError);
27-
// expect(() => checkValidAyahId(null as any)).toThrow(TypeError);
28-
// expect(() => checkValidAyahId(undefined as any)).toThrow(TypeError);
29-
// });
40+
it("should handle edge cases correctly", () => {
41+
expect(checkValidAyahId(1)).toBe(true)
42+
expect(checkValidAyahId(meta.numAyas)).toBe(true)
43+
expect(() => checkValidAyahId(meta.numAyas + 0.5)).toThrow(TypeError)
44+
expect(checkValidAyahId(Math.floor(meta.numAyas / 2))).toBe(true)
45+
})
46+
47+
it("should throw TypeError for non-number ayah id", () => {
48+
expect(() => checkValidAyahId("1" as any)).toThrow(TypeError)
49+
expect(() => checkValidAyahId(null as any)).toThrow(TypeError)
50+
expect(() => checkValidAyahId(undefined as any)).toThrow(TypeError)
51+
})
3052
})

tests/checkValidSurah.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@ describe("checkValidSurah", () => {
1919

2020
it("should return false for invalid surah numbers when checkOnly is true", () => {
2121
expect(checkValidSurah(0, true)).toBe(false)
22+
expect(checkValidSurah(1.9, true)).toBe(false)
2223
expect(checkValidSurah(meta.numSuras + 1, true)).toBe(false)
2324
})
2425

2526
it("should return true for valid surah numbers when checkOnly is true", () => {
2627
expect(checkValidSurah(1, true)).toBe(true)
2728
expect(checkValidSurah(meta.numSuras, true)).toBe(true)
2829
})
30+
31+
it("should throw TypeError for non-number surah number", () => {
32+
expect(() => checkValidSurah("1" as any)).toThrow(TypeError)
33+
expect(() => checkValidSurah(null as any)).toThrow(TypeError)
34+
expect(() => checkValidSurah(undefined as any)).toThrow(TypeError)
35+
})
36+
37+
it("should throw TypeError for non-integer ayah id", () => {
38+
expect(() => checkValidSurah(1.5)).toThrow(TypeError)
39+
expect(() => checkValidSurah(2.99)).toThrow(TypeError)
40+
expect(() => checkValidSurah(Math.PI)).toThrow(TypeError)
41+
})
2942
})

tests/findJuzHizbByAyaid.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ describe("findJuzHizbByAyaid", () => {
4242
expect(spy).toHaveBeenCalledWith(100)
4343
})
4444

45+
describe("findJuzHizbByAyaid error handling", () => {
46+
it("should throw an error for invalid Ayah ID", () => {
47+
expect(() => findJuzHizbByAyaid(6237)).toThrow()
48+
expect(() => findJuzHizbByAyaid(0)).toThrow()
49+
})
50+
})
51+
4552
it("should handle edge case when ayaId is at hizb boundary", () => {
4653
const result = findJuzHizbByAyaid(148)
4754
expect(result).toEqual({ juz: 1, hizb: 8, id: 8 })

0 commit comments

Comments
 (0)