haskell / haskell/filepath

Add function `stripPrefix`

Open
#73 11 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Haskell
Stars
74
Forks
38
PR merge metrics
No merged PRs in 30d

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:
```haskell
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:
```haskell
>>> 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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.