use serde_json::Value;
pub(crate) fn js_number(value: Option<&Value>) -> f64 {
match value {
None => f64::NAN,
Some(Value::Null) => 0.0,
Some(Value::Bool(value)) => f64::from(u8::from(*value)),
Some(Value::Number(number)) => number.as_f64().unwrap_or(f64::NAN),
Some(Value::String(text)) if text.trim().is_empty() => 0.0,
Some(Value::String(text)) => text.trim().parse().unwrap_or(f64::NAN),
Some(_) => f64::NAN,
}
}
pub(crate) fn js_string(value: Option<&Value>, fallback: &str) -> String {
match value {
None | Some(Value::Null) => fallback.to_string(),
Some(Value::String(text)) => text.clone(),
Some(value) => value.to_string(),
}
}
pub(crate) fn present(value: Option<&Value>) -> Option<&Value> {
value.filter(|value| !value.is_null())
}