From a5b2802662c1a1113a5d39281311ef9dd840c3c4 Mon Sep 17 00:00:00 2001 From: "Michael J. Roberts" <84131395+michealroberts@users.noreply.github.com> Date: Sat, 2 Sep 2023 15:04:16 +0100 Subject: [PATCH] feat: Added getLunarPhase() to moon module in @observerly/astrometry. feat: Added getLunarPhase() to moon module in @observerly/astrometry. Includes associated test suite and expected API output. --- src/moon.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++ tests/moon.spec.ts | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/moon.ts b/src/moon.ts index 868b829..f0abe6c 100644 --- a/src/moon.ts +++ b/src/moon.ts @@ -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 +} + +/*****************************************************************************************************************/ diff --git a/tests/moon.spec.ts b/tests/moon.spec.ts index 87c8b37..82f5971 100644 --- a/tests/moon.spec.ts +++ b/tests/moon.spec.ts @@ -30,7 +30,8 @@ import { getLunarDistance, getLunarAge, getLunarPhaseAngle, - getLunarIllumination + getLunarIllumination, + getLunarPhase } from '../src' /*****************************************************************************************************************/ @@ -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') + }) +}) + +/*****************************************************************************************************************/