#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum StdlibClass {
Date,
Map,
Match,
MatchGroups,
MatchFnResult,
RegExp,
Set,
}
pub const MATCH_CLASS_NAME: &str = "__SmeltMatch";
pub const MATCH_GROUPS_CLASS_NAME: &str = "__SmeltMatchGroups";
#[must_use]
pub fn typescript_stdlib_class(name: &str) -> Option<StdlibClass> {
match name {
"Date" => Some(StdlibClass::Date),
"Map" => Some(StdlibClass::Map),
MATCH_CLASS_NAME => Some(StdlibClass::Match),
MATCH_GROUPS_CLASS_NAME => Some(StdlibClass::MatchGroups),
"MatchFnResult" => Some(StdlibClass::MatchFnResult),
"RegExp" => Some(StdlibClass::RegExp),
"Set" => Some(StdlibClass::Set),
_ => None,
}
}
pub const TYPED_ARRAY_CLASS_NAMES: [&str; 11] = [
"Int8Array",
"Uint8Array",
"Uint8ClampedArray",
"Int16Array",
"Uint16Array",
"Int32Array",
"Uint32Array",
"Float32Array",
"Float64Array",
"BigInt64Array",
"BigUint64Array",
];
#[must_use]
pub fn is_typed_array_class_name(name: &str) -> bool {
TYPED_ARRAY_CLASS_NAMES.contains(&name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn recognizes_stdlib_class_names() {
assert_eq!(typescript_stdlib_class("Date"), Some(StdlibClass::Date));
assert_eq!(typescript_stdlib_class("Map"), Some(StdlibClass::Map));
assert_eq!(
typescript_stdlib_class(MATCH_CLASS_NAME),
Some(StdlibClass::Match)
);
assert_eq!(
typescript_stdlib_class(MATCH_GROUPS_CLASS_NAME),
Some(StdlibClass::MatchGroups)
);
assert_eq!(
typescript_stdlib_class("MatchFnResult"),
Some(StdlibClass::MatchFnResult)
);
assert_eq!(typescript_stdlib_class("RegExp"), Some(StdlibClass::RegExp));
assert_eq!(typescript_stdlib_class("Set"), Some(StdlibClass::Set));
}
#[test]
fn rejects_user_class_names() {
for name in ["Regexp", "RegExpLike", "Dates", "HashMap", "MyClass"] {
assert_eq!(typescript_stdlib_class(name), None);
}
}
#[test]
fn recognizes_typed_array_class_names() {
for name in TYPED_ARRAY_CLASS_NAMES {
assert!(
is_typed_array_class_name(name),
"expected `{name}` to be recognized as a typed array"
);
}
assert!(is_typed_array_class_name("BigUint64Array"));
for name in ["Array", "Uint8", "TypedArray", "Float16Array", "MyUint8Array"] {
assert!(
!is_typed_array_class_name(name),
"did not expect `{name}` to be recognized as a typed array"
);
}
}
}