Camel case and snake case: Difference between revisions

Content added Content deleted
(adding lambdatalk)
m (syntax highlighting fixup automation)
Line 14: Line 14:
:*Show the results on changing to both snake case and camel case for each of the following strings:
:*Show the results on changing to both snake case and camel case for each of the following strings:


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


;Related tasks:
;Related tasks:
Line 28: Line 28:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F snakeToCamelCase(nam, sep = ‘[_]+’, lcmiddle = 0B)
<syntaxhighlight lang=11l>F snakeToCamelCase(nam, sep = ‘[_]+’, lcmiddle = 0B)
‘ convert snake '_' separator case to camel case ’
‘ convert snake '_' separator case to camel case ’
I nam == ‘’
I nam == ‘’
Line 80: Line 80:
‘ spaces ’]
‘ spaces ’]
print(teststring.rjust(36)‘ => ’f(teststring))
print(teststring.rjust(36)‘ => ’f(teststring))
print()</lang>
print()</syntaxhighlight>


{{out}}
{{out}}
Line 270: Line 270:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Treating space, - and _ as equivalent "break" characters (as in most of the other samples) and adding kebab case (as in the Raku sample) and resisting the urge to add "space case" for languages like Algol 68 where (insignificant) spaces can appear in identifiers...
Treating space, - and _ as equivalent "break" characters (as in most of the other samples) and adding kebab case (as in the Raku sample) and resisting the urge to add "space case" for languages like Algol 68 where (insignificant) spaces can appear in identifiers...
<lang algol68>BEGIN # convert camel case to and from snake case #
<syntaxhighlight lang=algol68>BEGIN # convert camel case to and from snake case #
# returns c converted to uppercase if it is lowercase, c otherwise #
# returns c converted to uppercase if it is lowercase, c otherwise #
OP TOUPPER = ( CHAR c )STRING:
OP TOUPPER = ( CHAR c )STRING:
Line 383: Line 383:
print( ( 40 PAD identifier[ i ], " -> ", CAMELTOKEBAB identifier[ i ], newline ) )
print( ( 40 PAD identifier[ i ], " -> ", CAMELTOKEBAB identifier[ i ], newline ) )
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 417: Line 417:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>camelToSnake: function [str][
<syntaxhighlight lang=rebol>camelToSnake: function [str][
i: new 0
i: new 0
result: new ""
result: new ""
Line 457: Line 457:
loop tests 'test [
loop tests 'test [
print [pad test 35 "=> camel:" snakeToCamel test "snake:" camelToSnake test]
print [pad test 35 "=> camel:" snakeToCamel test "snake:" camelToSnake test]
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 473: Line 473:
In my interpretation of the task, leading/trailing whitespace should be ignored, not trimmed. And non-leading/trailing whitespace should be dealt with the same way as underscores and hyphens. Although the task says nothing about numbers, I chose to treat letter->number and number->letter transitions the same way as lower->upper for the sake of converting to snake case.
In my interpretation of the task, leading/trailing whitespace should be ignored, not trimmed. And non-leading/trailing whitespace should be dealt with the same way as underscores and hyphens. Although the task says nothing about numbers, I chose to treat letter->number and number->letter transitions the same way as lower->upper for the sake of converting to snake case.
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: formatting kernel math regexp sequences splitting
<syntaxhighlight lang=factor>USING: formatting kernel math regexp sequences splitting
splitting.extras unicode ;
splitting.extras unicode ;


Line 505: Line 505:
"c://my-docs/happy_Flag-Day/12.doc" " spaces "
"c://my-docs/happy_Flag-Day/12.doc" " spaces "
" internal space "
" internal space "
} [ test ] each</lang>
} [ test ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 534: Line 534:


'''Preliminaries'''
'''Preliminaries'''
<lang jq>def isUpper: explode[0] | 65 <= . and . <= 90;
<syntaxhighlight lang=jq>def isUpper: explode[0] | 65 <= . and . <= 90;


def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;


# "White space is not permitted as part of camel case or snake case variable names."
# "White space is not permitted as part of camel case or snake case variable names."
def trim: sub("^\\s+";"") | sub("\\s+$";"");</lang>
def trim: sub("^\\s+";"") | sub("\\s+$";"");</syntaxhighlight>
'''Camel and Snake'''
'''Camel and Snake'''
<lang jq>def toCamel:
<syntaxhighlight lang=jq>def toCamel:
trim as $snake
trim as $snake
| { camel: "", underscore : false}
| { camel: "", underscore : false}
Line 576: Line 576:
"",
"",
" === toCamelCase ===",
" === toCamelCase ===",
(tests[] | "\(lpad(33)) -> \(toCamel)")</lang>
(tests[] | "\(lpad(33)) -> \(toCamel)")</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 602: Line 602:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>#=
<syntaxhighlight lang=julia>#=
Regex based variable name convention change string functions.
Regex based variable name convention change string functions.
`sep` is the separator targeted for change from (to camel case) or to (to snake case)
`sep` is the separator targeted for change from (to camel case) or to (to snake case)
Line 669: Line 669:
println()
println()
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Testing function snakeToCamelCase:
Testing function snakeToCamelCase:
Line 855: Line 855:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang Scheme>
<syntaxhighlight lang=Scheme>
{def snake2camel
{def snake2camel
{lambda {:w}
{lambda {:w}
Line 885: Line 885:
}
}
-> snake_case my_brave_new_world ... and_so_on
-> snake_case my_brave_new_world ... and_so_on
</syntaxhighlight>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==
Line 895: Line 895:
We have the two functions toCamelCase() and fromCamelCase() that perform the conversion back and forth between camel case and non-camel case, including snake case. The test strings are assumed to potentially contain multiple names. Any character that isn't part of the matched names (including leading and trailing whitespace) is ignored/left alone. Numbers aren't mentioned in the task but the "variable_10_case"/"variable10Case" strings imply that numbers should be treated as their own words.
We have the two functions toCamelCase() and fromCamelCase() that perform the conversion back and forth between camel case and non-camel case, including snake case. The test strings are assumed to potentially contain multiple names. Any character that isn't part of the matched names (including leading and trailing whitespace) is ignored/left alone. Numbers aren't mentioned in the task but the "variable_10_case"/"variable10Case" strings imply that numbers should be treated as their own words.


<lang Lua>local function escapeForPattern(str)
<syntaxhighlight lang=Lua>local function escapeForPattern(str)
return (str:gsub("[-+*^?$.%%()[%]]", "%%%0"))
return (str:gsub("[-+*^?$.%%()[%]]", "%%%0"))
end
end
Line 920: Line 920:
)
)
end))
end))
end</lang>
end</syntaxhighlight>


'''Tests:'''
'''Tests:'''


<lang Lua>local function utf8Length(str)
<syntaxhighlight lang=Lua>local function utf8Length(str)
local len = 0
local len = 0
for char in str:gmatch"[\1-\127\194-\244][\128-\191]*" do
for char in str:gmatch"[\1-\127\194-\244][\128-\191]*" do
Line 953: Line 953:
convert("space case to camelCase", function(str) return toCamelCase (str, " ") end)
convert("space case to camelCase", function(str) return toCamelCase (str, " ") end)
convert("kebab-case to camelCase", function(str) return toCamelCase (str, "-") end)
convert("kebab-case to camelCase", function(str) return toCamelCase (str, "-") end)
convert("camelCase to snake_case", function(str) return fromCamelCase(str, "_") end)</lang>
convert("camelCase to snake_case", function(str) return fromCamelCase(str, "_") end)</syntaxhighlight>


{{out}}
{{out}}
Line 1,010: Line 1,010:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang=perl>#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Camel_case_and_snake_case
use strict; # https://rosettacode.org/wiki/Camel_case_and_snake_case
Line 1,040: Line 1,040:
{
{
printf "%35s -> %s\n", $word, tocamel($word);
printf "%35s -> %s\n", $word, tocamel($word);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,067: Line 1,067:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_snake_case</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_snake_case</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 1,099: Line 1,099:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" === to_snake_case ===\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_snake_case</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" === to_snake_case ===\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_snake_case</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"\n === toCamelCase ===\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">toCamelCase</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"\n === toCamelCase ===\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">toCamelCase</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,124: Line 1,124:


=={{header|Python}}==
=={{header|Python}}==
<lang python>""" https://rosettacode.org/wiki/Camel_case_and_snake_case """
<syntaxhighlight lang=python>""" https://rosettacode.org/wiki/Camel_case_and_snake_case """


import re
import re
Line 1,217: Line 1,217:
print(teststring.rjust(36), " => ", f(teststring))
print(teststring.rjust(36), " => ", f(teststring))
print()
print()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Testing function <function snakeToCamelCase at 0x000001F17C25AC10>:
Testing function <function snakeToCamelCase at 0x000001F17C25AC10>:
Line 1,404: Line 1,404:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ $ "_ -" find 3 < ] is separator ( c --> b )
<syntaxhighlight lang=Quackery> [ $ "_ -" find 3 < ] is separator ( c --> b )


[ dup lower != ] is capital ( c --> b )
[ dup lower != ] is capital ( c --> b )
Line 1,425: Line 1,425:
[ dip
[ dip
[ char _ join ] ]
[ char _ join ] ]
lower join ] ] is snakify ( $ --> $ )</lang>
lower join ] ] is snakify ( $ --> $ )</syntaxhighlight>


{{out}}
{{out}}
Line 1,469: Line 1,469:
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...) ''
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<
<syntaxhighlight lang=raku line>my @tests = qww<
snakeCase snake_case variable_10_case variable10Case "ɛrgo rE tHis"
snakeCase snake_case variable_10_case variable10Case "ɛrgo rE tHis"
hurry-up-joe! c://my-docs/happy_Flag-Day/12.doc " spaces "
hurry-up-joe! c://my-docs/happy_Flag-Day/12.doc " spaces "
Line 1,501: Line 1,501:
printf "%33s ==> %s\n", $_, .&toCamelCase for @tests;
printf "%33s ==> %s\n", $_, .&toCamelCase for @tests;
say "\n{' ' x 30}to-kabab-case";
say "\n{' ' x 30}to-kabab-case";
printf "%33s ==> %s\n", $_, .&to-kebab-case for @tests;</lang>
printf "%33s ==> %s\n", $_, .&to-kebab-case for @tests;</syntaxhighlight>
{{out}}
{{out}}
<pre> to_snake_case
<pre> to_snake_case
Line 1,535: Line 1,535:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|go}}
{{trans|go}}
<lang vlang>fn to_camel(snake string) string {
<syntaxhighlight lang=vlang>fn to_camel(snake string) string {
mut camel := ''
mut camel := ''
mut underscore := false
mut underscore := false
Line 1,582: Line 1,582:
println('${word:33} -> ${to_camel(word)}')
println('${word:33} -> ${to_camel(word)}')
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Same as Go entry</pre>
<pre>Same as Go entry</pre>
Line 1,594: Line 1,594:


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


Line 1,648: Line 1,648:
for (snake in tests) {
for (snake in tests) {
Fmt.print("$33s -> $s", snake, toCamel.call(snake))
Fmt.print("$33s -> $s", snake, toCamel.call(snake))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,674: Line 1,674:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>string 0; \use zero-terminated strings
<syntaxhighlight lang=XPL0>string 0; \use zero-terminated strings
char Out(100); \output string (made global for safety)
char Out(100); \output string (made global for safety)


Line 1,739: Line 1,739:
CrLf(0);
CrLf(0);
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}