Camel case and snake case

From Rosetta Code
Revision as of 10:21, 24 November 2021 by PureFox (talk | contribs) (Added Wren)
Camel case and snake case is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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... (May be completely wrong but without any examples of expected output it is hard to judge. This is what I would expect at least...)

<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

Wren

Library: Wren-str
Library: Wren-fmt

Well, I'm not entirely sure what I'm doing here as a result of space and hyphen being treated as equivalent to underscore but, in the case of the 'to snake' conversion:

1. I've retained any hyphens in the result string but replaced spaces with underscores as it says that white space is not permitted as part of the variable name.

2. I've assumed that an underscore should not be added if the previous character was already a separator. <lang ecmascript>import "./str" for Char import "/fmt" for Fmt

var toCamel = Fn.new { |snake|

   snake = snake.trim()
   var camel = ""
   var underscore = false
   for (c in snake) {
       if ("_- ".contains(c)) {
           underscore = true
       } else if (underscore) {
           camel = camel + Char.upper(c)
           underscore = false
       } else {
           camel = camel + c
       }
   }
   return camel

}

var toSnake = Fn.new { |camel|

   camel = camel.trim().replace(" ", "_") // we don't want any spaces in the result
   var snake = ""
   var first = true
   for (c in camel) {
       if (first) {
           snake = snake + c
           first = false
       } else if (!first && Char.isUpper(c)) {
           if (snake[-1] == "_" || snake[-1] == "-") {
               snake = snake + Char.lower(c)
           } else {
               snake = snake + "_" + Char.lower(c)
           }
       } else {
           snake = snake + c
       }
   }
   return snake

}

var tests = [

   "snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis",
   "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", "  spaces  "

]

System.print(" === to_snake_case ===") for (camel in tests) {

   Fmt.print("$33s -> $s", camel, toSnake.call(camel))

}

System.print("\n === toCamelCase ===") for (snake in tests) {

   Fmt.print("$33s -> $s", snake, toCamel.call(snake))

}</lang>

Output:
                          === to_snake_case ===
                        snakeCase -> snake_case
                       snake_case -> snake_case
                 variable_10_case -> variable_10_case
                   variable10Case -> variable10_case
                     ɛrgo rE tHis -> ɛrgo_r_e_t_his
                    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 -> ɛrgoRETHis
                    hurry-up-joe! -> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc -> c://myDocs/happyFlagDay/12.doc
                         spaces   -> spaces