Skip to content

Add function stripPrefix #73

Open
@fendor

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 FilePath

Some 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?

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions