1use crate::common::default::default;
2
3pub fn normalize(path: &str) -> String {
4 let path_split: Vec<_> = path.split('/').collect();
5 let mut result_split: Vec<&str> = Vec::new();
6 for &dir in path_split.iter() {
7 match dir {
8 "" => {}
9 ".." => {
10 let last = result_split.last();
11 match last {
12 Some(x) if x != &".." => {
13 result_split.pop();
14 }
15 _ => {
16 result_split.push(dir);
17 }
18 }
19 }
20 _ => {
21 result_split.push(dir);
22 }
23 }
24 }
25 result_split.join("/")
26}
27
28pub fn concat(a: &str, b: &str) -> String {
29 let mut concat = a.to_string();
30 concat.push('/');
31 concat.push_str(b);
32 normalize(&concat)
33}
34
35pub fn split(path: &str) -> (&str, &str) {
36 match path.rsplit_once('/') {
37 None => (default(), path),
38 Some(t) => t,
39 }
40}
41
42#[cfg(test)]
43mod test {
44 use wasm_bindgen_test::wasm_bindgen_test;
45
46 use crate::parser::path::{concat, normalize};
47
48 #[test]
49 #[wasm_bindgen_test]
50 fn test_norm() {
51 let norm = normalize("../../dir/file.json");
52 assert_eq!(norm, "../../dir/file.json");
53
54 let norm = normalize("../../dir/../file.json");
55 assert_eq!(norm, "../../file.json");
56 }
57
58 #[test]
59 #[wasm_bindgen_test]
60 fn test_concat() {
61 let result = concat("a", "b");
62 assert_eq!(result, "a/b");
63
64 let result = concat("a///b/", "c");
65 assert_eq!(result, "a/b/c");
66
67 let result = concat("a/../b/..", "c");
68 assert_eq!(result, "c");
69 }
70}