Skip to content

Commit

Permalink
fix: common_path was returning wrong result when input is length 1
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed Aug 2, 2024
1 parent 313127d commit 80b272c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MsCoreUtils
Title: Core Utils for Mass Spectrometry Data
Version: 1.17.0
Version: 1.17.1
Description: MsCoreUtils defines low-level functions for mass
spectrometry data and is independent of any high-level data
structures. These functions include mass spectra processing
Expand Down Expand Up @@ -80,4 +80,4 @@ BugReports: https://github.com/RforMassSpectrometry/MsCoreUtils/issues
URL: https://github.com/RforMassSpectrometry/MsCoreUtils
biocViews: Infrastructure, Proteomics, MassSpectrometry, Metabolomics
Roxygen: list(markdown=TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# MsCoreUtils 1.17

## MsCoreUtils 1.17.1

- Fix `common_path()` to not return also the file name if the input parameter
contains only identical character strings.

# MsCoreUtils 1.15

## MsCoreUtils 1.15.7
Expand Down
28 changes: 18 additions & 10 deletions R/file_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#'
#' @description
#'
#' Find the common part of the path for a provided set of files.
#' Find the common part of the path up to a provided set of files. Be aware that
#' the last element (after the last file separator) is treated as a *file*.
#' Thus, if only directories, without files are submitted, the common path
#' containing these directories is returned.
#'
#' @param x `character` with the file names (including paths).
#' @param x `character` with the **file names** (including paths).
#'
#' @param fsep `character(1)` defining the file separator to be used in
#' the returned common path. Defaults to the system platform's file
Expand Down Expand Up @@ -42,16 +45,21 @@
#'
#' ## No path
#' common_path(c("a.txt", "b.txt"))
#'
#' ## Same path for all
#' common_path(c("a/a.txt", "a/a.txt"))
common_path <- function(x, fsep = .Platform$file.sep) {
if (!length(x))
return(character())
sx <- strsplit(x, split = "(\\\\)|/")
minl <- min(lengths(sx))
cpath <- character()
for (i in seq_len(minl)) {
uvals <- unique(vapply(sx, `[`, character(1), i = i))
if (length(uvals) == 1L)
cpath <- c(cpath, uvals)
}
paste0(cpath, collapse = fsep)
minl <- min(lengths(sx)) - 1L
if (minl > 0) {
cpath <- character()
for (i in seq_len(minl)) {
uvals <- unique(vapply(sx, `[`, character(1), i = i))
if (length(uvals) == 1L)
cpath <- c(cpath, uvals)
}
paste0(cpath, collapse = fsep)
} else ""
}
10 changes: 8 additions & 2 deletions man/common_path.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/test_file_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ test_that("common_path works", {
expect_equal(common_path(x), "C:/first/second")
x <- c("C:\\first\\second\\a.txt", "C:\\first\\second\\third\\b.txt")
expect_equal(common_path(x), "C:/first/second")

## Check if we have a single input.
expect_equal(common_path("a/b.txt"), "a")
expect_equal(common_path("a/b.txt", "a/b.txt"), "a")
})

0 comments on commit 80b272c

Please sign in to comment.