Skip to content

Commit

Permalink
Replace maps with pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
NorbertGarfield committed May 16, 2022
1 parent b97b3ee commit cb12567
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 118 deletions.
148 changes: 66 additions & 82 deletions boa_engine/src/builtins/intl/date_time_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,7 @@ pub(crate) fn to_date_time_options(
/// - [Unicode LDML reference][spec]
///
/// [spec]: https://www.unicode.org/reports/tr35/#Unicode_locale_identifier
pub(crate) fn is_terminal(opt: &JsValue, context: &mut Context) -> bool {
let opt_str = opt
.to_string(context)
.unwrap_or_else(|_| JsString::empty())
.to_string();
pub(crate) fn is_terminal(opt_str: &str) -> bool {
if opt_str.is_empty() {
return true;
}
Expand All @@ -298,9 +294,9 @@ pub(crate) fn is_terminal(opt: &JsValue, context: &mut Context) -> bool {
return true;
}

if option
if !option
.chars()
.any(|character| !character.is_ascii_alphanumeric())
.all(|character| character.is_ascii_alphanumeric())
{
return true;
}
Expand All @@ -317,21 +313,21 @@ pub(crate) fn is_terminal(opt: &JsValue, context: &mut Context) -> bool {
/// [spec]: https://tc39.es/ecma402/#sec-intl.datetimeformat-internal-slots
fn build_locale_data(available_locales: &[JsString]) -> LocaleDataRecord {
let mut locale_data_entry = FxHashMap::default();
let nu_values = vec![JsString::new("arab")];
let nu_values = Vec::from([JsString::new("arab")]);
locale_data_entry.insert(JsString::new("nu"), nu_values);

let hc_values = vec![
let hc_values = Vec::from([
JsString::new("h11"),
JsString::new("h12"),
JsString::new("h23"),
JsString::new("h24"),
];
]);
locale_data_entry.insert(JsString::new("hc"), hc_values);

let ca_values = vec![JsString::new("gregory")];
let ca_values = Vec::from([JsString::new("gregory")]);
locale_data_entry.insert(JsString::new("ca"), ca_values);

let hour_cycle_values = vec![JsString::new("h24")];
let hour_cycle_values = Vec::from([JsString::new("h24")]);
locale_data_entry.insert(JsString::new("hourCycle"), hour_cycle_values);

let mut locale_data = FxHashMap::default();
Expand Down Expand Up @@ -423,16 +419,13 @@ pub(crate) fn canonicalize_time_zone_name(time_zone: &JsString) -> JsString {

/// Converts `hour_cycle_str` to `preferences::HourCycle`
pub(crate) fn string_to_hour_cycle(hour_cycle_str: &JsString) -> preferences::HourCycle {
let mut string_to_hc_map = FxHashMap::<JsString, preferences::HourCycle>::default();
string_to_hc_map.insert(JsString::from("h11"), preferences::HourCycle::H11);
string_to_hc_map.insert(JsString::from("h12"), preferences::HourCycle::H12);
string_to_hc_map.insert(JsString::from("h23"), preferences::HourCycle::H23);
string_to_hc_map.insert(JsString::from("h24"), preferences::HourCycle::H24);

let hour_cycle = string_to_hc_map
.get(hour_cycle_str)
.expect("Invalid hour cycle");
*hour_cycle
match hour_cycle_str.as_str() {
"h11" => preferences::HourCycle::H11,
"h12" => preferences::HourCycle::H12,
"h23" => preferences::HourCycle::H23,
"h24" => preferences::HourCycle::H24,
_ => panic!("Invalid hour cycle"),
}
}

/// Converts `JsValue` to `length::Date`
Expand All @@ -446,15 +439,12 @@ pub(crate) fn value_to_date_style(
let date_style_str = date_style_val
.to_string(context)
.unwrap_or_else(|_| JsString::empty());
let mut string_to_style_map = FxHashMap::<JsString, Option<length::Date>>::default();
string_to_style_map.insert(JsString::from("full"), Some(length::Date::Full));
string_to_style_map.insert(JsString::from("long"), Some(length::Date::Long));
string_to_style_map.insert(JsString::from("medium"), Some(length::Date::Medium));
string_to_style_map.insert(JsString::from("short"), Some(length::Date::Short));

match string_to_style_map.get(&date_style_str) {
Some(date_style) => *date_style,
None => None,
match date_style_str.as_str() {
"full" => Some(length::Date::Full),
"long" => Some(length::Date::Long),
"medium" => Some(length::Date::Medium),
"short" => Some(length::Date::Short),
_ => None,
}
}

Expand All @@ -469,15 +459,12 @@ pub(crate) fn value_to_time_style(
let time_style_str = time_style_val
.to_string(context)
.unwrap_or_else(|_| JsString::empty());
let mut string_to_style_map = FxHashMap::<JsString, Option<length::Time>>::default();
string_to_style_map.insert(JsString::from("full"), Some(length::Time::Full));
string_to_style_map.insert(JsString::from("long"), Some(length::Time::Long));
string_to_style_map.insert(JsString::from("medium"), Some(length::Time::Medium));
string_to_style_map.insert(JsString::from("short"), Some(length::Time::Short));

match string_to_style_map.get(&time_style_str) {
Some(time_style) => *time_style,
None => None,
match time_style_str.as_str() {
"full" => Some(length::Time::Full),
"long" => Some(length::Time::Long),
"medium" => Some(length::Time::Medium),
"short" => Some(length::Time::Short),
_ => None,
}
}

Expand Down Expand Up @@ -548,26 +535,17 @@ pub(crate) fn time_zone_to_value(maybe_tz: Option<components::TimeZoneName>) ->

/// Fetches field with name `property` from `format` bag
fn get_format_field(format: &components::Bag, property: &str) -> JsValue {
if property == "weekday" {
text_to_value(format.weekday)
} else if property == "era" {
text_to_value(format.era)
} else if property == "year" {
year_to_value(format.year)
} else if property == "month" {
month_to_value(format.month)
} else if property == "day" {
numeric_to_value(format.day)
} else if property == "hour" {
numeric_to_value(format.hour)
} else if property == "minute" {
numeric_to_value(format.minute)
} else if property == "second" {
numeric_to_value(format.second)
} else if property == "timeZoneName" {
time_zone_to_value(format.time_zone_name)
} else {
JsValue::undefined()
match property {
"weekday" => text_to_value(format.weekday),
"era" => text_to_value(format.era),
"year" => year_to_value(format.year),
"month" => month_to_value(format.month),
"day" => numeric_to_value(format.day),
"hour" => numeric_to_value(format.hour),
"minute" => numeric_to_value(format.minute),
"second" => numeric_to_value(format.second),
"timeZoneName" => time_zone_to_value(format.time_zone_name),
_ => JsValue::undefined(),
}
}

Expand All @@ -591,76 +569,76 @@ fn build_date_time_components() -> Vec<DateTimeComponents> {
vec![
DateTimeComponents {
property: JsString::new("weekday"),
values: vec![
values: Vec::from([
JsString::new("narrow"),
JsString::new("short"),
JsString::new("long"),
],
]),
},
DateTimeComponents {
property: JsString::new("era"),
values: vec![
values: Vec::from([
JsString::new("narrow"),
JsString::new("short"),
JsString::new("long"),
],
]),
},
DateTimeComponents {
property: JsString::new("year"),
values: vec![JsString::new("2-digit"), JsString::new("numeric")],
values: Vec::from([JsString::new("2-digit"), JsString::new("numeric")]),
},
DateTimeComponents {
property: JsString::new("month"),
values: vec![
values: Vec::from([
JsString::new("2-digit"),
JsString::new("numeric"),
JsString::new("narrow"),
JsString::new("short"),
JsString::new("long"),
],
]),
},
DateTimeComponents {
property: JsString::new("day"),
values: vec![JsString::new("2-digit"), JsString::new("numeric")],
values: Vec::from([JsString::new("2-digit"), JsString::new("numeric")]),
},
DateTimeComponents {
property: JsString::new("dayPeriod"),
values: vec![
values: Vec::from([
JsString::new("narrow"),
JsString::new("short"),
JsString::new("long"),
],
]),
},
DateTimeComponents {
property: JsString::new("hour"),
values: vec![JsString::new("2-digit"), JsString::new("numeric")],
values: Vec::from([JsString::new("2-digit"), JsString::new("numeric")]),
},
DateTimeComponents {
property: JsString::new("minute"),
values: vec![JsString::new("2-digit"), JsString::new("numeric")],
values: Vec::from([JsString::new("2-digit"), JsString::new("numeric")]),
},
DateTimeComponents {
property: JsString::new("second"),
values: vec![JsString::new("2-digit"), JsString::new("numeric")],
values: Vec::from([JsString::new("2-digit"), JsString::new("numeric")]),
},
DateTimeComponents {
property: JsString::new("fractionalSecondDigits"),
values: vec![
values: Vec::from([
JsString::new("1.0"),
JsString::new("2.0"),
JsString::new("3.0"),
],
]),
},
DateTimeComponents {
property: JsString::new("timeZoneName"),
values: vec![
values: Vec::from([
JsString::new("short"),
JsString::new("long"),
JsString::new("shortOffset"),
JsString::new("longOffset"),
JsString::new("shortGeneric"),
JsString::new("longGeneric"),
],
]),
},
]
}
Expand Down Expand Up @@ -740,9 +718,9 @@ pub(crate) struct StylesRecord {
/// The `DateTimeStyleFormat` abstract operation accepts arguments `dateStyle` and `timeStyle`,
/// which are each either undefined, "full", "long", "medium", or "short", at least one of which
/// is not undefined, and styles, which is a record from
/// %`DateTimeFormat`%.[[`LocaleData`]].[[<locale>]].[[styles]].[[<calendar>]] for some locale
/// `locale` and calendar `calendar`. It returns the appropriate format record for date time
/// formatting based on the parameters.
/// `%DateTimeFormat%.[[LocaleData]].[[<locale>]].[[styles]].[[<calendar>]]` for some locale and
/// calendar. It returns the appropriate format record for date time formatting based on the
/// parameters.
///
/// More information:
/// - [ECMAScript reference][spec]
Expand Down Expand Up @@ -1098,7 +1076,10 @@ fn initialize_date_time_format(
if !calendar.is_undefined() {
// a. If calendar does not match the Unicode Locale Identifier type nonterminal,
// throw a RangeError exception.
if is_terminal(&calendar, context) {
let calendar_str = calendar
.to_string(context)
.unwrap_or_else(|_| JsString::empty());
if is_terminal(&calendar_str) {
return context.throw_range_error("calendar must be nonterminal");
}
}
Expand All @@ -1120,7 +1101,10 @@ fn initialize_date_time_format(
if !numbering_system.is_undefined() {
// a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal,
// throw a RangeError exception.
if is_terminal(&numbering_system, context) {
let numbering_system_str = numbering_system
.to_string(context)
.unwrap_or_else(|_| JsString::empty());
if is_terminal(&numbering_system_str) {
return context.throw_range_error("numberingSystem must be nonterminal");
}
}
Expand Down
70 changes: 34 additions & 36 deletions boa_engine/src/builtins/intl/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,56 +524,54 @@ fn to_date_time_opts() {

#[test]
fn nonterminals() {
let mut context = Context::default();

let nonterminal_calendar_options = vec![
JsValue::String(JsString::new("")),
JsValue::String(JsString::new("a")),
JsValue::String(JsString::new("ab")),
JsValue::String(JsString::new("abcdefghi")),
JsValue::String(JsString::new("abc-abcdefghi")),
JsValue::String(JsString::new("!invalid!")),
JsValue::String(JsString::new("-gregory-")),
JsValue::String(JsString::new("gregory-")),
JsValue::String(JsString::new("gregory--")),
JsValue::String(JsString::new("gregory-nu")),
JsValue::String(JsString::new("gregory-nu-")),
JsValue::String(JsString::new("gregory-nu-latn")),
JsValue::String(JsString::new("gregoryé")),
JsValue::String(JsString::new("gregory역법")),
JsString::new(""),
JsString::new("a"),
JsString::new("ab"),
JsString::new("abcdefghi"),
JsString::new("abc-abcdefghi"),
JsString::new("!invalid!"),
JsString::new("-gregory-"),
JsString::new("gregory-"),
JsString::new("gregory--"),
JsString::new("gregory-nu"),
JsString::new("gregory-nu-"),
JsString::new("gregory-nu-latn"),
JsString::new("gregoryé"),
JsString::new("gregory역법"),
];

for calendar_opt in nonterminal_calendar_options {
assert_eq!(
crate::builtins::intl::date_time_format::is_terminal(&calendar_opt, &mut context),
crate::builtins::intl::date_time_format::is_terminal(&calendar_opt),
true
);
}

let terminal_calendar_options = vec![
JsValue::String(JsString::new("buddhist")),
JsValue::String(JsString::new("chinese")),
JsValue::String(JsString::new("coptic")),
JsValue::String(JsString::new("dangi")),
JsValue::String(JsString::new("ethioaa")),
JsValue::String(JsString::new("ethiopic")),
JsValue::String(JsString::new("gregory")),
JsValue::String(JsString::new("hebrew")),
JsValue::String(JsString::new("indian")),
JsValue::String(JsString::new("islamic")),
JsValue::String(JsString::new("islamic-umalqura")),
JsValue::String(JsString::new("islamic-tbla")),
JsValue::String(JsString::new("islamic-civil")),
JsValue::String(JsString::new("islamic-rgsa")),
JsValue::String(JsString::new("iso8601")),
JsValue::String(JsString::new("japanese")),
JsValue::String(JsString::new("persian")),
JsValue::String(JsString::new("roc")),
JsString::new("buddhist"),
JsString::new("chinese"),
JsString::new("coptic"),
JsString::new("dangi"),
JsString::new("ethioaa"),
JsString::new("ethiopic"),
JsString::new("gregory"),
JsString::new("hebrew"),
JsString::new("indian"),
JsString::new("islamic"),
JsString::new("islamic-umalqura"),
JsString::new("islamic-tbla"),
JsString::new("islamic-civil"),
JsString::new("islamic-rgsa"),
JsString::new("iso8601"),
JsString::new("japanese"),
JsString::new("persian"),
JsString::new("roc"),
];

for calendar_opt in terminal_calendar_options {
assert_eq!(
crate::builtins::intl::date_time_format::is_terminal(&calendar_opt, &mut context),
crate::builtins::intl::date_time_format::is_terminal(&calendar_opt),
false
);
}
Expand Down

0 comments on commit cb12567

Please sign in to comment.