UNPKG

3.09 kBJavaScriptView Raw
1const addressUtils = require('../address-utils');
2
3class InvalidFormatError {
4 constructor(message, contactDetail) {
5 this.message = message;
6 this.contactDetail = contactDetail;
7 }
8}
9
10const formatPhoneNumber = function(phone, formatBelgianInternationalNumbers) {
11 let propertyName, newValue;
12 if(typeof phone === 'string' || phone instanceof String) {
13 propertyName = null;
14 newValue = phone;
15 } else {
16 propertyName = phone.value ? 'value' : 'number';
17 newValue = phone[propertyName];
18 }
19 newValue = newValue.replace(/\//g, '').replace(/\./g, '').replace(/\s/g, '').replace(/\(/g, '').replace(/\)/g, '').replace(/\-/g, '').replace(/\'/g, '').replace(/;/g, '');
20 if(!newValue.match(/^\+?0*[1-9]{1}[0-9]{6}[0-9]{0,8}$/)) {
21 throw new InvalidFormatError('Can not format ' + newValue, phone);
22 }
23 if(newValue.substring(0,2) === '00') {
24 newValue = newValue.replace(/^00/, '+');
25 }
26 let isBelgianInternationalNumber = false;
27 if(formatBelgianInternationalNumbers && newValue.substring(0,3) === '+32') {
28 isBelgianInternationalNumber = true;
29 newValue = newValue.replace(/^\+32/, '0');
30 }
31 if(newValue.match(/^0[89]00/)) {
32 newValue = newValue.substring(0,4) + ' ' + newValue.substring(4);
33 } else if(newValue.substring(0,1) !== '+') {
34 if(newValue.length === 10 && newValue.substring(0,2) === '04') {
35 newValue = newValue.substring(0,4) + ' ' + newValue.substring(4,6) + ' ' + newValue.substring(6,8) + ' ' + newValue.substring(8);
36 } else if(newValue.length === 9) {
37 if(newValue.substring(0,2) === '02' || newValue.substring(0,2) === '03') {
38 newValue = newValue.substring(0,2) + ' ' + newValue.substring(2,5) + ' ' + newValue.substring(5,7) + ' ' + newValue.substring(7);
39 } else if( (newValue.substring(0,2) === '04' || newValue.substring(0,2) === '09') && (newValue.substring(2,3) === '2' || newValue.substring(2,3) === '3') ) {
40 newValue = newValue.substring(0,2) + ' ' + newValue.substring(2,5) + ' ' + newValue.substring(5,7) + ' ' + newValue.substring(7);
41 } else if(newValue.substring(0,1) === '0' && newValue.substring(1,2) !== '4' && newValue.substring(1,2) !== '9') {
42 newValue = newValue.substring(0,3) + ' ' + newValue.substring(3,5) + ' ' + newValue.substring(5,7) + ' ' + newValue.substring(7);
43 } else {
44 throw new InvalidFormatError('Can not format ' + newValue, phone);
45 newValue = phone.number.replace(/^0*/,'+');
46 }
47 } else {
48 throw new InvalidFormatError('Can not format ' + newValue, phone);
49 //phone.number = phone.number.replace(/^0*/,'+');
50 }
51 }
52 if(isBelgianInternationalNumber) {
53 newValue = newValue.replace(/^0/, '+32 ');
54 }
55 if(propertyName) {
56 phone[propertyName] = newValue;
57 }
58 return newValue;
59};
60
61module.exports = {
62 InvalidFormatError: InvalidFormatError,
63 formatPhoneNumber: formatPhoneNumber,
64 isSameHouseNumberAndMailbox: addressUtils.isSameHouseNumberAndMailBox,
65 isSameStreet: addressUtils.isSameStreet,
66 addSubCityHref: addressUtils.addSubCityHref,
67 addStreetHref: addressUtils.addStreetHref
68};