Skip to content

Commit 572f310

Browse files
committed
Class patterns for exports
1 parent b8d8299 commit 572f310

2 files changed

Lines changed: 139 additions & 129 deletions

File tree

src/Julian.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { EPOCH } from "./constants/Time";
22

33
/**
4-
* Julian day from Gregorian date.
4+
* Julian calendar, chronological days since noon Universal Time on January 1, 4713 BC
55
*/
6-
export const fromDate = (date = new Date()): number => {
7-
const time = date.getTime();
8-
return time / 86400000 - date.getTimezoneOffset() / 1440 + EPOCH;
9-
};
6+
export class Julian {
7+
/**
8+
* Julian day from Gregorian date.
9+
*/
10+
static fromDate(date = new Date()): number {
11+
const time = date.getTime();
12+
return time / 86400000 - date.getTimezoneOffset() / 1440 + EPOCH;
13+
}
1014

11-
/**
12-
* Gregorian date from Julian day
13-
*/
14-
export const toDate = (julian: number): Date => {
15-
const date = new Date();
16-
date.setTime((julian - EPOCH + date.getTimezoneOffset() / 1440) * 86400000);
15+
/**
16+
* Gregorian date from Julian day
17+
*/
18+
static toDate(julian: number): Date {
19+
const date = new Date();
20+
date.setTime((julian - EPOCH + date.getTimezoneOffset() / 1440) * 86400000);
1721

18-
return date;
19-
};
22+
return date;
23+
}
24+
}

src/Moon.ts

Lines changed: 121 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Julian from "./Julian";
1+
import { Julian } from "./Julian";
22
import { ANOMALISTIC_MONTH, LUNATION_BASE_JULIAN_DAY, SYNODIC_MONTH } from "./constants/Time";
33
import { Hemisphere } from "./constants/Hemisphere";
44
import { LunarPhase } from "./constants/LunarPhase";
@@ -8,131 +8,136 @@ import { defaultOptions } from "./factory/defaultOptions";
88
import { normalize } from "./utils/MathUtil";
99

1010
/**
11-
* Moon's age, or Earth days since the last new moon,
12-
* normalized within a 29.53059 Earth days calendar.
11+
* Calculations relating to Earth's moon.
1312
*/
14-
export const lunarAge = (date = new Date()) => {
15-
const percent = lunarAgePercent(date);
16-
return percent * SYNODIC_MONTH;
17-
};
18-
19-
/**
20-
* Percentage through the lunar synodic month.
21-
*/
22-
export const lunarAgePercent = (date = new Date()) => {
23-
return normalize((Julian.fromDate(date) - 2451550.1) / SYNODIC_MONTH);
24-
};
25-
26-
/**
27-
* Brown Lunation Number (BLN), per Ernest William Brown's lunar theory,
28-
* defining Lunation 1 as the first new moon of 1923 at
29-
* approximately 02:41 UTC, January 17, 1923.
30-
*/
31-
export const lunationNumber = (date = new Date()) => {
32-
return Math.round((Julian.fromDate(date) - LUNATION_BASE_JULIAN_DAY) / SYNODIC_MONTH) + 1;
33-
};
13+
export class Moon {
14+
/**
15+
* Moon's age, or Earth days since the last new moon,
16+
* normalized within a 29.53059 Earth days calendar.
17+
*/
18+
static lunarAge(date = new Date()) {
19+
const percent = Moon.lunarAgePercent(date);
20+
return percent * SYNODIC_MONTH;
21+
}
3422

35-
/**
36-
* Distance to the moon measured in units of Earth radii, with
37-
* perigee at 56 and apogee at 63.8.
38-
*/
39-
export const lunarDistance = (date = new Date()) => {
40-
const julian = Julian.fromDate(date);
41-
const agePercent = lunarAgePercent(date);
42-
const radians = agePercent * 2 * Math.PI;
43-
const percent = 2 * Math.PI * normalize((julian - 2451562.2) / ANOMALISTIC_MONTH);
23+
/**
24+
* Percentage through the lunar synodic month.
25+
*/
26+
static lunarAgePercent(date = new Date()) {
27+
return normalize((Julian.fromDate(date) - 2451550.1) / SYNODIC_MONTH);
28+
}
4429

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

48-
/**
49-
* Name of the lunar phase per date submitted.
50-
*/
51-
export const lunarPhase = (date = new Date(), options?: Partial<MoonOptions>) => {
52-
options = {
53-
...defaultOptions,
54-
...options,
55-
};
56-
57-
const age = lunarAge(date);
58-
59-
if (age < 1.84566173161) return LunarPhase.NEW;
60-
else if (age < 5.53698519483) return LunarPhase.WAXING_CRESCENT;
61-
else if (age < 9.22830865805) return LunarPhase.FIRST_QUARTER;
62-
else if (age < 12.91963212127) return LunarPhase.WAXING_GIBBOUS;
63-
else if (age < 16.61095558449) return LunarPhase.FULL;
64-
else if (age < 20.30227904771) return LunarPhase.WANING_GIBBOUS;
65-
else if (age < 23.99360251093) return LunarPhase.LAST_QUARTER;
66-
else if (age < 27.68492597415) return LunarPhase.WANING_CRESCENT;
67-
68-
return LunarPhase.NEW;
69-
};
39+
/**
40+
* Distance to the moon measured in units of Earth radii, with
41+
* perigee at 56 and apogee at 63.8.
42+
*/
43+
static lunarDistance(date = new Date()) {
44+
const julian = Julian.fromDate(date);
45+
const agePercent = Moon.lunarAgePercent(date);
46+
const radians = agePercent * 2 * Math.PI;
47+
const percent = 2 * Math.PI * normalize((julian - 2451562.2) / ANOMALISTIC_MONTH);
48+
49+
return 60.4 - 3.3 * Math.cos(percent) - 0.6 * Math.cos(2 * radians - percent) - 0.5 * Math.cos(2 * radians);
50+
}
7051

71-
/**
72-
* Emoji of the lunar phase per date submitted.
73-
*/
74-
export const lunarPhaseEmoji = (date = new Date(), options?: Partial<MoonOptions>) => {
75-
options = {
76-
...defaultOptions,
77-
...options,
78-
};
52+
/**
53+
* Name of the lunar phase per date submitted.
54+
*/
55+
static lunarPhase(date = new Date(), options?: Partial<MoonOptions>) {
56+
options = {
57+
...defaultOptions,
58+
...options,
59+
};
60+
61+
const age = Moon.lunarAge(date);
62+
63+
if (age < 1.84566173161) return LunarPhase.NEW;
64+
else if (age < 5.53698519483) return LunarPhase.WAXING_CRESCENT;
65+
else if (age < 9.22830865805) return LunarPhase.FIRST_QUARTER;
66+
else if (age < 12.91963212127) return LunarPhase.WAXING_GIBBOUS;
67+
else if (age < 16.61095558449) return LunarPhase.FULL;
68+
else if (age < 20.30227904771) return LunarPhase.WANING_GIBBOUS;
69+
else if (age < 23.99360251093) return LunarPhase.LAST_QUARTER;
70+
else if (age < 27.68492597415) return LunarPhase.WANING_CRESCENT;
71+
72+
return LunarPhase.NEW;
73+
}
7974

80-
const phase = lunarPhase(date);
75+
/**
76+
* Emoji of the lunar phase per date submitted.
77+
*/
78+
static lunarPhaseEmoji(date = new Date(), options?: Partial<MoonOptions>) {
79+
options = {
80+
...defaultOptions,
81+
...options,
82+
};
8183

82-
return emojiForLunarPhase(phase, options);
83-
};
84+
const phase = Moon.lunarPhase(date);
8485

85-
/**
86-
* Emoji for specified lunar phase.
87-
*/
88-
export const emojiForLunarPhase = (phase: LunarPhase, options?: Partial<MoonOptions>) => {
89-
const { hemisphere } = {
90-
...defaultOptions,
91-
...options,
92-
};
93-
94-
let emoji;
95-
96-
if (hemisphere === Hemisphere.SOUTHERN) {
97-
emoji = SouthernHemisphereLunarEmoji;
98-
} else {
99-
emoji = NorthernHemisphereLunarEmoji;
86+
return Moon.emojiForLunarPhase(phase, options);
10087
}
10188

102-
switch (phase) {
103-
case LunarPhase.WANING_CRESCENT:
104-
return emoji["WANING_CRESCENT"];
105-
case LunarPhase.LAST_QUARTER:
106-
return emoji["LAST_QUARTER"];
107-
case LunarPhase.WANING_GIBBOUS:
108-
return emoji["WANING_GIBBOUS"];
109-
case LunarPhase.FULL:
110-
return emoji["FULL"];
111-
case LunarPhase.WAXING_GIBBOUS:
112-
return emoji["WAXING_GIBBOUS"];
113-
case LunarPhase.FIRST_QUARTER:
114-
return emoji["FIRST_QUARTER"];
115-
case LunarPhase.WAXING_CRESCENT:
116-
return emoji["WAXING_CRESCENT"];
117-
118-
default:
119-
case LunarPhase.NEW:
120-
return emoji["NEW"];
89+
/**
90+
* Emoji for specified lunar phase.
91+
*/
92+
static emojiForLunarPhase(phase: LunarPhase, options?: Partial<MoonOptions>) {
93+
const { hemisphere } = {
94+
...defaultOptions,
95+
...options,
96+
};
97+
98+
let emoji;
99+
100+
if (hemisphere === Hemisphere.SOUTHERN) {
101+
emoji = SouthernHemisphereLunarEmoji;
102+
} else {
103+
emoji = NorthernHemisphereLunarEmoji;
104+
}
105+
106+
switch (phase) {
107+
case LunarPhase.WANING_CRESCENT:
108+
return emoji["WANING_CRESCENT"];
109+
case LunarPhase.LAST_QUARTER:
110+
return emoji["LAST_QUARTER"];
111+
case LunarPhase.WANING_GIBBOUS:
112+
return emoji["WANING_GIBBOUS"];
113+
case LunarPhase.FULL:
114+
return emoji["FULL"];
115+
case LunarPhase.WAXING_GIBBOUS:
116+
return emoji["WAXING_GIBBOUS"];
117+
case LunarPhase.FIRST_QUARTER:
118+
return emoji["FIRST_QUARTER"];
119+
case LunarPhase.WAXING_CRESCENT:
120+
return emoji["WAXING_CRESCENT"];
121+
122+
default:
123+
case LunarPhase.NEW:
124+
return emoji["NEW"];
125+
}
121126
}
122-
};
123127

124-
/**
125-
* Whether the moon is currently waxing (growing).
126-
*/
127-
export const isWaxing = (date = new Date()) => {
128-
const age = lunarAge(date);
129-
return age <= 14.765;
130-
};
128+
/**
129+
* Whether the moon is currently waxing (growing).
130+
*/
131+
static isWaxing(date = new Date()) {
132+
const age = Moon.lunarAge(date);
133+
return age <= 14.765;
134+
}
131135

132-
/**
133-
* Whether the moon is currently waning (shrinking).
134-
*/
135-
export const isWaning = (date = new Date()) => {
136-
const age = lunarAge(date);
137-
return age > 14.765;
138-
};
136+
/**
137+
* Whether the moon is currently waning (shrinking).
138+
*/
139+
static isWaning(date = new Date()) {
140+
const age = Moon.lunarAge(date);
141+
return age > 14.765;
142+
}
143+
}

0 commit comments

Comments
 (0)