1+ import {
2+ ChapterId ,
3+ HizbNumber ,
4+ JuzNumber ,
5+ PageNumber ,
6+ RubNumber ,
7+ VerseKey ,
8+ } from '../types' ;
9+
110// this maps chapterNumber to verseCount
211export const versesMapping = {
312 '1' : 7 ,
@@ -126,7 +135,7 @@ export const versesMapping = {
126135 isValidChapterId('-1') // false
127136 isValidChapterId('200') // false
128137 */
129- const isValidChapterId = ( id : string | number ) => {
138+ const isValidChapterId = ( id : string | number ) : id is ChapterId => {
130139 const parsedId = typeof id === 'number' ? id : Number ( id ) ;
131140 if ( ! parsedId || parsedId <= 0 || parsedId > 114 ) return false ;
132141 return true ;
@@ -142,7 +151,7 @@ const isValidChapterId = (id: string | number) => {
142151 isValidJuz('-1') // false
143152 isValidJuz('200') // false
144153 */
145- const isValidJuz = ( juz : string | number ) => {
154+ const isValidJuz = ( juz : string | number ) : juz is JuzNumber => {
146155 const parsedJuz = typeof juz === 'number' ? juz : Number ( juz ) ;
147156 if ( ! parsedJuz || parsedJuz <= 0 || parsedJuz > 30 ) return false ;
148157 return true ;
@@ -158,7 +167,7 @@ const isValidJuz = (juz: string | number) => {
158167 isValidRub('-1') // false
159168 isValidRub('300') // false
160169 */
161- const isValidRub = ( rub : string | number ) => {
170+ const isValidRub = ( rub : string | number ) : rub is RubNumber => {
162171 const parsedRub = typeof rub === 'number' ? rub : Number ( rub ) ;
163172 if ( ! parsedRub || parsedRub <= 0 || parsedRub > 240 ) return false ;
164173 return true ;
@@ -174,7 +183,7 @@ const isValidRub = (rub: string | number) => {
174183 isValidHizb('-1') // false
175184 isValidHizb('200') // false
176185 */
177- const isValidHizb = ( hizb : string | number ) => {
186+ const isValidHizb = ( hizb : string | number ) : hizb is HizbNumber => {
178187 const parsedHizb = typeof hizb === 'number' ? hizb : Number ( hizb ) ;
179188 if ( ! parsedHizb || parsedHizb <= 0 || parsedHizb > 60 ) return false ;
180189 return true ;
@@ -190,7 +199,7 @@ const isValidHizb = (hizb: string | number) => {
190199 isValidQuranPage('-1') // false
191200 isValidQuranPage('1000') // false
192201 */
193- const isValidQuranPage = ( page : string | number ) => {
202+ const isValidQuranPage = ( page : string | number ) : page is PageNumber => {
194203 const parsedPage = typeof page === 'number' ? page : Number ( page ) ;
195204 if ( ! parsedPage || parsedPage <= 0 || parsedPage > 604 ) return false ;
196205 return true ;
@@ -206,19 +215,19 @@ const isValidQuranPage = (page: string | number) => {
206215 isValidVerseKey('1:-') // false
207216 isValidVerseKey('1_1') // false
208217 */
209- const isValidVerseKey = ( key : string ) => {
218+ const isValidVerseKey = ( key : string ) : key is VerseKey => {
210219 const [ chapterId , verseId ] = key . trim ( ) . split ( ':' ) ;
211220 if ( ! chapterId || ! verseId || ! isValidChapterId ( chapterId ) ) return false ;
212221
213222 const parsedVerse = Number ( verseId ) ;
214- const verseCount = ( versesMapping as any ) [ chapterId ] ;
223+ const verseCount = ( versesMapping as Record < string , number > ) [ chapterId ] ;
215224 if ( ! parsedVerse || parsedVerse <= 0 || parsedVerse > verseCount )
216225 return false ;
217226
218227 return true ;
219228} ;
220229
221- const Utils = {
230+ const utils = {
222231 isValidChapterId,
223232 isValidJuz,
224233 isValidRub,
@@ -227,4 +236,4 @@ const Utils = {
227236 isValidVerseKey,
228237} ;
229238
230- export default Utils ;
239+ export default utils ;
0 commit comments