Skip to content
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

Fix and test acronym polishing #13

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading