-
Notifications
You must be signed in to change notification settings - Fork 35
Open
Labels
Milestone
Description
The intention is to strip away a common prefix and return the relative FilePath, if and only if there is a common prefix.
Possible function signature:
stripPrefix ::
-- | Root directory. May be relative or absolute.
FilePath ->
-- | FilePath to strip away from the root directory.
FilePath ->
-- | FilePath relative to the root directory or Nothing.
Maybe FilePathSome example calls:
>>> stripPrefix "app" "app/File.hs"
Just "File.hs"
>>> stripPrefix "src" "app/File.hs"
Nothing
>>> stripPrefix "src" "src-dir/File.hs"
Nothing
>>> stripPrefix "." "src/File.hs"
Just "src/File.hs"
>>> stripPrefix "app/" "./app/Lib/File.hs"
Just "Lib/File.hs"
>>> stripPrefix "/app/" "./app/Lib/File.hs"
Nothing -- Nothing since '/app/' is absolute
>>> stripPrefix "/app" "/app/Lib/File.hs"
Just "Lib/File.hs"Motivation: I often want to strip away some directory from another FilePath if and only if the given FilePath is part of the directory. This is some combination of makeRelative and checking, whether the given FilePath is in the given directory.
Other programming languages, such as Rust (https://doc.rust-lang.org/std/path/struct.Path.html#method.strip_prefix) also implement a similar function in the standard library for Path manipulation.
Is such a function useful to others as well?
Or maybe, this function is trivial to express with the given API already?
mb720