We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Post 6
camel_case to snake_case
Published on: 2025-05-07
Tags:
elixir, Generators , Blog, NimbleParsec
This one started off small and grew into more and more; create a function that parses a data file and replaces camelCase with snake_case. I was able to figure out the camelCase pretty quick, set up: lowercase = ascii_char([?a..?z]) uppercase = ascii_char([?A..?Z]) word_start = concat(uppercase, repeat(lowercase)) |> reduce({List, :to_string, []}) first_word = concat(lowercase, repeat(lowercase)) |> reduce({List, :to_string, []}) camel_case = concat(first_word, times(word_start, min: 1)) As you can see I set up the letters and words and set a minimum for the amount of first_word's that are needed to trigger a replace. Next came the replacement which again just needed a join and a downcase. defparsec( :parse_camel_case, camel_case |> reduce({Enum, :join, ["_"]}) |> reduce({Enum, :map, [&String.downcase/1]}) ) Okay that is good I know how to parse any word and it only triggers if it follows the pattern given. Now we need to start opening files and parsing each line. I'll add an other blog entrance for the file part of this and will stop here.
