diff --git a/src/polish_names.jl b/src/polish_names.jl index f71500f..31cc037 100644 --- a/src/polish_names.jl +++ b/src/polish_names.jl @@ -42,6 +42,8 @@ end Return a vector of symbols containing new names that are unique and formated using the `style` selected. """ function generate_polished_names(names; style::Symbol=:snake_case) + names = _preprocess_name.(names) + return generate_polished_names(names, Style(style)) end @@ -50,7 +52,7 @@ function generate_polished_names(names, ::Style{:snake_case}) for name in names new_name = _sanitize_snake_case( - join(split(_replace_uppers(String(name)), SPECIAL_CHARS; keepempty=false), "_") + join(split(_replace_uppers(name), SPECIAL_CHARS; keepempty=false), "_") ) push!(new_names, new_name) end @@ -63,7 +65,7 @@ function generate_polished_names(names, ::Style{:camelCase}) for name in names new_name = lowercasefirst( - join(uppercasefirst.(split(String(name), SPECIAL_CHARS; keepempty=false)), "") + join(uppercasefirst.(split(name, SPECIAL_CHARS; keepempty=false)), "") ) push!(new_names, new_name) end @@ -75,6 +77,17 @@ function generate_polished_names(names, ::Style) return error("Invalid style selected. Options are :snake_case, :camelCase") end +function _preprocess_name(name) + preprocessed = String(name) + + matched = match(r"^[[:upper:]]+$", preprocessed) + if matched !== nothing + return lowercase(preprocessed) + end + + return preprocessed +end + function _replace_uppers(word) fixed_word = "" diff --git a/test/test_polish_names.jl b/test/test_polish_names.jl index c3c0d34..e77b74d 100644 --- a/test/test_polish_names.jl +++ b/test/test_polish_names.jl @@ -33,6 +33,7 @@ using DataFrames: DataFrame " _aName with_loTsOfProblems", " _aName with_loTsOfProblems_1", " _aName with_loTsOfProblems_1_a/b'c", + "ID", ]; style=:snake_case, ) == Vector{Symbol}([ @@ -40,6 +41,7 @@ using DataFrames: DataFrame :a_name_with_lo_ts_of_problems_1, :a_name_with_lo_ts_of_problems_1_1, :a_name_with_lo_ts_of_problems_1_a_b_c, + :id, ]) @test generate_polished_names( @@ -49,6 +51,7 @@ using DataFrames: DataFrame " _aName with_loTsOfProblems_1", " _aNameABC with_loTsOfProblemsDEF", " _aNameABC with_loTsOfProblemsDEF_a/b'c", + "ID", ]; style=:camelCase, ) == Vector{Symbol}([ @@ -57,6 +60,7 @@ using DataFrames: DataFrame :aNameWithLoTsOfProblems1, :aNameABCWithLoTsOfProblemsDEF, :aNameABCWithLoTsOfProblemsDEFABC, + :id, ]) let err = nothing