-
Notifications
You must be signed in to change notification settings - Fork 422
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
}()); | ||
}()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JWebCoder Mind to add the newline back where it was 😉 ? |
There was a problem hiding this comment.
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