mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:47:45 +00:00
LibJS: Trim whitespace from string before coercing to number
This commit is contained in:
parent
f39c7c2978
commit
de42ddfd93
2 changed files with 4 additions and 2 deletions
|
@ -165,8 +165,7 @@ Value Value::to_number() const
|
||||||
case Type::Number:
|
case Type::Number:
|
||||||
return Value(m_value.as_double);
|
return Value(m_value.as_double);
|
||||||
case Type::String: {
|
case Type::String: {
|
||||||
// FIXME: Trim whitespace beforehand
|
auto string = as_string().string().trim_whitespace();
|
||||||
auto& string = as_string().string();
|
|
||||||
if (string.is_empty())
|
if (string.is_empty())
|
||||||
return Value(0);
|
return Value(0);
|
||||||
if (string == "Infinity" || string == "+Infinity")
|
if (string == "Infinity" || string == "+Infinity")
|
||||||
|
|
|
@ -35,6 +35,9 @@ try {
|
||||||
assert(-"Infinity" === -Infinity);
|
assert(-"Infinity" === -Infinity);
|
||||||
assert(-"+Infinity" === -Infinity);
|
assert(-"+Infinity" === -Infinity);
|
||||||
assert(-"-Infinity" === Infinity);
|
assert(-"-Infinity" === Infinity);
|
||||||
|
assert(+" \r \t \n " === 0);
|
||||||
|
assert(+" \n \t Infinity \r " === Infinity);
|
||||||
|
assert(+"\r \n1.23 \t\t\t \n" === 1.23);
|
||||||
|
|
||||||
assert(isNaN(+undefined));
|
assert(isNaN(+undefined));
|
||||||
assert(isNaN(-undefined));
|
assert(isNaN(-undefined));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue