Skip to content

Commit a678af0

Browse files
committed
feat: add ruku validation
1 parent 84f5afe commit a678af0

File tree

5 files changed

+113
-96
lines changed

5 files changed

+113
-96
lines changed

CHANGELOG.md

Lines changed: 8 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ All notable changes to this project will be documented in this file. See [standa
55

66
## v4.0.4-7
77

8-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.4-6...v4.0.4-7)
8+
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.3...v4.0.4-7)
99

1010
### 🚀 Enhancements
1111

1212
- Add findRukuByAyahId (b89a77d)
13+
- Add rukus to meta (0845054)
14+
- Added getAyahMetasForSurah (a696010)
1315

1416
### 🏡 Chore
1517

@@ -19,50 +21,6 @@ All notable changes to this project will be documented in this file. See [standa
1921

2022
- HG ([@husayt](https://github.com/husayt))
2123

22-
## v4.0.4-6
23-
24-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.4-5...v4.0.4-6)
25-
26-
## v4.0.4-5
27-
28-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.4-4...v4.0.4-5)
29-
30-
## v4.0.4-4
31-
32-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.4-3...v4.0.4-4)
33-
34-
### 🚀 Enhancements
35-
36-
- Add rukus to meta (0845054)
37-
38-
### ❤️ Contributors
39-
40-
- HG ([@husayt](https://github.com/husayt))
41-
42-
## v4.0.4-3
43-
44-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.4-2...v4.0.4-3)
45-
46-
## v4.0.4-2
47-
48-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.4-1...v4.0.4-2)
49-
50-
## v4.0.4-1
51-
52-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.4-0...v4.0.4-1)
53-
54-
## v4.0.4-0
55-
56-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.3...v4.0.4-0)
57-
58-
### 🚀 Enhancements
59-
60-
- Added getAyahMetasForSurah (a696010)
61-
62-
### ❤️ Contributors
63-
64-
- HG ([@husayt](https://github.com/husayt))
65-
6624
## v4.0.3
6725

6826
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.2...v4.0.3)
@@ -78,7 +36,7 @@ All notable changes to this project will be documented in this file. See [standa
7836

7937
## v4.0.1
8038

81-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v4.0.1-0...v4.0.1)
39+
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v3.2.1...v4.0.1)
8240

8341
**Breaking changes in this version**
8442

@@ -103,6 +61,7 @@ By leveraging TypeScript and proper type usage, many issues can be identified ea
10361
- Better type guards (472366e)
10462
- Added type guards for Hizb and RubAlhizb
10563
- Add more guards and `getJuzMeta` function (1a6bbe2)
64+
- Major update better types and many fixes (b556bc9)
10665

10766
### 🏡 Chore
10867

@@ -114,21 +73,6 @@ By leveraging TypeScript and proper type usage, many issues can be identified ea
11473
- Updated docs (8cf8fe4)
11574
- Add more checks (4a3640a)
11675
- Fix docs (e953d37)
117-
118-
### ❤️ Contributors
119-
120-
- HG ([@husayt](http://github.com/husayt))
121-
122-
## v4.0.1-0
123-
124-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v3.2.1...v4.0.1-0)
125-
126-
### 🚀 Enhancements
127-
128-
- Major update better types and many fixes (b556bc9)
129-
130-
### 🏡 Chore
131-
13276
- Fix tests (15f22bd)
13377
- Fix docs (85dc3a8)
13478

@@ -155,22 +99,11 @@ By leveraging TypeScript and proper type usage, many issues can be identified ea
15599

156100
## v3.2.0
157101

158-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v3.1.1-0...v3.2.0)
102+
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v3.1.0...v3.2.0)
159103

160104
### 🚀 Enhancements
161105

162106
- Update dependencies (be0d65b)
163-
164-
### ❤️ Contributors
165-
166-
- HG ([@husayt](http://github.com/husayt))
167-
168-
## v3.1.1-0
169-
170-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v3.1.0...v3.1.1-0)
171-
172-
### 🚀 Enhancements
173-
174107
- Add getList Method (5f17784)
175108

176109
### 🏡 Chore
@@ -267,27 +200,11 @@ Full unit test coverage has been achieved. Furthermore, in order to guarantee ab
267200

268201
## v2.5.3
269202

270-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v2.5.3-0...v2.5.3)
271-
272-
## v2.5.3-0
273-
274-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v2.5.2...v2.5.3-0)
203+
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v2.5.2...v2.5.3)
275204

276205
## v2.5.2
277206

278-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v2.5.2-3...v2.5.2)
279-
280-
## v2.5.2-3
281-
282-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v2.5.2-2...v2.5.2-3)
283-
284-
## v2.5.2-2
285-
286-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v2.5.2-1...v2.5.2-2)
287-
288-
## v2.5.2-0
289-
290-
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v2.4.15...v2.5.2-0)
207+
[compare changes](https://git+https@github.com/quran-center/quran-meta/compare/v2.4.15...v2.5.2)
291208

292209
### 🚀 Enhancements
293210

src/typeGuards.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { maxAyahsInSurah, meta } from "./const"
22
import { getAyahCountInSurah } from "./getAyahCountInSurah"
3-
import { AyahId, AyahNo, HizbId, Juz, RubAlHizbId, Surah, SurahAyah } from "./types"
3+
import { AyahId, AyahNo, HizbId, Juz, RubAlHizbId, Ruku, Surah, SurahAyah } from "./types"
44

55
/**
66
* Checks if the given value is a valid AyahId.
@@ -89,3 +89,18 @@ export function isValidRubAlHizb(x: unknown): x is RubAlHizbId {
8989
export function isValidPage(x: unknown): x is Juz {
9090
return Number.isInteger(x) && 1 <= (x as number) && x as number <= meta.numPages
9191
}
92+
93+
/**
94+
* Type guard that checks if a value is a valid Ruku number
95+
* @param x - The value to check
96+
* @returns True if x is an integer between 1 and the total number of Rukus
97+
* @example
98+
* ```ts
99+
* if (isValidRuku(5)) {
100+
* // 5 is a valid Ruku number
101+
* }
102+
* ```
103+
*/
104+
export function isValidRuku(x: unknown): x is Ruku {
105+
return Number.isInteger(x) && 1 <= (x as number) && x as number <= meta.numRukus
106+
}

src/validation.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { meta } from "./const"
22
import { getAyahCountInSurah } from "./getAyahCountInSurah"
3-
import { isValidAyahId, isValidJuz, isValidPage, isValidSurah } from "./typeGuards"
4-
import { AyahId, AyahNo, Juz, Page, Surah, SurahAyah } from "./types"
3+
import { isValidAyahId, isValidJuz, isValidPage, isValidRuku, isValidSurah } from "./typeGuards"
4+
import { AyahId, AyahNo, Juz, Page, Ruku, Surah, SurahAyah } from "./types"
55

66
/**
77
* Validates if the provided value is a valid Surah number.
@@ -68,6 +68,13 @@ export function checkValidAyahId(ayahId: unknown | number | AyahId): asserts aya
6868
}
6969
}
7070

71+
/**
72+
* Checks if a value is a valid Page number.
73+
* @param x - The value to check
74+
* @throws {TypeError} When the value is not an integer
75+
* @throws {RangeError} When the value is not within valid page range (1 to numPages)
76+
* @remarks This is a type assertion function that ensures a value is a valid Page number
77+
*/
7178
export function checkValidPage(x: unknown | number | Page): asserts x is Page {
7279
if (typeof x !== "number" || !Number.isInteger(x)) {
7380
throw new TypeError("Page must be an integer")
@@ -77,6 +84,15 @@ export function checkValidPage(x: unknown | number | Page): asserts x is Page {
7784
}
7885
}
7986

87+
/**
88+
* Type guard that checks if a value is a valid Juz number.
89+
* Throws TypeError if value is not an integer.
90+
* Throws RangeError if value is outside valid Juz range.
91+
*
92+
* @param x - Value to check
93+
* @throws {TypeError} If value is not an integer
94+
* @throws {RangeError} If value is not between 1 and the total number of Juz
95+
*/
8096
export function checkValidJuz(x: unknown | number | Juz): asserts x is Juz {
8197
if (typeof x !== "number" || !Number.isInteger(x)) {
8298
throw new TypeError("Juz must be an integer")
@@ -85,3 +101,24 @@ export function checkValidJuz(x: unknown | number | Juz): asserts x is Juz {
85101
throw new RangeError("Juz must be between 1 and " + meta.numJuzs)
86102
}
87103
}
104+
105+
/**
106+
* Type guard that checks if a value is a valid Ruku number.
107+
* @param x The value to check
108+
* @throws {TypeError} If the value is not an integer number
109+
* @throws {RangeError} If the number is not within valid Ruku range
110+
* @example
111+
* ```typescript
112+
* checkValidRuku(5); // OK
113+
* checkValidRuku("5"); // Throws TypeError
114+
* checkValidRuku(999); // Throws RangeError
115+
* ```
116+
*/
117+
export function checkValidRuku(x: unknown | number | Ruku): asserts x is Ruku {
118+
if (typeof x !== "number" || !Number.isInteger(x)) {
119+
throw new TypeError("Ruku must be an integer")
120+
}
121+
if (!isValidRuku(x)) {
122+
throw new RangeError("Ruku must be between 1 and " + meta.numJuzs)
123+
}
124+
}

tests/typeGuards.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { isValidHizb, isValidJuz, isValidRubAlHizb, isValidSurah, isValidSurahAyah } from "../src/typeGuards"
1+
import { meta } from "../src"
2+
import { isValidHizb, isValidJuz, isValidRubAlHizb, isValidRuku, isValidSurah, isValidSurahAyah } from "../src/typeGuards"
23

34
describe("isValidJuz", () => {
45
it("should return true for valid juz numbers", () => {
@@ -113,4 +114,25 @@ describe("isValidHizb", () => {
113114
expect(isValidHizb(NaN)).toBe(false)
114115
expect(isValidHizb(Infinity)).toBe(false)
115116
})
117+
118+
describe("isValidRuku", () => {
119+
it("should return true for valid ruku numbers", () => {
120+
expect(isValidRuku(1)).toBe(true)
121+
expect(isValidRuku(30)).toBe(true)
122+
expect(isValidRuku(60)).toBe(true)
123+
})
124+
125+
it("should return false for invalid ruku numbers", () => {
126+
expect(isValidRuku(0)).toBe(false)
127+
expect(isValidRuku(-1)).toBe(false)
128+
expect(isValidRuku(meta.numRukus + 1)).toBe(false)
129+
})
130+
131+
it("should return false for non-integer values", () => {
132+
expect(isValidRuku("1")).toBe(false)
133+
expect(isValidRuku(1.5)).toBe(false)
134+
expect(isValidRuku(NaN)).toBe(false)
135+
expect(isValidRuku(Infinity)).toBe(false)
136+
})
137+
})
116138
})

tests/validation.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest"
2-
import { checkValidJuz, checkValidPage } from "../src/validation"
2+
import { checkValidJuz, checkValidPage, checkValidRuku } from "../src/validation"
33
import { meta } from "../src/const"
44

55
describe("checkValidPage", () => {
@@ -51,5 +51,31 @@ describe("checkValidPage", () => {
5151
expect(() => checkValidJuz(-1)).toThrow(RangeError)
5252
expect(() => checkValidJuz(meta.numJuzs + 1)).toThrow(RangeError)
5353
})
54+
55+
describe("checkValidRuku", () => {
56+
it("should accept valid ruku numbers", () => {
57+
expect(() => checkValidRuku(1)).not.toThrow()
58+
expect(() => checkValidRuku(meta.numRukus)).not.toThrow()
59+
expect(() => checkValidRuku(15)).not.toThrow()
60+
})
61+
62+
it("should throw TypeError for non-number values", () => {
63+
expect(() => checkValidRuku("1")).toThrow(TypeError)
64+
expect(() => checkValidRuku(null)).toThrow(TypeError)
65+
expect(() => checkValidRuku(undefined)).toThrow(TypeError)
66+
expect(() => checkValidRuku({})).toThrow(TypeError)
67+
})
68+
69+
it("should throw TypeError for non-integer numbers", () => {
70+
expect(() => checkValidRuku(1.5)).toThrow(TypeError)
71+
expect(() => checkValidRuku(2.7)).toThrow(TypeError)
72+
})
73+
74+
it("should throw RangeError for out of range values", () => {
75+
expect(() => checkValidRuku(0)).toThrow(RangeError)
76+
expect(() => checkValidRuku(-1)).toThrow(RangeError)
77+
expect(() => checkValidRuku(meta.numRukus + 1)).toThrow(RangeError)
78+
})
79+
})
5480
})
5581
})

0 commit comments

Comments
 (0)