Skip to content

Commit

Permalink
allow for multiple trailing and/or leading whitespace in header (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjyetman authored Jan 15, 2024
1 parent b0997c0 commit 8e2543d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
6 changes: 3 additions & 3 deletions R/determine_headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ determine_headers <- function(filepath) {
num_of_cols <- length(headers)

if (num_of_cols >= 3) {
isin_col <- grep(pattern = "^[[:space:]]?isin[[:space:]]?$", x = headers, ignore.case = TRUE, value = TRUE)
market_value_col <- grep(pattern = "^[[:space:]]?market[._ ]{0,1}value[[:space:]]?$", x = headers, ignore.case = TRUE, value = TRUE)
currency_col <- grep(pattern = "^[[:space:]]?currency[[:space:]]?$", x = headers, ignore.case = TRUE, value = TRUE)
isin_col <- grep(pattern = "^[[:space:]]*isin[[:space:]]*$", x = headers, ignore.case = TRUE, value = TRUE)
market_value_col <- grep(pattern = "^[[:space:]]*market[._ ]{0,1}value[[:space:]]*$", x = headers, ignore.case = TRUE, value = TRUE)
currency_col <- grep(pattern = "^[[:space:]]*currency[[:space:]]*$", x = headers, ignore.case = TRUE, value = TRUE)

if (num_of_cols > 3) {
investor_name_col <- grep(pattern = "investor", x = headers, ignore.case = TRUE, value = TRUE)
Expand Down
30 changes: 29 additions & 1 deletion tests/testthat/test-determine_headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,35 @@ test_that("column names with no underscore are properly determined", {
test_that("column names with leading and lagging whitespace are properly determined", {
proper_names <- c("investor_name", "portfolio_name", "isin", "market_value", "currency")
lead_and_lag_whitespace <- withr::local_tempfile()
writeLines(" investor_name,portfolio_name , isin ,market_value,currency\nx,y,z,1,a", lead_and_lag_whitespace)
writeLines(" investor_name,portfolio_name , isin , market_value,currency \nx,y,z,1,a", lead_and_lag_whitespace)
expect_setequal(names(determine_headers(lead_and_lag_whitespace)), proper_names)
})

test_that("column names with leading and lagging whitespace are properly determined (double-padded)", {
proper_names <- c("investor_name", "portfolio_name", "isin", "market_value", "currency")
lead_and_lag_whitespace <- withr::local_tempfile()
writeLines(" investor_name,portfolio_name , isin , market_value,currency \nx,y,z,1,a", lead_and_lag_whitespace)
expect_setequal(names(determine_headers(lead_and_lag_whitespace)), proper_names)
})

test_that("column names with leading and lagging whitespace are properly determined (tab-padded)", {
proper_names <- c("investor_name", "portfolio_name", "isin", "market_value", "currency")
lead_and_lag_whitespace <- withr::local_tempfile()
writeLines("\tinvestor_name,portfolio_name\t,\tisin\t,\tmarket_value,currency \nx,y,z,1,a", lead_and_lag_whitespace)
expect_setequal(names(determine_headers(lead_and_lag_whitespace)), proper_names)
})

test_that("column names with leading and lagging whitespace are properly determined (double-tab-padded)", {
proper_names <- c("investor_name", "portfolio_name", "isin", "market_value", "currency")
lead_and_lag_whitespace <- withr::local_tempfile()
writeLines("\t\tinvestor_name,portfolio_name\t\t,\t\tisin\t\t,\t\tmarket_value,currency \nx,y,z,1,a", lead_and_lag_whitespace)
expect_setequal(names(determine_headers(lead_and_lag_whitespace)), proper_names)
})

test_that("column names with leading and lagging whitespace are properly determined (space-and-tab-padded)", {
proper_names <- c("investor_name", "portfolio_name", "isin", "market_value", "currency")
lead_and_lag_whitespace <- withr::local_tempfile()
writeLines(" \tinvestor_name,portfolio_name\t ,\t isin\t\t, \tmarket_value,currency \nx,y,z,1,a", lead_and_lag_whitespace)
expect_setequal(names(determine_headers(lead_and_lag_whitespace)), proper_names)
})

Expand Down

0 comments on commit 8e2543d

Please sign in to comment.