Browse Source

Fix some cases in value is_true() (#28)

pull/37/head
Damian Senn 5 years ago committed by Jason Williams
parent
commit
3aa4d4091b
  1. 21
      src/lib/js/value.rs

21
src/lib/js/value.rs

@ -142,8 +142,8 @@ impl ValueData {
match *self {
ValueData::Object(_, _) => true,
ValueData::String(ref s) if !s.is_empty() => true,
ValueData::Number(n) if n >= 1.0 && n % 1.0 == 0.0 => true,
ValueData::Integer(n) if n > 1 => true,
ValueData::Number(n) if n != 0.0 && !n.is_nan() => true,
ValueData::Integer(n) if n != 0 => true,
ValueData::Boolean(v) => v,
_ => false,
}
@ -880,4 +880,21 @@ mod tests {
assert_eq!(obj.get_field_slice("foo").to_string(), "bar");
}
#[test]
fn check_integer_is_true() {
assert_eq!(1.to_value().is_true(), true);
assert_eq!(0.to_value().is_true(), false);
assert_eq!((-1).to_value().is_true(), true);
}
#[test]
fn check_number_is_true() {
assert_eq!(1.0.to_value().is_true(), true);
assert_eq!(0.1.to_value().is_true(), true);
assert_eq!(0.0.to_value().is_true(), false);
assert_eq!((-0.0).to_value().is_true(), false);
assert_eq!((-1.0).to_value().is_true(), true);
assert_eq!(NAN.to_value().is_true(), false);
}
}

Loading…
Cancel
Save