From d7f432dec484559af1d6e6d248e39e0d8c96b9c7 Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Mon, 1 Apr 2024 19:52:40 +0300 Subject: [PATCH 1/3] Minor docstring phrasing and whitespace fixes. --- src/filepath.gleam | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/filepath.gleam b/src/filepath.gleam index 3dae367..f62b6c6 100644 --- a/src/filepath.gleam +++ b/src/filepath.gleam @@ -59,12 +59,10 @@ fn remove_trailing_slash(path: String) -> String { } } -// TODO: Windows support /// Split a path into its segments. /// -/// When running on Windows both `/` and `\` are treated as path separators, and -/// if the path starts with a drive letter then the drive letter then it is -/// lowercased. +/// When running on Windows both `/` and `\` are treated as path separators, and +/// if the path starts with a drive letter then the drive letter is lowercased. /// /// ## Examples /// @@ -192,24 +190,24 @@ pub fn extension(path: String) -> Result(String, Nil) { } /// Remove the extension from a file, if it has any. -/// +/// /// ## Examples -/// +/// /// ```gleam /// strip_extension("src/main.gleam") /// // -> "src/main" /// ``` -/// +/// /// ```gleam /// strip_extension("package.tar.gz") /// // -> "package.tar" /// ``` -/// +/// /// ```gleam /// strip_extension("src/gleam") /// // -> "src/gleam" /// ``` -/// +/// pub fn strip_extension(path: String) -> String { case extension(path) { Ok(extension) -> From 7be661028b8ab38890e619dcd6e01c8752e40669 Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Wed, 27 Mar 2024 16:47:52 +0200 Subject: [PATCH 2/3] Add path separator constants and utility function. Define constants for `path_separator_{unix,windows}` and add `path_separator()` utility function getting the right one for the platform currently being run on. --- src/filepath.gleam | 32 ++++++++++++++++++++++++++++++++ test/filepath_test.gleam | 22 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/filepath.gleam b/src/filepath.gleam index f62b6c6..f05ade5 100644 --- a/src/filepath.gleam +++ b/src/filepath.gleam @@ -20,6 +20,38 @@ import gleam/option.{type Option, None, Some} @external(javascript, "./filepath_ffi.mjs", "is_windows") fn is_windows() -> Bool +/// Path separator for Unix platforms. +pub const path_separator_unix = "/" + +/// Path separator for the Windows platform. +pub const path_separator_windows = "\\" + +/// Returns the path separator for the operating system +/// which it's currently being run on. +/// +/// For Windows, this will be `\`. +/// For any non-Windows platform, this will be `/`. +/// +/// ## Examples +/// +/// ```gleam +/// path_separator() +/// // -> "/" +/// ``` +/// +/// ```gleam +/// // Windows-only behavior: +/// path_separator() +/// // -> "\\" +/// ``` +/// +pub fn path_separator() -> String { + case is_windows() { + True -> path_separator_windows + False -> path_separator_unix + } +} + /// Join two paths together. /// /// This function does not expand `..` or `.` segments, use the `expand` diff --git a/test/filepath_test.gleam b/test/filepath_test.gleam index 475c97b..1ec81c5 100644 --- a/test/filepath_test.gleam +++ b/test/filepath_test.gleam @@ -20,6 +20,28 @@ fn windows_only(f: fn() -> whatever) -> Nil { } } +fn non_windows_only(f: fn() -> whatever) -> Nil { + case is_windows() { + False -> { + f() + Nil + } + True -> Nil + } +} + +pub fn path_separator_windows_0_test() { + use <- windows_only + filepath.path_separator() + |> should.equal(filepath.path_separator_windows) +} + +pub fn path_separator_non_windows_0_test() { + use <- non_windows_only + filepath.path_separator() + |> should.equal(filepath.path_separator_unix) +} + pub fn split_0_test() { filepath.split("") |> should.equal([]) From 3cc0bf02cb9eb45f14a4bf76f74ebc9e2e09aed2 Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Mon, 1 Apr 2024 20:22:19 +0300 Subject: [PATCH 3/3] Bump version to 1.1.0 and update CHANGELOG. --- CHANGELOG.md | 6 ++++++ gleam.toml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28f57a2..1a5acbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## v1.1.0 - 2024-04-01 + +- Add `path_separator_unix` and `path_separator_windows` constants. +- Add `path_separator()` function for returning the path separator + for the platform currently being run on. + ## v1.0.0 - 2024-02-08 - All existing functions now support Windows paths when run on Windows. diff --git a/gleam.toml b/gleam.toml index 0922a7f..f5c419a 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,5 +1,5 @@ name = "filepath" -version = "1.0.0" +version = "1.1.0" description = "Work with file paths in Gleam!" licences = ["Apache-2.0"] repository = { type = "github", user = "lpil", repo = "filepath" }