From 87bee7dcc0ccc91d2537cfffb613b471b341aa37 Mon Sep 17 00:00:00 2001 From: InversionSpaces Date: Tue, 5 Dec 2023 10:43:36 +0000 Subject: [PATCH] Use path.parse + path.format --- io/js/src/main/scala/fs2/io/file/Path.scala | 28 ++++++++----------- .../scala/fs2/io/internal/facade/path.scala | 4 +++ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/io/js/src/main/scala/fs2/io/file/Path.scala b/io/js/src/main/scala/fs2/io/file/Path.scala index d0f8125d94..fc52f1b0a0 100644 --- a/io/js/src/main/scala/fs2/io/file/Path.scala +++ b/io/js/src/main/scala/fs2/io/file/Path.scala @@ -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) + } } diff --git a/io/js/src/main/scala/fs2/io/internal/facade/path.scala b/io/js/src/main/scala/fs2/io/internal/facade/path.scala index c657165c2e..d890a7c745 100644 --- a/io/js/src/main/scala/fs2/io/internal/facade/path.scala +++ b/io/js/src/main/scala/fs2/io/internal/facade/path.scala @@ -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 }