20 lines
489 B
Python
20 lines
489 B
Python
import re
|
|
from typing import Any
|
|
|
|
|
|
_WHITESPACE_RE = re.compile(r"\s+")
|
|
|
|
|
|
def normalize_text(value: str) -> str:
|
|
return _WHITESPACE_RE.sub(" ", value).strip()
|
|
|
|
|
|
def normalize_payload(value: Any) -> Any:
|
|
if isinstance(value, dict):
|
|
return {k: normalize_payload(v) for k, v in sorted(value.items())}
|
|
if isinstance(value, list):
|
|
return [normalize_payload(item) for item in value]
|
|
if isinstance(value, str):
|
|
return normalize_text(value)
|
|
return value
|