Skip to content

Commit 6602d84

Browse files
committed
mv 'assert_ext' -> 'has_ext' & mv to 'utils.R' for #38
1 parent 2a0ad5b commit 6602d84

File tree

5 files changed

+37
-37
lines changed

5 files changed

+37
-37
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Collate:
6666
'gtfsio.R'
6767
'import_gtfs.R'
6868
'new_gtfs.R'
69+
'utils.R'
6970
LazyData: true
7071
Depends:
7172
R (>= 3.1.0)

R/assert_inputs.R

-13
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,3 @@ error_x_wrong_inheritance <- function(input_name,
278278
error_call
279279
)
280280
}
281-
282-
#' Vectorized assertion of path extensions
283-
#'
284-
#' @param path Vector of file paths
285-
#' @param ext File extension to be asserted for each `path`
286-
#'
287-
#' @return Logical vector of same length as `path`, with `TRUE` for each element
288-
#' with specified extension, `FALSE` otherwise.
289-
#'
290-
#' @noRd
291-
assert_extension <- function(path, ext = "zip") {
292-
fs::path_ext(path) == ext
293-
}

R/export_gtfs.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export_gtfs <- function(gtfs,
6868
# input checks that depend on more than one argument
6969

7070
if (fs::file_exists(path) & !overwrite) error_cannot_overwrite()
71-
if (!as_dir & !assert_extension(path, "zip")) error_ext_must_be_zip()
72-
if (as_dir & assert_extension(path, "zip")) error_path_must_be_dir()
71+
if (!as_dir & !has_extension(path, "zip")) error_ext_must_be_zip()
72+
if (as_dir & has_extension(path, "zip")) error_path_must_be_dir()
7373

7474
extra_files <- setdiff(files, names(gtfsio::gtfs_reference))
7575
if (standard_only & !is.null(files) & !identical(extra_files, character(0))) {
@@ -132,7 +132,7 @@ export_gtfs <- function(gtfs,
132132

133133
dt <- gtfs[[file]]
134134

135-
if (assert_extension(filename, "geojson")) {
135+
if (has_extension(filename, "geojson")) {
136136
jsonlite::write_json(dt, filepath, pretty = FALSE, auto_unbox = TRUE, digits = 8)
137137
} else {
138138

R/import_gtfs.R

+2-21
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ import_gtfs <- function(path,
117117
)
118118
if (inherits(filenames_in_gtfs, "error")) error_path_must_be_zip()
119119

120-
non_standard_file_ext <- filenames_in_gtfs[!(assert_extension(filenames_in_gtfs, "txt") | assert_extension(filenames_in_gtfs, "geojson"))]
120+
non_standard_file_ext <- filenames_in_gtfs[!(has_extension(filenames_in_gtfs, "txt") | has_extension(filenames_in_gtfs, "geojson"))]
121121

122122
if (!identical(non_standard_file_ext, character(0))) {
123123
warning(
@@ -241,7 +241,7 @@ read_files <- function(file,
241241
stopifnot(length(file) == 1L)
242242

243243
filename <- file
244-
file_type <- ifelse(assert_extension(file, "txt"), "txt", "geojson")
244+
file_type <- ifelse(has_extension(file, "txt"), "txt", "geojson")
245245
file <- remove_file_ext(file)
246246

247247
if (!quiet) message("Reading ", file)
@@ -374,25 +374,6 @@ read_geojson <- function(file.geojson) {
374374
read_json(file.geojson)
375375
}
376376

377-
remove_file_ext = function(file) {
378-
fs::path_ext_remove(file)
379-
}
380-
381-
append_file_ext = function(file) {
382-
vapply(file, function(.f) {
383-
file_ext <- gtfsio::gtfs_reference[[remove_file_ext(.f)]][["file_ext"]]
384-
if (is.null(file_ext)) {
385-
# use default for argument-specified non-standard files,
386-
# behaviour defined in test_import_gtfs.R#292
387-
file_ext <- "txt"
388-
}
389-
if (!assert_extension(.f, file_ext)) {
390-
.f <- fs::path_ext_set(.f, file_ext)
391-
}
392-
return(.f)
393-
}, ".txt", USE.NAMES = FALSE)
394-
}
395-
396377
# errors ------------------------------------------------------------------
397378

398379

R/utils.R

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
remove_file_ext = function(file) {
2+
fs::path_ext_remove(file)
3+
}
4+
5+
append_file_ext = function(file) {
6+
vapply(file, function(.f) {
7+
file_ext <- gtfsio::gtfs_reference[[remove_file_ext(.f)]][["file_ext"]]
8+
if (is.null(file_ext)) {
9+
# use default for argument-specified non-standard files,
10+
# behaviour defined in test_import_gtfs.R#292
11+
file_ext <- "txt"
12+
}
13+
if (!has_extension(.f, file_ext)) {
14+
.f <- fs::path_ext_set(.f, file_ext)
15+
}
16+
return(.f)
17+
}, ".txt", USE.NAMES = FALSE)
18+
}
19+
20+
#' Vectorized assertion of path extensions
21+
#'
22+
#' @param path Vector of file paths
23+
#' @param ext File extension to be asserted for each `path`
24+
#'
25+
#' @return Logical vector of same length as `path`, with `TRUE` for each element
26+
#' with specified extension, `FALSE` otherwise.
27+
#'
28+
#' @noRd
29+
has_extension <- function(path, ext = "zip") {
30+
fs::path_ext(path) == ext
31+
}

0 commit comments

Comments
 (0)