Skip to content

Replace slashes in a name with $slash instead of - #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features:
Bugfixes:

Other improvements:
- Changed `slashEscaper`: replace slashes in a name with `$slash` instead of `-` (#48 by @rintcius)

## [v8.1.0](https://github.com/purescript-contrib/purescript-pathy/releases/tag/v8.1.0) - 2021-05-06

Expand Down
10 changes: 5 additions & 5 deletions src/Pathy/Printer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ instance semigroupEscaper :: Semigroup Escaper where
instance monoidEscaper :: Monoid Escaper where
mempty = Escaper identity

-- | An escaper that replaces all `'/'` characters in a name with `'-'`s.
-- | An escaper that replaces all `'/'` characters in a name with `$slash`.
slashEscaper :: Escaper
slashEscaper = Escaper (NES.replaceAll slash dash)
slashEscaper = Escaper (NES.replaceAll slash escaped)
where
slash = Str.Pattern "/"
dash = NES.NonEmptyReplacement (NES.singleton '-')
escaped = NES.NonEmptyReplacement $ unsafePartial NES.unsafeFromString "$slash"

-- | An escaper that replaces names `"."` and `".."` with `"$dot"` and
-- | `"$dot$dot"`.
Expand All @@ -175,8 +175,8 @@ dotEscaper = Escaper \s -> case NES.toString s of
"." -> unsafePartial NES.unsafeFromString "$dot"
_ -> s

-- | An escaper that removes all slashes, converts ".." into "$dot$dot", and
-- | converts "." into "$dot".
-- | An escaper that converts slashes into "$slash", ".." into "$dot$dot", and
-- | "." into "$dot".
posixEscaper :: Escaper
posixEscaper = slashEscaper <> dotEscaper

Expand Down
24 changes: 24 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ main = do
(printPath posixPrinter $ sandboxAny rootDir)
"""/"""

test """printPath posixPrinter - escape . - /C/$dot/"""
(printPath posixPrinter $ sandboxAny $ rootDir </> dir (Proxy :: Proxy "C") </> dir (Proxy :: Proxy "."))
"""/C/$dot/"""

test """printPath posixPrinter - escape .. - /C/$dot$dot/"""
(printPath posixPrinter $ sandboxAny $ rootDir </> dir (Proxy :: Proxy "C") </> dir (Proxy :: Proxy ".."))
"""/C/$dot$dot/"""

test """printPath posixPrinter - don't escape ... - /C/.../"""
(printPath posixPrinter $ sandboxAny $ rootDir </> dir (Proxy :: Proxy "C") </> dir (Proxy :: Proxy "..."))
"""/C/.../"""

test """printPath posixPrinter - don't escape foo.bar - /C/foo.bar/"""
(printPath posixPrinter $ sandboxAny $ rootDir </> dir (Proxy :: Proxy "C") </> dir (Proxy :: Proxy "foo.bar"))
"""/C/foo.bar/"""

test """printPath posixPrinter - escape / - /C/$slash/"""
(printPath posixPrinter $ sandboxAny $ rootDir </> dir (Proxy :: Proxy "C") </> dir (Proxy :: Proxy "/"))
"""/C/$slash/"""

test """printPath posixPrinter - escape foo/bar - /C/foo$slashbar/"""
(printPath posixPrinter $ sandboxAny $ rootDir </> dir (Proxy :: Proxy "C") </> dir (Proxy :: Proxy "foo/bar"))
"""/C/foo$slashbar/"""

test' "(</>) - ./../foo/"
(parentOf currentDir </> dirFoo)
"./../foo/"
Expand Down