-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (87 loc) · 2.11 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
const data = require('./data/data.json');
var langs = {
alpha1: russianNameByCode1,
alpha2: russianNameByCode2,
alpha3: russianNameByCode3,
gost: russianNameByGostCode,
numeric: russianNameByNumericCode,
all: getAllLanguages,
names: getAllLanguageNames,
codes: getAllCodes,
ruNames: getRuNames,
originalNames: getOriginalNames
}
module.exports = langs;
function russianNameByCode3(code) {
return data.find((lang) => lang["3"] === code.toLowerCase())
}
function russianNameByCode2(code) {
return data.find((lang) => lang["2"] === code.toLowerCase())
}
function russianNameByCode1(code) {
return data.find((lang) => lang["1"] === code.toLowerCase())
}
function russianNameByGostCode(code) {
return data.find((lang) => lang.gost77597 === code.toLowerCase())
}
function russianNameByNumericCode(code) {
code = Number(code);
return data.find((lang) => lang.numeric === code)
}
function getAllLanguages() {
return data
}
function getAllCodes(byCode) {
byCode = String(byCode);
return data.map((lang) => lang[byCode]);
}
function getAllLanguageNames() {
return data.map(lang => lang.name)
}
function getRuNames(obj) {
if(Object.keys(obj).length === 0) {
return ""
}
if(typeof obj === 'string' && obj === "") {
return ""
}
// todo: refactor
if(typeof obj === 'string') {
let langObject = data.find((lang) => {
let toFind = obj.toUpperCase();
let currLang = lang["3"];
let upCurrLang = currLang.toUpperCase();
let finded = upCurrLang === toFind
return finded
});
return langObject.name
}
let codes = Object.keys(obj);
let names = [];
codes.map(code => {
if(obj[code] === 'Moldavian') {
names.push('Молдавский')
}
let findedCode = data.find((lang) => {
return lang["3"] === code
});
names.push(findedCode.name)
})
let joined = names.join(", ");
return joined
}
function getOriginalNames(obj) {
if(typeof obj === 'string' && obj === "") {
return ""
}
if(Object.keys(obj).length === 0) {
return ""
}
let codes = Object.keys(obj);
let names = [];
codes.map(code => {
names.push(obj[code])
})
let joined = names.join(", ");
return joined
}