-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
165 lines (131 loc) · 4.06 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const regex = new RegExp('^[a-z]{2}(-[A-Z]{2})?$'); // xx-YY
module.exports = defaultLanguage => {
// Language format verification
if (regex.test(defaultLanguage)) { // Language format (xx-YY) verification
defaultLanguage = defaultLanguage.substring(0, 2);
} else {
defaultLanguage = 'en';
}
return {
getSignByDate: ({ day, month } = { day: new Date().getDate(), month: new Date().getMonth() + 1 }, language = defaultLanguage) => {
return getSignByDate({ day, month }, language);
},
getSignByName: (signName, language = defaultLanguage) => {
return getSignByName(signName, parseLanguage(language));
},
getSignBySymbol: (signSymbol, language = defaultLanguage) => {
return getSignBySymbol(signSymbol, parseLanguage(language));
},
getSymbols: () => {
return getSymbols();
},
getNames: (language = defaultLanguage) => {
return getNames(parseLanguage(language));
},
getElements: (language = defaultLanguage) => {
return getElements(parseLanguage(language));
}
}
};
const getSignByDate = ({ day, month }, language) => {
const date = new Date(`2000-${month}-${day}`);
if (date.toString() === 'Invalid Date') {
return -1;
}
const signsData = Object.values(require('./data/zodiac.json'));
let signsLocale;
try {
signsLocale = Object.values(require(`./locales/${language}/zodiac.json`));
} catch {
signsLocale = Object.values(require('./locales/en/zodiac.json'));
}
let dateMin;
let dateMax;
const i = signsData.findIndex(sign => {
dateMin = new Date(sign.dateMin);
dateMax = new Date(sign.dateMax);
return date.getDate() >= dateMin.getDate() && date.getMonth() == dateMin.getMonth() || date.getDate() <= dateMax.getDate() && date.getMonth() === dateMax.getMonth();
});
let sign = Object.assign(signsLocale[i], signsData[i]);
sign = getElement(sign, language);
return sign;
};
const getSignByName = (signName, language) => {
if (signName === null || !(typeof signName === 'string')) {
return -2;
}
const index = getNames(language).indexOf(signName.charAt(0).toUpperCase() + signName.slice(1));
return getSignByIndex(index, language);
};
const getSignBySymbol = (signSymbol, language) => {
if (signSymbol === null || !(typeof signSymbol === 'string')) {
return -2;
}
const index = getSymbols().indexOf(signSymbol);
return getSignByIndex(index, language);
};
const getSymbols = () => {
const signsData = require('./data/zodiac.json');
return getListValue('symbol', signsData);
};
const getNames = (language) => {
let signsLocale;
try {
signsLocale = Object.values(require(`./locales/${language}/zodiac.json`));
} catch {
signsLocale = Object.values(require('./locales/en/zodiac.json'));
}
return getListValue('name', signsLocale);
};
const getElements = (language) => {
let elementsData;
try {
elementsData = Object.values(require(`./locales/${language}/elements.json`));
} catch {
elementsData = Object.values(require('./locales/en/elements.json'));
}
return elementsData;
};
const getSignByIndex = (index, language) => {
if (index === -1) {
return -2;
}
const signsData = Object.values(require('./data/zodiac.json'));
let signsLocale;
try {
signsLocale = Object.values(require(`./locales/${language}/zodiac.json`));
} catch {
signsLocale = Object.values(require('./locales/en/zodiac.json'));
}
let sign = Object.assign(signsLocale[index], signsData[index]);
sign = getElement(sign, language);
return sign;
};
const getListValue = (key, data) => {
data = Object.values(data);
const values = [];
data.forEach(value => {
values.push(value[key]);
});
return values;
};
const getElement = (sign, language) => {
let elementsData;
try {
elementsData = require(`./locales/${language}/elements.json`);
} catch {
elementsData = require('./locales/en/elements.json');
}
sign.element = elementsData[sign.element];
return sign;
};
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;
}
const parseDate = (language) => {
};