-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
54 lines (46 loc) · 1.2 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
module.exports = defaultLanguage => {
return {
month: (month = new Date().getMonth() + 1, language = defaultLanguage) => {
return getGems(parseMonth(month), 'month', parseLanguage(language));
},
day: (day = new Date().getDay() + 1, language = defaultLanguage) => {
return getGems(parseDay(day), 'day', parseLanguage(language));
}
}
}
const getGems = (date, namespace, language) => {
let gemsData;
try {
gemsData = require(`./locales/${language}/birthstones_${namespace}.json`);
}
catch {
gemsData = require(`./locales/en/birthstones_${namespace}.json`);
}
// Error code
if (date < 0) {
return date;
}
return Object.values(gemsData)[date];
}
const parseMonth = month => {
// January: 1; December: 12
if (!Number.isInteger(month) || month === null || month < 1 || month > 12) {
return -1;
}
return month - 1;
}
const parseDay = day => {
// Monday: 1; Sunday: 7
if (!Number.isInteger(day) || day < 1 || day > 7) {
return -2;
}
return day - 1;
}
const parseLanguage = language => {
const regex = new RegExp('^[a-z]{2}(-[A-Z]{2})?$'); // xx-YY
// Language format (xx-YY) verification
if (regex.test(language)) {
language = language.substring(0, 2);
}
return language;
}