-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Valid NPI Creation: "Double-Add-Double" Luhn #17
Comments
Could create an NPI constructor, ala: Function code
library(provider)
library(dplyr)
library(purrr)
library(rlang)
create_npi <- function(npi,
arg = rlang::caller_arg(npi),
call = rlang::caller_env()) {
# Must be numeric
if (grepl("^[[:digit:]]+$", npi) == FALSE) {
cli::cli_abort(c(
"An {.strong NPI} must be {.emph numeric}.",
"x" = "{.val {npi}} contains {.emph non-numeric} characters."),
call = call)
}
# Must be 10 char length
if (nchar(npi) != 10L) {
cli::cli_abort(c(
"An {.strong NPI} must be {.emph 100 digits long}.",
"x" = "{.val {npi}} contains {.val {nchar(npi)}} digit{?s}."),
call = call)
}
# Must pass Luhn algorithm
npi_test <- as.character(npi)
# Remove the 10th digit to create the 9-position identifier part of the NPI
id <- unlist(strsplit(npi_test, ""), use.names = FALSE)[1:9]
# Reverse order of digits
x <- rev(id)
# Select index of every other digit
idx <- seq(1, length(x), 2)
# Double the value of the alternate digits
x[idx] <- as.numeric(x[idx]) * 2
# Reverse order of digits again
x <- rev(x)
# Split and unlist to separate digits
x <- unlist(strsplit(x, ""), use.names = FALSE)
# Add constant 24 to the sum of the digits
x <- sum(as.numeric(x)) + 24
# Find the next higher number ending in zero
y <- ceiling(x / 10) * 10
# Find the check digit by subtracting x from y
z <- y - x
# Append the check digit to the end of the 9-digit identifier
id[10] <- z
# Collapse the vector into a single string
npi_valid <- paste0(id, collapse = "")
results <- list(
input = npi_test,
valid = npi_valid,
equal = identical(npi_valid, npi_test))
return(results)
} # invalid
create_npi(1234567891)
#> $input
#> [1] "1234567891"
#>
#> $valid
#> [1] "1234567893"
#>
#> $equal
#> [1] FALSE
create_npi(1528060439)
#> $input
#> [1] "1528060439"
#>
#> $valid
#> [1] "1528060431"
#>
#> $equal
#> [1] FALSE
# valid
create_npi(1528060837)
#> $input
#> [1] "1528060837"
#>
#> $valid
#> [1] "1528060837"
#>
#> $equal
#> [1] TRUE Created on 2023-10-23 with reprex v2.0.2 |
Replace
|
andrewallenbruce
added a commit
that referenced
this issue
Oct 27, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Luhn Formula for Modulus 10 “double-add-double” Check Digit
Example of Check Digit Calculation for NPI used without Prefix:
Created on 2023-10-12 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: