Skip to content

Commit

Permalink
Use path.parse + path.format
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces committed Dec 5, 2023
1 parent b35d234 commit 87bee7d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
28 changes: 11 additions & 17 deletions io/js/src/main/scala/fs2/io/file/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,15 @@ final case class Path private[file] (override val toString: String) extends Path
object Path extends PathCompanionApi {
private[file] val sep = facade.path.sep

/*
* NOTE: It seems like scala js does not rewrite this to a loop?
* Call stack limit is reached if there are too many separators.
*/
@tailrec
private[file] def dropTrailingSep(path: String): String =
// Drop separator only if there is something else left
if (path.endsWith(sep) && path.length > sep.length)
dropTrailingSep(path.dropRight(sep.length))
else path

/** This method drops trailing separators to match
* `java.nio.file.Path.get` behaviour.
* But root ("/") is untouched.
*/
def apply(path: String): Path =
new Path(dropTrailingSep(path))
def apply(path: String): Path = {

/** Parse and then reconstruct the path
* to drop all trailing separators
* to match `java.nio.file.Paths.get` behaviour.
*/
val parsed = facade.path.parse(path)
val formatted = facade.path.format(parsed)

new Path(formatted)
}
}
4 changes: 4 additions & 0 deletions io/js/src/main/scala/fs2/io/internal/facade/path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ private[io] object path {
def name: String = js.native
def ext: String = js.native
}

@js.native
@JSImport("path", "format")
def format(pathObject: ParsedPath): String = js.native
}

0 comments on commit 87bee7d

Please sign in to comment.