23 lines
612 B
Python
23 lines
612 B
Python
"""Helpers for query-string coercion."""
|
|
|
|
from __future__ import annotations
|
|
|
|
_TRUE = {"1", "true", "yes", "y", "on"}
|
|
_FALSE = {"0", "false", "no", "n", "off"}
|
|
|
|
|
|
def tri_state(value: str | None) -> bool | None:
|
|
"""Read a three-state boolean filter: ``None`` means "do not filter".
|
|
|
|
Accepts ``1/0`` and the usual ``true/false`` spellings so a caller sending
|
|
``true`` never silently gets an unfiltered result set.
|
|
"""
|
|
text = (value or "").strip().lower()
|
|
if not text:
|
|
return None
|
|
if text in _TRUE:
|
|
return True
|
|
if text in _FALSE:
|
|
return False
|
|
return None
|