Camel case and snake case
Two common conventions for naming of computer program variables are Snake Case and Camel Case.
Snake case variables are generally all lower case, with an underscore between words in the variable, as in snake_case_variable'. Camel case variables are generally lower case first (except in some Pascal conventions or with class names in many other languages), with captalization of the initial letter of the words within the variable, as in 'camelCaseVariable'.
Leading underscores are not used in such variables except as part of a different naming convention, usually for special internal or system variables. White space is not permitted as part of camel case or snake case variable names.
- Task
- Write two functions, one to change snake case to camel case and one to change camel case to snake case. If possible, generalize the function enough to apply to strings containing spaces between words or a `-` dash between words, assuming that in those cases a space or hyphen is a also a separator character, like `_`, for the purpose of creating a new variable name. Leading or trailing whitespace may be ignored.
- Show the results on changing to both snake case and camel case for each of the following strings:
<lang java> "snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces " </lang>
- Related tasks
Raku
The specs are a little vague, but taking a wild stab at it...
<lang perl6>my @tests = qww<
snakeCase snake_case variable_10_case variable10Case "ɛrgo rE tHis" hurry-up-joe! c://my-docs/happy_Flag-Day/12.doc " spaces "
>;
sub to_snake_case (Str $snake_case_string is copy) {
$snake_case_string.=trim; return $snake_case_string if $snake_case_string.contains: / \s | '/' /; $snake_case_string.=subst: / <after <:Ll>> (<:Lu>|<:digit>+) /, {'_' ~ $0.lc}, :g; $snake_case_string.=subst: / <after <:digit>> (<:Lu>) /, {'_' ~ $0.lc}, :g;
}
sub toCamelCase (Str $camelCaseString is copy) {
$camelCaseString.=trim; return $camelCaseString if $camelCaseString.contains: / \s | '/' /; $camelCaseString.=subst: / ('_') (\w) /, {$1.uc}, :g;
}
sub to-kebab-case (Str $kebab-case-string is copy) {
$kebab-case-string.=trim; return $kebab-case-string if $kebab-case-string.contains: / \s | '/' /; $kebab-case-string.=subst: / ('_') (\w) /, {'-' ~ $1.lc}, :g; $kebab-case-string.=subst: / <after <:Ll>> (<:Lu>|<:digit>+) /, {'-' ~ $0.lc}, :g; $kebab-case-string.=subst: / <after <:digit>> (<:Lu>) /, {'-' ~ $0.lc}, :g;
}
say "{' ' x 32}to_snake_case"; printf "%35s ==> %-35s\n", $_, .&to_snake_case for @tests; say "\n{' ' x 32}toCamelCase"; printf "%35s ==> %-35s\n", $_, .&toCamelCase for @tests; say "\n{' ' x 32}to-kabab-case"; printf "%35s ==> %-35s\n", $_, .&to-kebab-case for @tests;</lang>
- Output:
to_snake_case snakeCase ==> snake_case snake_case ==> snake_case variable_10_case ==> variable_10_case variable10Case ==> variable_10_case ɛrgo rE tHis ==> ɛrgo rE tHis hurry-up-joe! ==> hurry-up-joe! c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc spaces ==> spaces toCamelCase snakeCase ==> snakeCase snake_case ==> snakeCase variable_10_case ==> variable10Case variable10Case ==> variable10Case ɛrgo rE tHis ==> ɛrgo rE tHis hurry-up-joe! ==> hurry-up-joe! c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc spaces ==> spaces to-kabab-case snakeCase ==> snake-case snake_case ==> snake-case variable_10_case ==> variable-10-case variable10Case ==> variable-10-case ɛrgo rE tHis ==> ɛrgo rE tHis hurry-up-joe! ==> hurry-up-joe! c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc spaces ==> spaces