Public-Health-Scotland/phsmethods

Make chi_pad() more strict

Closed this issue ยท 0 comments

Hi guys!

Just because I've left PHS, it doesn't mean you're shot of me ๐Ÿ‘€ I'm still working with health data and still using phsmethods.

At the moment chi_pad() adds a leading zero to any nine digit character string:

phsmethods::chi_pad(c("123456789", "abcdefghi", "23"))
#> [1] "0123456789" "0abcdefghi" "23"

I'd like to make it a little more strict, so that it only adds a leading zero to strings comprised of nine numeric digits:

chi_pad <- function(x) {
  
  if (!inherits(x, "character")) {
    stop("The input must be of character class")
  }
  
  # Add a leading zero to any string comprised of nine numeric digits
  ifelse(stringr::str_detect(x, "^[0-9]{9}$"),
         paste0("0", x),
         x)
}

chi_pad(c("123456789", "abcdefghi", "23"))
#> [1] "0123456789" "abcdefghi"  "23"

I can update the documentation and tests as well.

Thanks!