Skip to content

Commit

Permalink
Fix and test acronym polishing (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRoniOne authored Feb 2, 2024
1 parent ce2513f commit 89e8aa3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/polish_names.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 = ""

Expand Down
4 changes: 4 additions & 0 deletions test/test_polish_names.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ using DataFrames: DataFrame
" _aName with_loTsOfProblems",
" _aName with_loTsOfProblems_1",
" _aName with_loTsOfProblems_1_a/b'c",
"ID",
];
style=:snake_case,
) == Vector{Symbol}([
:a_name_with_lo_ts_of_problems,
: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(
Expand All @@ -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}([
Expand All @@ -57,6 +60,7 @@ using DataFrames: DataFrame
:aNameWithLoTsOfProblems1,
:aNameABCWithLoTsOfProblemsDEF,
:aNameABCWithLoTsOfProblemsDEFABC,
:id,
])

let err = nothing
Expand Down

0 comments on commit 89e8aa3

Please sign in to comment.