Coverage for src/couchers/phone/check.py: 88%

8 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-20 18:03 +0000

1import phonenumbers 

2 

3 

4def is_e164_format(phone): 

5 """Return true if string is in E.164 format with leading +, for example "+46701740605" """ 

6 return len(phone) > 2 and phone[0] == "+" and phone[1:].isdigit() and len(phone) <= 16 

7 

8 

9def is_known_operator(phone): 

10 """Return true if phone is in E.164 format with leading +, and has the 

11 correct amount of digits considering the country and operator. 

12 """ 

13 

14 if not is_e164_format(phone): 

15 return False 

16 

17 z = phonenumbers.parse(phone) 

18 return phonenumbers.is_valid_number(z)