Skip to content

Commit

Permalink
primeira versao de padronizar_ceps()
Browse files Browse the repository at this point in the history
  • Loading branch information
dhersz committed Jan 24, 2024
1 parent d345c8b commit e468f0a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 1 deletion.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Description: Padroniza endereços brasileiros a partir de diferentes
License: MIT + file LICENSE
URL: https://github.com/ipeaGIT/enderecopadrao
BugReports: https://github.com/ipeaGIT/enderecopadrao/issues
Imports:
checkmate,
stringr
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(padronizar_ceps)
44 changes: 44 additions & 0 deletions R/padronizar_ceps.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' Padronizar CEPs
#'
#' Padroniza CEPs.
#'
#' @param ceps Um vetor de caracteres ou números. Os CEPs a serem padronizados.
#'
#' @return Um vetor de caracteres com os CEPs padronizados.
#'
#' @examples
#' ceps <- c("22290-140", "22.290-140", "22290 140", "22290140")
#' padronizar_ceps(ceps)
#'
#' ceps <- c(22290140, 1000000, NA)
#' padronizar_ceps(ceps)
#'
#' @export
padronizar_ceps <- function(ceps) {
checkmate::assert(
checkmate::check_character(ceps),
checkmate::check_numeric(ceps),
combine = "or"
)

# alguns ceps podem vir vazios e devem permanecer vazios ao final. nesse caso,
# a chamada da str_pad() abaixo faz com que esses ceps virem "00000000". para
# evitar que o resultado contenha esses valores, identificamos o indice dos
# ceps vazios para ao final "reesvazia-los"

indice_cep_vazio <- which(ceps == "" | is.na(ceps))

ceps_padrao <- if (is.numeric(ceps)) {
format(ceps, scientific = FALSE)
} else {
ceps
}

ceps_padrao <- stringr::str_squish(ceps_padrao)
ceps_padrao <- stringr::str_replace_all(ceps_padrao, "\\.|-|,| ", "")
ceps_padrao <- stringr::str_pad(ceps_padrao, width = 8, pad = "0")

ceps_padrao[indice_cep_vazio] <- ""

return(ceps_padrao)
}
5 changes: 5 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
reference:
- title: "Campos individuais"
- contents:
- padronizar_ceps

development:
mode: auto
29 changes: 28 additions & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,34 @@
}
],
"softwareRequirements": {
"1": {
"@type": "SoftwareApplication",
"identifier": "checkmate",
"name": "checkmate",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=checkmate"
},
"2": {
"@type": "SoftwareApplication",
"identifier": "stringr",
"name": "stringr",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=stringr"
},
"SystemRequirements": null
},
"fileSize": "4.336KB"
"fileSize": "6.204KB",
"readme": "https://github.com/ipeaGIT/enderecopadrao/blob/main/README.md",
"contIntegration": ["https://github.com/ipeaGIT/enderecopadrao/actions?query=workflow%3Acheck", "https://app.codecov.io/gh/ipeaGIT/enderecopadrao?branch=main"],
"developmentStatus": "https://lifecycle.r-lib.org/articles/stages.html"
}
25 changes: 25 additions & 0 deletions man/padronizar_ceps.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e468f0a

Please sign in to comment.