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

added moon transits calculations #66

Open
wants to merge 2 commits into
base: master
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
70 changes: 69 additions & 1 deletion suncalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,79 @@ SunCalc.getMoonTimes = function (date, lat, lng, inUTC) {

return result;
};

function calcMoonTransit(rize, set) {
if (rize > set) {
return new Date(set.getTime() + (rize - set) / 2);
} else {
return new Date(rize.getTime() + (set - rize) / 2);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be a one-liner with Math.abs

}

function mainMoonTransit(rise, set, lat, lng) {
var main = "",
day = set.getDate(),
tempTransitBefore,
tempTransitAfter;

if ((rise && set) && (rise < set)) {
main = calcMoonTransit(rise, set);
} else {
if (rise) {
tempTransitAfter = calcMoonTransit(rise, SunCalc.getMoonTimes(new Date(rise).setDate(day + 1), lat, lng).set);
if (tempTransitAfter.getDate() === day) {
main = tempTransitAfter;
}
}

if (set) {
tempTransitBefore = calcMoonTransit(set, SunCalc.getMoonTimes(new Date(set).setDate(day - 1), lat, lng).rise);
if (tempTransitBefore.getDate() === day) {
main = tempTransitBefore;
}
}
}
return main;
}

function invertMoonTransit(rise, set, lat, lng) {
var invert = "",
day = set.getDate(),
tempTransitBefore,
tempTransitAfter;

if ((rise && set) && (set < rise)) {
invert = calcMoonTransit(rise, set);
} else {
if (rise) {
tempTransitBefore = calcMoonTransit(rise, SunCalc.getMoonTimes(new Date(rise).setDate(day - 1), lat, lng).set);
if (tempTransitBefore.getDate() === day) {
invert = tempTransitBefore;
}
}

if (set) {
tempTransitAfter = calcMoonTransit(set, SunCalc.getMoonTimes(new Date(set).setDate(day + 1), lat, lng).rise);
if (tempTransitAfter.getDate() === day) {
invert = tempTransitAfter;
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like all this code could be DRY-ed up to be much shorter. There are minimal differences between normal and invert functions.


return invert;
}

SunCalc.moonTransit = function (rise, set, lat, lng) {
var transit = {};

transit.main = mainMoonTransit(rise, set, lat, lng);
transit.invert = invertMoonTransit(rise, set, lat, lng);
return transit;
};

// export as AMD module / Node module / browser variable
if (typeof define === 'function' && define.amd) define(SunCalc);
else if (typeof module !== 'undefined') module.exports = SunCalc;
else window.SunCalc = SunCalc;

}());
}());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JWebCoder Mind to add the newline back where it was 😉 ?