diff options
Diffstat (limited to 'cli/util/path.rs')
-rw-r--r-- | cli/util/path.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/cli/util/path.rs b/cli/util/path.rs index 804b26f65..6f09cf1ea 100644 --- a/cli/util/path.rs +++ b/cli/util/path.rs @@ -42,6 +42,21 @@ pub fn get_extension(file_path: &Path) -> Option<String> { .map(|e| e.to_lowercase()); } +pub fn specifier_has_extension( + specifier: &ModuleSpecifier, + searching_ext: &str, +) -> bool { + let Some((_, ext)) = specifier.path().rsplit_once('.') else { + return false; + }; + let searching_ext = searching_ext.strip_prefix('.').unwrap_or(searching_ext); + debug_assert!(!searching_ext.contains('.')); // exts like .d.ts are not implemented here + if ext.len() != searching_ext.len() { + return false; + } + ext.eq_ignore_ascii_case(searching_ext) +} + pub fn get_atomic_dir_path(file_path: &Path) -> PathBuf { let rand = gen_rand_path_component(); let new_file_name = format!( @@ -378,6 +393,18 @@ mod test { } #[test] + fn test_specifier_has_extension() { + fn get(specifier: &str, ext: &str) -> bool { + specifier_has_extension(&ModuleSpecifier::parse(specifier).unwrap(), ext) + } + + assert!(get("file:///a/b/c.ts", "ts")); + assert!(get("file:///a/b/c.ts", ".ts")); + assert!(!get("file:///a/b/c.ts", ".cts")); + assert!(get("file:///a/b/c.CtS", ".cts")); + } + + #[test] fn test_to_percent_decoded_str() { let str = to_percent_decoded_str("%F0%9F%A6%95"); assert_eq!(str, "🦕"); |