Skip to content

Commit

Permalink
Class patterns for exports
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonsturges committed Dec 30, 2022
1 parent b8d8299 commit 572f310
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 129 deletions.
31 changes: 18 additions & 13 deletions src/Julian.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { EPOCH } from "./constants/Time";

/**
* Julian day from Gregorian date.
* Julian calendar, chronological days since noon Universal Time on January 1, 4713 BC
*/
export const fromDate = (date = new Date()): number => {
const time = date.getTime();
return time / 86400000 - date.getTimezoneOffset() / 1440 + EPOCH;
};
export class Julian {
/**
* Julian day from Gregorian date.
*/
static fromDate(date = new Date()): number {
const time = date.getTime();
return time / 86400000 - date.getTimezoneOffset() / 1440 + EPOCH;
}

/**
* Gregorian date from Julian day
*/
export const toDate = (julian: number): Date => {
const date = new Date();
date.setTime((julian - EPOCH + date.getTimezoneOffset() / 1440) * 86400000);
/**
* Gregorian date from Julian day
*/
static toDate(julian: number): Date {
const date = new Date();
date.setTime((julian - EPOCH + date.getTimezoneOffset() / 1440) * 86400000);

return date;
};
return date;
}
}
237 changes: 121 additions & 116 deletions src/Moon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Julian from "./Julian";
import { Julian } from "./Julian";
import { ANOMALISTIC_MONTH, LUNATION_BASE_JULIAN_DAY, SYNODIC_MONTH } from "./constants/Time";
import { Hemisphere } from "./constants/Hemisphere";
import { LunarPhase } from "./constants/LunarPhase";
Expand All @@ -8,131 +8,136 @@ import { defaultOptions } from "./factory/defaultOptions";
import { normalize } from "./utils/MathUtil";

/**
* Moon's age, or Earth days since the last new moon,
* normalized within a 29.53059 Earth days calendar.
* Calculations relating to Earth's moon.
*/
export const lunarAge = (date = new Date()) => {
const percent = lunarAgePercent(date);
return percent * SYNODIC_MONTH;
};

/**
* Percentage through the lunar synodic month.
*/
export const lunarAgePercent = (date = new Date()) => {
return normalize((Julian.fromDate(date) - 2451550.1) / SYNODIC_MONTH);
};

/**
* Brown Lunation Number (BLN), per Ernest William Brown's lunar theory,
* defining Lunation 1 as the first new moon of 1923 at
* approximately 02:41 UTC, January 17, 1923.
*/
export const lunationNumber = (date = new Date()) => {
return Math.round((Julian.fromDate(date) - LUNATION_BASE_JULIAN_DAY) / SYNODIC_MONTH) + 1;
};
export class Moon {
/**
* Moon's age, or Earth days since the last new moon,
* normalized within a 29.53059 Earth days calendar.
*/
static lunarAge(date = new Date()) {
const percent = Moon.lunarAgePercent(date);
return percent * SYNODIC_MONTH;
}

/**
* Distance to the moon measured in units of Earth radii, with
* perigee at 56 and apogee at 63.8.
*/
export const lunarDistance = (date = new Date()) => {
const julian = Julian.fromDate(date);
const agePercent = lunarAgePercent(date);
const radians = agePercent * 2 * Math.PI;
const percent = 2 * Math.PI * normalize((julian - 2451562.2) / ANOMALISTIC_MONTH);
/**
* Percentage through the lunar synodic month.
*/
static lunarAgePercent(date = new Date()) {
return normalize((Julian.fromDate(date) - 2451550.1) / SYNODIC_MONTH);
}

return 60.4 - 3.3 * Math.cos(percent) - 0.6 * Math.cos(2 * radians - percent) - 0.5 * Math.cos(2 * radians);
};
/**
* Brown Lunation Number (BLN), per Ernest William Brown's lunar theory,
* defining Lunation 1 as the first new moon of 1923 at
* approximately 02:41 UTC, January 17, 1923.
*/
static lunationNumber(date = new Date()) {
return Math.round((Julian.fromDate(date) - LUNATION_BASE_JULIAN_DAY) / SYNODIC_MONTH) + 1;
}

/**
* Name of the lunar phase per date submitted.
*/
export const lunarPhase = (date = new Date(), options?: Partial<MoonOptions>) => {
options = {
...defaultOptions,
...options,
};

const age = lunarAge(date);

if (age < 1.84566173161) return LunarPhase.NEW;
else if (age < 5.53698519483) return LunarPhase.WAXING_CRESCENT;
else if (age < 9.22830865805) return LunarPhase.FIRST_QUARTER;
else if (age < 12.91963212127) return LunarPhase.WAXING_GIBBOUS;
else if (age < 16.61095558449) return LunarPhase.FULL;
else if (age < 20.30227904771) return LunarPhase.WANING_GIBBOUS;
else if (age < 23.99360251093) return LunarPhase.LAST_QUARTER;
else if (age < 27.68492597415) return LunarPhase.WANING_CRESCENT;

return LunarPhase.NEW;
};
/**
* Distance to the moon measured in units of Earth radii, with
* perigee at 56 and apogee at 63.8.
*/
static lunarDistance(date = new Date()) {
const julian = Julian.fromDate(date);
const agePercent = Moon.lunarAgePercent(date);
const radians = agePercent * 2 * Math.PI;
const percent = 2 * Math.PI * normalize((julian - 2451562.2) / ANOMALISTIC_MONTH);

return 60.4 - 3.3 * Math.cos(percent) - 0.6 * Math.cos(2 * radians - percent) - 0.5 * Math.cos(2 * radians);
}

/**
* Emoji of the lunar phase per date submitted.
*/
export const lunarPhaseEmoji = (date = new Date(), options?: Partial<MoonOptions>) => {
options = {
...defaultOptions,
...options,
};
/**
* Name of the lunar phase per date submitted.
*/
static lunarPhase(date = new Date(), options?: Partial<MoonOptions>) {
options = {
...defaultOptions,
...options,
};

const age = Moon.lunarAge(date);

if (age < 1.84566173161) return LunarPhase.NEW;
else if (age < 5.53698519483) return LunarPhase.WAXING_CRESCENT;
else if (age < 9.22830865805) return LunarPhase.FIRST_QUARTER;
else if (age < 12.91963212127) return LunarPhase.WAXING_GIBBOUS;
else if (age < 16.61095558449) return LunarPhase.FULL;
else if (age < 20.30227904771) return LunarPhase.WANING_GIBBOUS;
else if (age < 23.99360251093) return LunarPhase.LAST_QUARTER;
else if (age < 27.68492597415) return LunarPhase.WANING_CRESCENT;

return LunarPhase.NEW;
}

const phase = lunarPhase(date);
/**
* Emoji of the lunar phase per date submitted.
*/
static lunarPhaseEmoji(date = new Date(), options?: Partial<MoonOptions>) {
options = {
...defaultOptions,
...options,
};

return emojiForLunarPhase(phase, options);
};
const phase = Moon.lunarPhase(date);

/**
* Emoji for specified lunar phase.
*/
export const emojiForLunarPhase = (phase: LunarPhase, options?: Partial<MoonOptions>) => {
const { hemisphere } = {
...defaultOptions,
...options,
};

let emoji;

if (hemisphere === Hemisphere.SOUTHERN) {
emoji = SouthernHemisphereLunarEmoji;
} else {
emoji = NorthernHemisphereLunarEmoji;
return Moon.emojiForLunarPhase(phase, options);
}

switch (phase) {
case LunarPhase.WANING_CRESCENT:
return emoji["WANING_CRESCENT"];
case LunarPhase.LAST_QUARTER:
return emoji["LAST_QUARTER"];
case LunarPhase.WANING_GIBBOUS:
return emoji["WANING_GIBBOUS"];
case LunarPhase.FULL:
return emoji["FULL"];
case LunarPhase.WAXING_GIBBOUS:
return emoji["WAXING_GIBBOUS"];
case LunarPhase.FIRST_QUARTER:
return emoji["FIRST_QUARTER"];
case LunarPhase.WAXING_CRESCENT:
return emoji["WAXING_CRESCENT"];

default:
case LunarPhase.NEW:
return emoji["NEW"];
/**
* Emoji for specified lunar phase.
*/
static emojiForLunarPhase(phase: LunarPhase, options?: Partial<MoonOptions>) {
const { hemisphere } = {
...defaultOptions,
...options,
};

let emoji;

if (hemisphere === Hemisphere.SOUTHERN) {
emoji = SouthernHemisphereLunarEmoji;
} else {
emoji = NorthernHemisphereLunarEmoji;
}

switch (phase) {
case LunarPhase.WANING_CRESCENT:
return emoji["WANING_CRESCENT"];
case LunarPhase.LAST_QUARTER:
return emoji["LAST_QUARTER"];
case LunarPhase.WANING_GIBBOUS:
return emoji["WANING_GIBBOUS"];
case LunarPhase.FULL:
return emoji["FULL"];
case LunarPhase.WAXING_GIBBOUS:
return emoji["WAXING_GIBBOUS"];
case LunarPhase.FIRST_QUARTER:
return emoji["FIRST_QUARTER"];
case LunarPhase.WAXING_CRESCENT:
return emoji["WAXING_CRESCENT"];

default:
case LunarPhase.NEW:
return emoji["NEW"];
}
}
};

/**
* Whether the moon is currently waxing (growing).
*/
export const isWaxing = (date = new Date()) => {
const age = lunarAge(date);
return age <= 14.765;
};
/**
* Whether the moon is currently waxing (growing).
*/
static isWaxing(date = new Date()) {
const age = Moon.lunarAge(date);
return age <= 14.765;
}

/**
* Whether the moon is currently waning (shrinking).
*/
export const isWaning = (date = new Date()) => {
const age = lunarAge(date);
return age > 14.765;
};
/**
* Whether the moon is currently waning (shrinking).
*/
static isWaning(date = new Date()) {
const age = Moon.lunarAge(date);
return age > 14.765;
}
}

0 comments on commit 572f310

Please sign in to comment.