batoulapps / adhan-js

High precision Islamic prayer time library for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prayers Time in Array

farisfaisalthena opened this issue · comments

Would it be possible for the prayers response to return as an array alongside the names? Something like below

calculationParameters: {...}
fajrAngle: {...}
coordinates: {...}
date: Mon Jul 05 2021 11:35:42 GMT+0800 (Malaysia Time)
prayers: [
{ name: fajr, time: Mon Jul 05 2021 05:45:00 GMT+0800 (Malaysia Time) },
{ name: sunrise, time: Mon Jul 05 2021 05:45:00 GMT+0800 (Malaysia Time) },
{ name: dhuhr, time: Mon Jul 05 2021 05:45:00 GMT+0800 (Malaysia Time) },
{ name: asr, time: Mon Jul 05 2021 05:45:00 GMT+0800 (Malaysia Time) },
{ name: maghrib, time: Mon Jul 05 2021 05:45:00 GMT+0800 (Malaysia Time) },
{ name: isha, time: Mon Jul 05 2021 05:45:00 GMT+0800 (Malaysia Time) },
]

You can do it quite simply with the following :

const prayersTimes = new adhan.PrayersTimes(...);
prayersTimes.prayers = [adhan.Prayer.Fajr, adhan.Prayer.Sunrise, adhan.Prayer.Dhuhr, adhan.Prayer.Asr, adhan.Prayer.Maghrib, adhan.Prayer.Isha]
      .map((name) => ({ name, time: prayersTimes[name] }))

For your information, we are currently working on a scheduler, I was intending to offer such a current method, so hopefully, it will be part of the implementation.

alright then. just to add the implementation below:

const prayers = [adhan.Prayer.Fajr, adhan.Prayer.Sunrise, adhan.Prayer.Dhuhr, adhan.Prayer.Asr, adhan.Prayer.Maghrib, adhan.Prayer.Isha].map((name) => ({ name, time: this.time[name] }))

      const final = {
        prayers,
        nextPrayers: {
          name: this.time.nextPrayer(),
          time: this.time.timeForPrayer(this.time.nextPrayer())
        }
      }

This also includes the next prayers information. Besides that you could also add the current prayers in there. Thanks!

Just curious why does this.time.nextPrayer() not return a string? seems like the interface shows its using Date

Just curious why does this.time.nextPrayer() not return a string? seems like the interface shows its using Date

Because a date object is probably the most pertinent and expectable type to return when it comes to prayers times, does it cause you any limitation/trouble?
You can easily format it to a string at your convenience with JS built-in functions, or using more advanced libs like Intl, or even using external libraries like moment or luxon (which might sound overkilled though)

does it cause you any limitation/trouble?

No actually but just curious why something like asr does not return as string

does it cause you any limitation/trouble?

No actually but just curious why something like asr does not return as string

I got you, you're right, the objects returned by timeToNextPrayer are not dates, they are of type Prayer :

nextPrayer(date?: Date): Prayer

The Prayer type is actually an enumeration of strings in Typescript :

const Prayer = {
  Fajr: 'fajr',
  Sunrise: 'sunrise',
  Dhuhr: 'dhuhr',
  Asr: 'asr',
  Maghrib: 'maghrib',
  Isha: 'isha',
  None: 'none'
}

It's a pretty common thing to use this kind of object to identify values in a cleaner way than doing string comparisons. For example, if tomorrow we want to change the string values to capital values (ie. asr becomes ASR),

The following instruction will still work if(prayer === adhan.Prayer.Asr)

But this one will break if(prayer === 'asr')

I advise you to use adhan.Prayer.XXX in your code instead of string values, here is a little example if you need https://codesandbox.io/s/trusting-forest-o7n17?file=/src/index.js

Stale issue message