Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
48 changes: 39 additions & 9 deletions src/filepath.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -59,12 +91,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
///
Expand Down Expand Up @@ -192,24 +222,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) ->
Expand Down
22 changes: 22 additions & 0 deletions test/filepath_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down