1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 const entries = require('./mooninfo_2023.json');
29 const FULLNESS_THRESHOLD = 80.00;
30
31 function Moon(){
32 let entry = this.GetInfo(Date.now());
33 this.is_full = entry.fullness > FULLNESS_THRESHOLD;
34 }
35
36
37 Moon.prototype.CheckPhase = function(date, onFullChange) {
38 let entry = this.GetInfo(date);
39 this.SetFullness(entry.fullness, onFullChange);
40 }
41
42
43 Moon.prototype.GetInfo = function(requestedDate) {
44 let requestedEntry;
45
46 entries.forEach(function(entry) {
47 let timestamp = Date.parse(entry.time);
48
49 if (timestamp > requestedDate && !requestedEntry) {
50 requestedEntry = {
51 date: timestamp,
52 age: parseFloat(entry.age),
53 fullness: parseFloat(entry.phase)
54 };
55 }
56 });
57
58 return requestedEntry;
59 }
60
61
62 Moon.prototype.SetFullness = function(fullness, onFullChange) {
63 if ((fullness >= FULLNESS_THRESHOLD) && !this.is_full) {
64 this.is_full = true;
65 onFullChange(true);
66 } else if ((fullness < FULLNESS_THRESHOLD) && this.is_full) {
67 this.is_full = false;
68 onFullChange(false);
69 }
70 }
71
72 module.exports = Moon;