batoulapps / adhan-js

High precision Islamic prayer time library for JavaScript
MIT License
388 stars 87 forks source link

Prayers Time in Array #75

Closed farisfaisalthena closed 3 years ago

farisfaisalthena commented 3 years ago

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) },
]
korbav commented 3 years ago

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.

farisfaisalthena commented 3 years ago

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!

farisfaisalthena commented 3 years ago

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

korbav commented 3 years ago

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)

farisfaisalthena commented 3 years ago

does it cause you any limitation/trouble?

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

korbav commented 3 years ago

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

github-actions[bot] commented 3 years ago

Stale issue message