-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (53 loc) · 1.51 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
const list = require('./list.js');
/** Adding functional for list */
class Locales {
/** Class constructor */
constructor() {
this.list = list;
this.codeList = [];
let i;
for (i = 0; i < this.list.length; i++) {
if (this.codeList.indexOf(this.list[i].locale) === -1) {
this.codeList.push(this.list[i].locale);
}
}
this.codeList = Object.freeze(this.codeList);
}
/**
* Search locale in list by locale code
* @param {String} code Code for search
* @returns {{locale:String, name:String}|Null} Object with locale data or NULL
* @private
*/
_search(code) {
const self = this;
try {
let i;
for (i = 0; i < self.list.length; i++) {
if (self.list[i].locale.toLowerCase() === code.toLowerCase()) {
return self.list[i];
}
}
throw `Not found`;
} catch (error) {
return null;
}
}
/**
* Validate is locale exist
* @param {String} code Locale for validation
* @returns {boolean} True if locale exist or False
*/
validate(code) {
return !!(this._search(code));
}
/**
* Get locale info if exist
* @param {String} code Locale for search
* @returns {{locale:String, name:String}|Null} Object with locale data or NULL
*/
get(code) {
return this._search(code);
}
}
module.exports = new Locales();