Skip to content

Commit

Permalink
feat(ecmascript): add StringToNumber (#6576)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 15, 2024
1 parent a71e8a0 commit d11770d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
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

0 comments on commit d11770d

Please sign in to comment.