-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
75 lines (68 loc) · 1.48 KB
/
index.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const {
MOON_CYCLE,
YEAR_CYCLE,
MONTH_CYCLE,
} = require('./lib/cycles');
const {
FORMAT_ICON,
FORMAT_CODE,
FORMAT_NAME,
FORMATS,
} = require('./lib/formats');
const {
ICONS,
CODES,
} = require('./lib/names');
const word = require('./lib/word');
const NINETEEN_HUNDRED = 694039.09;
const SUFFIX = 'Moon';
function moon(...args) {
const options = typeof args[args.length - 1] === 'object' ? args.pop() : {};
const { format = FORMAT_ICON } = options;
const now = new Date();
let [
year = now.getFullYear(),
month = now.getMonth() + 1,
day = now.getDate(),
] = args;
if (month < 3) {
year--;
month += 12;
}
let total_days_elapsed = year * YEAR_CYCLE + month * MONTH_CYCLE + day - NINETEEN_HUNDRED;
total_days_elapsed /= MOON_CYCLE;
total_days_elapsed -= parseInt(total_days_elapsed); // subtract integer part to leave fractional part of original
let phase = Math.round(total_days_elapsed * ICONS.length);
phase = phase >= ICONS.length ? 0 : phase; // turn 8 into 0
switch (format) {
case FORMAT_CODE:
return CODES[phase];
case FORMAT_NAME:
return [ word(CODES[phase]), SUFFIX ].join(' ');
default:
return ICONS[phase];
}
}
module.exports = Object.defineProperties(
moon,
{
ICONS: {
value: ICONS,
enumerable: true,
},
CODES: {
value: CODES,
enumerable: true,
},
NAMES: {
get: () => CODES.map(
name => [ word(name), SUFFIX ].join(' '),
),
enumerable: true,
},
FORMATS: {
value: FORMATS,
enumerable: true,
},
},
);