Skip to content

Commit

Permalink
feat: add is_relative_specifier (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Feb 21, 2025
1 parent 603f84f commit f667439
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,26 @@ fn gen_rand_path_component(sys: &impl SystemRandom) -> String {
})
}

pub fn is_relative_specifier(specifier: &str) -> bool {
let mut specifier_chars = specifier.chars();
let Some(first_char) = specifier_chars.next() else {
return false;
};
if first_char != '.' {
return false;
}
let Some(second_char) = specifier_chars.next() else {
return true;
};
if second_char == '/' {
return true;
}
let Some(third_char) = specifier_chars.next() else {
return second_char == '.';
};
second_char == '.' && third_char == '/'
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -721,4 +741,20 @@ mod tests {
assert_eq!(url, expected_url);
}
}

#[test]
fn test_is_relative_specifier() {
assert!(is_relative_specifier("."));
assert!(is_relative_specifier(".."));
assert!(is_relative_specifier("../"));
assert!(is_relative_specifier("../test"));
assert!(is_relative_specifier("./"));
assert!(is_relative_specifier("./test"));
assert!(!is_relative_specifier(""));
assert!(!is_relative_specifier("a"));
assert!(!is_relative_specifier(".test"));
assert!(!is_relative_specifier("..test"));
assert!(!is_relative_specifier("/test"));
assert!(!is_relative_specifier("test"));
}
}

0 comments on commit f667439

Please sign in to comment.