pub typealias String as Fpath///|
test {
let windows_path : @fpath.Fpath = "C:\\Users\\name\\file.txt"
let unix_path = @fpath.ensure_unix(windows_path)
@json.inspect(unix_path, content="C:/Users/name/file.txt")
}let dir = "src/main"
let dir_path = fpath_ensure_directoryness(dir)
// Result: "src/main/"let messy_path = "src//./main///utils/./file.txt"
let clean_path = fpath_sanitize(messy_path)
// Result: "src/main/utils/file.txt"// Windows input
let user_path = "Documents\\Projects\\src\\main.mbt"
// Normalize for ZIP
let zip_path = fpath_ensure_unix(user_path)
let clean_path = fpath_sanitize(zip_path)
// Result: "Documents/Projects/src/main.mbt"// Create directory entry
let dir_name = "src/utils"
let dir_path = fpath_ensure_directoryness(
fpath_ensure_unix(
fpath_sanitize(dir_name)
)
)
// Result: "src/utils/"fn normalize_for_zip(path : String) -> Fpath {
path
|> fpath_sanitize
|> fpath_ensure_unix
}
fn normalize_directory(path : String) -> Fpath {
path
|> normalize_for_zip
|> fpath_ensure_directoryness
}
let file_path = normalize_for_zip("dir\\./subdir//file.txt")
// Result: "dir/subdir/file.txt"
let dir_path = normalize_directory("my\\project\\src")
// Result: "my/project/src/"Input: "dir\\subdir\\file.txt"
Step 1: Replace '\' with '/'
Output: "dir/subdir/file.txt"Input: "dir///subdir//file.txt"
Step 1: Replace multiple '/' with single '/'
Output: "dir/subdir/file.txt"Input: "dir/./subdir/./file.txt"
Step 1: Remove '/.' components
Output: "dir/subdir/file.txt"Input: "dir/subdir"
Step 1: Check if ends with '/'
Step 2: If not, append '/'
Output: "dir/subdir/"fn make_file_path(raw_path : String) -> Fpath {
raw_path
|> fpath_sanitize // Clean up
|> fpath_ensure_unix // Unix separators
}fn make_dir_path(raw_path : String) -> Fpath {
raw_path
|> fpath_sanitize // Clean up
|> fpath_ensure_unix // Unix separators
|> fpath_ensure_directoryness // Add trailing slash
}fn normalize_zip_path(raw_path : String, is_dir : Bool) -> Fpath {
let path = raw_path
|> fpath_sanitize
|> fpath_ensure_unix
if is_dir {
fpath_ensure_directoryness(path)
} else {
path
}
}moon test types/fpathInstall
Download zip