1
+ import {
2
+ ChapterId ,
3
+ HizbNumber ,
4
+ JuzNumber ,
5
+ PageNumber ,
6
+ RubNumber ,
7
+ VerseKey ,
8
+ } from '../types' ;
9
+
1
10
// this maps chapterNumber to verseCount
2
11
export const versesMapping = {
3
12
'1' : 7 ,
@@ -126,7 +135,7 @@ export const versesMapping = {
126
135
isValidChapterId('-1') // false
127
136
isValidChapterId('200') // false
128
137
*/
129
- const isValidChapterId = ( id : string | number ) => {
138
+ const isValidChapterId = ( id : string | number ) : id is ChapterId => {
130
139
const parsedId = typeof id === 'number' ? id : Number ( id ) ;
131
140
if ( ! parsedId || parsedId <= 0 || parsedId > 114 ) return false ;
132
141
return true ;
@@ -142,7 +151,7 @@ const isValidChapterId = (id: string | number) => {
142
151
isValidJuz('-1') // false
143
152
isValidJuz('200') // false
144
153
*/
145
- const isValidJuz = ( juz : string | number ) => {
154
+ const isValidJuz = ( juz : string | number ) : juz is JuzNumber => {
146
155
const parsedJuz = typeof juz === 'number' ? juz : Number ( juz ) ;
147
156
if ( ! parsedJuz || parsedJuz <= 0 || parsedJuz > 30 ) return false ;
148
157
return true ;
@@ -158,7 +167,7 @@ const isValidJuz = (juz: string | number) => {
158
167
isValidRub('-1') // false
159
168
isValidRub('300') // false
160
169
*/
161
- const isValidRub = ( rub : string | number ) => {
170
+ const isValidRub = ( rub : string | number ) : rub is RubNumber => {
162
171
const parsedRub = typeof rub === 'number' ? rub : Number ( rub ) ;
163
172
if ( ! parsedRub || parsedRub <= 0 || parsedRub > 240 ) return false ;
164
173
return true ;
@@ -174,7 +183,7 @@ const isValidRub = (rub: string | number) => {
174
183
isValidHizb('-1') // false
175
184
isValidHizb('200') // false
176
185
*/
177
- const isValidHizb = ( hizb : string | number ) => {
186
+ const isValidHizb = ( hizb : string | number ) : hizb is HizbNumber => {
178
187
const parsedHizb = typeof hizb === 'number' ? hizb : Number ( hizb ) ;
179
188
if ( ! parsedHizb || parsedHizb <= 0 || parsedHizb > 60 ) return false ;
180
189
return true ;
@@ -190,7 +199,7 @@ const isValidHizb = (hizb: string | number) => {
190
199
isValidQuranPage('-1') // false
191
200
isValidQuranPage('1000') // false
192
201
*/
193
- const isValidQuranPage = ( page : string | number ) => {
202
+ const isValidQuranPage = ( page : string | number ) : page is PageNumber => {
194
203
const parsedPage = typeof page === 'number' ? page : Number ( page ) ;
195
204
if ( ! parsedPage || parsedPage <= 0 || parsedPage > 604 ) return false ;
196
205
return true ;
@@ -206,19 +215,19 @@ const isValidQuranPage = (page: string | number) => {
206
215
isValidVerseKey('1:-') // false
207
216
isValidVerseKey('1_1') // false
208
217
*/
209
- const isValidVerseKey = ( key : string ) => {
218
+ const isValidVerseKey = ( key : string ) : key is VerseKey => {
210
219
const [ chapterId , verseId ] = key . trim ( ) . split ( ':' ) ;
211
220
if ( ! chapterId || ! verseId || ! isValidChapterId ( chapterId ) ) return false ;
212
221
213
222
const parsedVerse = Number ( verseId ) ;
214
- const verseCount = ( versesMapping as any ) [ chapterId ] ;
223
+ const verseCount = ( versesMapping as Record < string , number > ) [ chapterId ] ;
215
224
if ( ! parsedVerse || parsedVerse <= 0 || parsedVerse > verseCount )
216
225
return false ;
217
226
218
227
return true ;
219
228
} ;
220
229
221
- const Utils = {
230
+ const utils = {
222
231
isValidChapterId,
223
232
isValidJuz,
224
233
isValidRub,
@@ -227,4 +236,4 @@ const Utils = {
227
236
isValidVerseKey,
228
237
} ;
229
238
230
- export default Utils ;
239
+ export default utils ;
0 commit comments