Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecmascript): add StringToNumber #6576

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/oxc_ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod string_char_at;
mod string_index_of;
mod string_last_index_of;
mod string_to_big_int;
mod string_to_number;
mod to_big_int;
mod to_boolean;
mod to_int_32;
Expand All @@ -28,6 +29,6 @@ pub use self::{
private_bound_identifiers::PrivateBoundIdentifiers, prop_name::PropName,
string_char_at::StringCharAt, string_index_of::StringIndexOf,
string_last_index_of::StringLastIndexOf, string_to_big_int::StringToBigInt,
to_big_int::ToBigInt, to_boolean::ToBoolean, to_int_32::ToInt32, to_number::ToNumber,
to_string::ToJsString,
string_to_number::StringToNumber, to_big_int::ToBigInt, to_boolean::ToBoolean,
to_int_32::ToInt32, to_number::ToNumber, to_string::ToJsString,
};
9 changes: 9 additions & 0 deletions crates/oxc_ecmascript/src/string_to_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub trait StringToNumber {
fn string_to_number(&self) -> f64;
}

impl StringToNumber for &str {
fn string_to_number(&self) -> f64 {
self.parse::<f64>().unwrap_or(f64::NAN)
}
}
6 changes: 3 additions & 3 deletions crates/oxc_ecmascript/src/to_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ impl<'a> ToNumber<'a> for Expression<'a> {
"NaN" | "undefined" => Some(f64::NAN),
_ => None,
},
// TODO: StringToNumber
Expression::StringLiteral(string_literal) => {
string_literal.value.parse::<f64>().map_or(Some(f64::NAN), Some)
Expression::StringLiteral(lit) => {
use crate::StringToNumber;
Some(lit.value.as_str().string_to_number())
}
_ => None,
}
Expand Down
Loading