Browse Source

feat(boa): adds normalize method (#1369)

* feat(boa): adds normalize method

- adds string.prototype.normalize
- uses `unicode_normalization` crate

Closes #13

* cleanup
pull/1385/head
neeldug 3 years ago committed by GitHub
parent
commit
09efb2e578
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      Cargo.lock
  2. 1
      boa/Cargo.toml
  3. 36
      boa/src/builtins/string/mod.rs

1
Cargo.lock generated

@ -24,6 +24,7 @@ dependencies = [
"ryu-js", "ryu-js",
"serde", "serde",
"serde_json", "serde_json",
"unicode-normalization",
] ]
[[package]] [[package]]

1
boa/Cargo.toml

@ -36,6 +36,7 @@ indexmap = "1.7.0"
ryu-js = "0.2.1" ryu-js = "0.2.1"
chrono = "0.4.19" chrono = "0.4.19"
fast-float = "0.2.0" fast-float = "0.2.0"
unicode-normalization = "0.1.19"
# Optional Dependencies # Optional Dependencies
measureme = { version = "9.1.2", optional = true } measureme = { version = "9.1.2", optional = true }

36
boa/src/builtins/string/mod.rs

@ -30,6 +30,7 @@ use std::{
cmp::{max, min}, cmp::{max, min},
string::String as StdString, string::String as StdString,
}; };
use unicode_normalization::UnicodeNormalization;
pub(crate) fn code_point_at(string: RcString, position: i32) -> Option<(u32, u8, bool)> { pub(crate) fn code_point_at(string: RcString, position: i32) -> Option<(u32, u8, bool)> {
let size = string.encode_utf16().count() as i32; let size = string.encode_utf16().count() as i32;
@ -119,6 +120,7 @@ impl BuiltIn for String {
.method(Self::index_of, "indexOf", 1) .method(Self::index_of, "indexOf", 1)
.method(Self::last_index_of, "lastIndexOf", 1) .method(Self::last_index_of, "lastIndexOf", 1)
.method(Self::r#match, "match", 1) .method(Self::r#match, "match", 1)
.method(Self::normalize, "normalize", 1)
.method(Self::pad_end, "padEnd", 1) .method(Self::pad_end, "padEnd", 1)
.method(Self::pad_start, "padStart", 1) .method(Self::pad_start, "padStart", 1)
.method(Self::trim, "trim", 0) .method(Self::trim, "trim", 0)
@ -1329,6 +1331,40 @@ impl String {
RegExp::match_all(&re, this.to_string(context)?.to_string(), context) RegExp::match_all(&re, this.to_string(context)?.to_string(), context)
} }
/// `String.prototype.normalize( [ form ] )`
///
/// The normalize() method normalizes a string into a form specified in the Unicode® Standard Annex #15
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-string.prototype.normalize
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
pub(crate) fn normalize(this: &Value, args: &[Value], context: &mut Context) -> Result<Value> {
let this = this.require_object_coercible(context)?;
let s = this.to_string(context)?;
let form = args.get(0).cloned().unwrap_or_default();
let f_str;
let f = if form.is_undefined() {
"NFC"
} else {
f_str = form.to_string(context)?;
f_str.as_str()
};
match f {
"NFC" => Ok(Value::from(s.nfc().collect::<StdString>())),
"NFD" => Ok(Value::from(s.nfd().collect::<StdString>())),
"NFKC" => Ok(Value::from(s.nfkc().collect::<StdString>())),
"NFKD" => Ok(Value::from(s.nfkd().collect::<StdString>())),
_ => context
.throw_range_error("The normalization form should be one of NFC, NFD, NFKC, NFKD."),
}
}
/// `String.prototype.search( regexp )` /// `String.prototype.search( regexp )`
/// ///
/// The search() method executes a search for a match between a regular expression and this String object. /// The search() method executes a search for a match between a regular expression and this String object.

Loading…
Cancel
Save