use std::cmp::Ordering;
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Number(f64),
Text(String),
Boolean(bool),
}
impl Value {
pub fn scratch_cmp(&self, other: &Value) -> Ordering {
let (a, b) = (self.compare_number(), other.compare_number());
if a.is_nan() || b.is_nan() {
return self.compare_string().cmp(&other.compare_string());
}
a.partial_cmp(&b).unwrap()
}
pub fn scratch_eq(&self, other: &Self) -> bool {
self.scratch_cmp(other) == Ordering::Equal
}
fn compare_number(&self) -> f64 {
match self {
Value::Number(n) => *n,
Value::Boolean(b) => f64::from(u8::from(*b)),
Value::Text(s) if s.trim().is_empty() => f64::NAN,
Value::Text(s) => parse_js_number(s),
}
}
fn compare_string(&self) -> String {
match self {
Value::Text(s) => s.to_lowercase(),
Value::Boolean(b) => b.to_string(),
Value::Number(n) => js_number_to_string(*n).to_lowercase(),
}
}
}
fn js_number_to_string(n: f64) -> String {
if n == 0.0 { "0".into() } else { n.to_string() }
}
fn parse_js_number(s: &str) -> f64 {
let s = s.trim();
if s.is_empty() {
return 0.0;
}
let radix = match s.get(..2) {
Some("0x" | "0X") => Some(16),
Some("0b" | "0B") => Some(2),
Some("0o" | "0O") => Some(8),
_ => None,
};
if let Some(radix) = radix {
return u64::from_str_radix(&s[2..], radix).map_or(f64::NAN, |n| n as f64);
}
match s {
"Infinity" | "+Infinity" => return f64::INFINITY,
"-Infinity" => return f64::NEG_INFINITY,
_ => {}
}
if !s
.bytes()
.all(|b| matches!(b, b'0'..=b'9' | b'.' | b'e' | b'E' | b'+' | b'-'))
{
return f64::NAN;
}
s.parse().unwrap_or(f64::NAN)
}