Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add next and previous lunar phase methods #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,44 @@ export const isWaning = (date = new Date()) => {
const age = lunarAge(date);
return age > 14.765;
};

/**
* Whether the moon is equal to the given phase.
*
* @param {LunarPhase} phase Lunar phase, per the LunarPhase enum
* @param {Date} date Date used for calculation.
* @returns {boolean} True if moon is in the phase on the given date.
*/
export const isLunarPhase = (phase, date = new Date()) => {
return phase === lunarPhase(date);
};

/**
* The date of the next lunar phase.
*
* @param {LunarPhase} phase Lunar phase, per the LunarPhase enum
* @param {Date} date Starting date used for calculation.
* @returns {Date} Date of next lunar phase
*/

export const nextLunarPhase = (phase, date = new Date()) => {
while (!isLunarPhase(phase, date)) {
date.setDate(date.getDate() + 1);
}
return date;
};

/**
* The date of the previous lunar phase.
*
* @param {LunarPhase} phase Lunar phase, per the LunarPhase enum
* @param {Date} date Starting date used for calculation.
* @returns {Date} Date of previous lunar phase
*/

export const previousLunarPhase = (phase, date = new Date()) => {
while (!isLunarPhase(phase, date)) {
date.setDate(date.getDate() - 1);
}
return date;
};