-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
31 lines (29 loc) · 1.08 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
var db = require('./db.js').db;
function checkCountryCode(number, value){
return ((number[0] === "+" || number[0] === "(") && number.indexOf(value.code) === 1) || number.indexOf(value.code) === 0 || (number[0] === "0" && number[1] === "0") && number.indexOf(value.code) === 2;
}
module.exports = {
numberToInformation: function(number){
return db.reduce(function(obj, value, index){
if(checkCountryCode(number, value)){
var returnObj = Number(obj.code) > Number(value.code) ? obj : value;
returnObj.operator = value.operators.filter(function(data){
if(number.indexOf(data.code) === (number.indexOf(value.code) + value.code.length)){
data.isValid = number.length - (number.indexOf(value.code) + value.code.length) == data.isValid;
return true;
}
return false;
})[0];
delete value.operators;
delete value.example;
return returnObj;
}
return obj;
}, {});
},
countryToInformation: function(name){
return db.filter(function(value){
return name.toLowerCase() === value.name.toLowerCase()
})[0];
}
};