Coverage for src / ai_lls_lib / common.py: 100%
14 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-06 23:45 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-06 23:45 +0000
1"""Common utilities for ai-lls-lib."""
3import json
4from decimal import Decimal
7class DecimalEncoder(json.JSONEncoder):
8 """JSON encoder that handles DynamoDB Decimal types."""
10 def default(self, obj: object) -> object:
11 if isinstance(obj, Decimal):
12 return int(obj) if obj % 1 == 0 else float(obj)
13 return super().default(obj)
16def extract_area_code(phone: str) -> str:
17 """Extract 3-digit area code from a phone number string.
19 Handles E.164 format (+1XXXXXXXXXX) and raw digits.
20 Returns 'unknown' if fewer than 3 digits.
21 """
22 digits = "".join(c for c in phone if c.isdigit())
23 if digits.startswith("1") and len(digits) >= 4:
24 return digits[1:4]
25 if len(digits) >= 3:
26 return digits[:3]
27 return "unknown"