Skip to content

Commit

Permalink
Merge pull request #101 from observerly/feature/moon/getLunarPhase
Browse files Browse the repository at this point in the history
  • Loading branch information
michealroberts authored Sep 2, 2023
2 parents fb46185 + a5b2802 commit a727bde
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/moon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,51 @@ export const getLunarIllumination = (datetime: Date): number => {
}

/*****************************************************************************************************************/

/**
*
* getLunarPhase()
*
* The phase of the Moon is the shape of the Moon's illuminated portion
* as seen from the Earth.
*
* @param date - The date to calculate the Moon's phase for.
* @returns The phase of the Moon.
*
*/
export const getLunarPhase = (datetime: Date): Phase => {
// Get the age of the Moon:
const { age } = getLunarAge(datetime)

if (age >= 0 && age < 3.7) {
return Phases.WaningCrescent
}

if (age >= 3.7 && age < 7.4) {
return Phases.WaxingCrescent
}

if (age >= 7.4 && age < 11.1) {
return Phases.FirstQuarter
}

if (age >= 11.1 && age < 14.6) {
return Phases.WaxingGibbous
}

if (age >= 14.6 && age < 15.0) {
return Phases.Full
}

if (age >= 15.0 && age < 22.1) {
return Phases.WaningGibbous
}

if (age >= 22.1 && age < 25.8) {
return Phases.LastQuarter
}

return Phases.New
}

/*****************************************************************************************************************/
35 changes: 34 additions & 1 deletion tests/moon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
getLunarDistance,
getLunarAge,
getLunarPhaseAngle,
getLunarIllumination
getLunarIllumination,
getLunarPhase
} from '../src'

/*****************************************************************************************************************/
Expand Down Expand Up @@ -310,3 +311,35 @@ describe('getLunarIllumination', () => {
})

/*****************************************************************************************************************/

describe('getLunarPhase', () => {
it('should be defined', () => {
expect(getLunarPhase).toBeDefined()
})

it('should return the correct Lunar phase for the given date as Waxing Gibbous', () => {
const datetime = new Date('2015-01-02T02:00:00.000+00:00')
const phase = getLunarPhase(datetime)
expect(phase).toBe('Waxing Gibbous')
})

it('should return the correct Lunar phase for the given date as Full Moon', () => {
const datetime = new Date('2015-01-05T07:00:00.000+00:00')
const phase = getLunarPhase(datetime)
expect(phase).toBe('Full')
})

it('should return the correct Lunar phase for the given date as Waning Gibbous', () => {
const datetime = new Date('2015-01-07T23:00:00.000+00:00')
const phase = getLunarPhase(datetime)
expect(phase).toBe('Waning Gibbous')
})

it('should return the correct Lunar phase for the given date as Last Quarter', () => {
const datetime = new Date('2015-02-12T17:00:00.000+00:00')
const phase = getLunarPhase(datetime)
expect(phase).toBe('Last Quarter')
})
})

/*****************************************************************************************************************/

0 comments on commit a727bde

Please sign in to comment.