-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.js
38 lines (34 loc) · 1.26 KB
/
parse.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
const PNF = require('google-libphonenumber').PhoneNumberFormat;
const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
const COUNTRY_CODE = "CA";
module.exports.parse = function (phoneNumber) {
return new Promise((resolve, reject) => {
const number = phoneUtil.parseAndKeepRawInput(phoneNumber, COUNTRY_CODE);
if (isValid(number)){
const formattedNumber = phoneUtil.format(number, PNF.NATIONAL);
resolve(formattedNumber);
} else {
reject("Phone is invalid")
}
})
}
module.exports.parseFile = function (data) {
let validPhones = [];
return new Promise((resolve, reject) => {
for(i = 0; i < data.length; i++){
console.log(data[i]);
const number = phoneUtil.parseAndKeepRawInput(data[i], COUNTRY_CODE);
if (isValid(number)) {
validPhones.push(phoneUtil.format(number, PNF.NATIONAL));
}
}
if(validPhones){
resolve(validPhones);
}else{
reject("No Valid Phones found");
}
})
}
function isValid(number){
return phoneUtil.isPossibleNumber(number) && phoneUtil.isValidNumber(number) && phoneUtil.isValidNumberForRegion(number, COUNTRY_CODE)
}