R/sedol.R

Defines functions sedol_compute_checksum sedol_has_correct_checksum sedol_check

Documented in sedol_check

sedol_compute_checksum <- function(s) {
  u8.l <- figi_char_to_utf8(s)
  figi_code.l <- base::lapply(u8.l, FUN = figi_utf8_to_code)
  mulw.l <- base::lapply(figi_code.l,
                         FUN = function (v) { v * c(1L, 3L, 1L, 7L, 3L, 9L) })
  sum_digits.v <- base::vapply(mulw.l, FUN = sum, FUN.VALUE = NA_integer_)
  check_digit.v <- (10L - sum_digits.v %% 10L) %% 10L
  as.character(check_digit.v)
}

sedol_has_correct_checksum <- function(s) {
  sedol_compute_checksum(substr(s, 1L, 6L)) == substr(s, 7L, 7L)
}

#' Check validity of SEDOL
#'
#' Given a character vector, check the validity of SEDOL
#' (Stock Exchange Daily Official List)
#' for each of its elements.
#'
#' @param s a character vector for whose elements validity of SEDOL
#'   is checked.
#'
#' @return A logical vector.
#'
#' @examples
#' sedol_check('B014635')
#' sedol_check('0263494')
#' sedol_check(c('B014635', '0263494'))
#' sedol_check(c('B014635', '0263495'))
#'
#' @export
sedol_check <- function(s) {
  !is.na(s) &
  base::nchar(s) == 7L &
    grepl('^[0-9BCDFGHJKLMNPQRSTVWXYZ]{7}$',
          s) &
    sedol_has_correct_checksum(s)
}
philaris/figir documentation built on Jan. 13, 2020, 11:27 p.m.