Skip to content

Commit

Permalink
feat(boa): adds normalize method (#1369)
Browse files Browse the repository at this point in the history
* feat(boa): adds normalize method

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

Closes #13

* cleanup
  • Loading branch information
neeldug authored Jul 1, 2021
1 parent ad972f7 commit 09efb2e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions boa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ indexmap = "1.7.0"
ryu-js = "0.2.1"
chrono = "0.4.19"
fast-float = "0.2.0"
unicode-normalization = "0.1.19"

# Optional Dependencies
measureme = { version = "9.1.2", optional = true }
Expand Down
36 changes: 36 additions & 0 deletions boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::{
cmp::{max, min},
string::String as StdString,
};
use unicode_normalization::UnicodeNormalization;

pub(crate) fn code_point_at(string: RcString, position: i32) -> Option<(u32, u8, bool)> {
let size = string.encode_utf16().count() as i32;
Expand Down Expand Up @@ -119,6 +120,7 @@ impl BuiltIn for String {
.method(Self::index_of, "indexOf", 1)
.method(Self::last_index_of, "lastIndexOf", 1)
.method(Self::r#match, "match", 1)
.method(Self::normalize, "normalize", 1)
.method(Self::pad_end, "padEnd", 1)
.method(Self::pad_start, "padStart", 1)
.method(Self::trim, "trim", 0)
Expand Down Expand Up @@ -1329,6 +1331,40 @@ impl String {
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 )`
///
/// The search() method executes a search for a match between a regular expression and this String object.
Expand Down

0 comments on commit 09efb2e

Please sign in to comment.