mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(ecmascript): add StringToNumber (#6576)
This commit is contained in:
parent
a71e8a00bf
commit
d11770d552
3 changed files with 15 additions and 5 deletions
|
|
@ -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;
|
||||
|
|
@ -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
crates/oxc_ecmascript/src/string_to_number.rs
Normal file
9
crates/oxc_ecmascript/src/string_to_number.rs
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue