Uecko_ERP_FactuGES_sync/app/utils/telefonos_helper.py
2025-11-05 18:43:40 +01:00

20 lines
575 B
Python

import re
from typing import Optional, Any
def normalizar_telefono_con_plus(texto: Any) -> Optional[str]:
"""
Mantiene '+' si está al principio y el resto sólo dígitos.
- ' (+34) 600-123-456 ' -> '+34600123456'
- '620 61 24 91 Tamara' -> '620612491'
- None o sin dígitos -> None
"""
if texto is None:
return None
s = str(texto).strip()
keep_plus = s.startswith("+")
# Quitar todo lo que NO sea dígito
digits = re.sub(r"\D", "", s)
if not digits:
return None
return ("+" if keep_plus else "") + digits