-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumberFormat.js
87 lines (79 loc) · 2.86 KB
/
numberFormat.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
const { findCountryByIso, findCountryByDial } = require("./countries");
class PhoneFormat {
/**
* ⚡️ Will send you all the number format with the country data
* @param {string} number accept number too and it's should have the phone number
* @param {string} iso accept string only, it's should have the country code like iq usa etc...
*/
static getAllFormats(number, iso) {
let obj = {};
number = this.convertNumbers2English(number);
obj.isNumber = number.match(/[^0-9]/gi) == null;
number = number.toString().replace(/[^0-9]/gi, "");
number = this.normalize(number);
let country = findCountryByIso(iso);
let cleanNumber = this.normalize(number);
let globalK = country.dial + cleanNumber;
obj.globalZ = "00" + country.dial + cleanNumber;
obj.globalP = "+" + country.dial + cleanNumber;
obj.globalK = globalK;
obj.domestic = "0" + cleanNumber;
obj.domestic2 = cleanNumber;
obj.format1 = this.format(globalK, "(NNN) NNN-NNNN");
obj.format2 = this.format(globalK, "NNN.NNN.NNNN");
obj.country = country;
return obj;
}
/**
* 🌏 Will try to know what country the number is belong to
* @param {string} number accept number too and it's should have the phone number
*/
static knowCountry(number) {
number = this.convertNumbers2English(number);
number = number.toString().replace(/[^0-9]/gi, "");
if (`${number[0]}${number[1]}` == "00") number = number.substr(2);
let str = "";
for (let i = 0; i < 7; i++) {
str = str + number[i];
if (findCountryByDial(str)) return findCountryByDial(str);
}
}
/**
* 🧼 Will send a clean number without the extra keys
* @param {string} number accept number too and it's should have the phone number
*/
static normalize(phoneNumber) {
return phoneNumber.replace(
/^[\+\d{1,3}\-\s]*\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/,
"$1$2$3"
);
}
/**
* 😎 Will give you the cool formats for the number like '(964) 781-****'
* @param {string} number accept number too and it's should have the phone number
*/
static format(phoneNumber, formatString, options) {
// Normalize the phone number first unless not asked to do so in the options
if (!options || !options.normalize) {
phoneNumber = this.normalize(phoneNumber);
}
for (var i = 0, l = phoneNumber.length; i < l; i++) {
formatString = formatString.replace("N", phoneNumber[i]);
}
return formatString;
}
/**
* 👳🏽♀️Arabic numbers to English
* @param {String} string Any string
*/
static convertNumbers2English(string) {
return string
.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function(c) {
return c.charCodeAt(0) - 1632;
})
.replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function(c) {
return c.charCodeAt(0) - 1776;
});
}
}
module.exports = PhoneFormat;