Camel case and snake case: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
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:


<syntaxhighlight 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 "
Line 22: Line 22:


* &nbsp; [[Naming_conventions]]
* &nbsp; [[Naming_conventions]]



=={{header|11l}}==
=={{header|11l}}==
{{trans|Python}}
{{trans|Python}}


<syntaxhighlight 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 267: Line 264:


</pre>
</pre>

=={{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...
<syntaxhighlight 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 414: Line 410:
spaces -> spaces
spaces -> spaces
</pre>
</pre>

=={{header|Arturo}}==
=={{header|Arturo}}==


<syntaxhighlight lang=rebol>camelToSnake: function [str][
<syntaxhighlight lang="rebol">camelToSnake: function [str][
i: new 0
i: new 0
result: new ""
result: new ""
Line 469: Line 464:
c://my-docs/happy_Flag-Day/12.doc => camel: c://my-docs/happyFlag-Day/12.doc snake: c://my-docs/happy__flag-_day/12.doc
c://my-docs/happy_Flag-Day/12.doc => camel: c://my-docs/happyFlag-Day/12.doc snake: c://my-docs/happy__flag-_day/12.doc
spaces => camel: spaces snake: spaces</pre>
spaces => camel: spaces snake: spaces</pre>

=={{header|Factor}}==
=={{header|Factor}}==
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}}
<syntaxhighlight 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 527: Line 521:
" internal space " >camel " internalSpace "
" internal space " >camel " internalSpace "
</pre>
</pre>

=={{header|jq}}==
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
'''Adapted from [[#Wren|Wren]]'''
Line 534: Line 527:


'''Preliminaries'''
'''Preliminaries'''
<syntaxhighlight 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] + .;
Line 541: Line 534:
def trim: sub("^\\s+";"") | sub("\\s+$";"");</syntaxhighlight>
def trim: sub("^\\s+";"") | sub("\\s+$";"");</syntaxhighlight>
'''Camel and Snake'''
'''Camel and Snake'''
<syntaxhighlight lang=jq>def toCamel:
<syntaxhighlight lang="jq">def toCamel:
trim as $snake
trim as $snake
| { camel: "", underscore : false}
| { camel: "", underscore : false}
Line 599: Line 592:
spaces -> spaces
spaces -> spaces
</pre>
</pre>


=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight 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 853: Line 844:
spaces => spaces
spaces => spaces
</pre>
</pre>

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

=={{header|Lua}}==
=={{header|Lua}}==


Line 895: Line 884:
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.


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


<syntaxhighlight 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 1,008: Line 997:
* We don't handle consecutive upper case characters (e.g. "newUIBox").
* We don't handle consecutive upper case characters (e.g. "newUIBox").
* We don't handle non-ASCII characters because of Lua's limited support for them ("ɛ" just happen to work here).
* We don't handle non-ASCII characters because of Lua's limited support for them ("ɛ" just happen to work here).

=={{header|Perl}}==
=={{header|Perl}}==
<syntaxhighlight 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,065: Line 1,053:
spaces -> spaces
spaces -> spaces
</pre>
</pre>

=={{header|Phix}}==
=={{header|Phix}}==
<!--<syntaxhighlight 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,122: Line 1,109:
spaces ===> spaces
spaces ===> spaces
</pre>
</pre>

=={{header|Python}}==
=={{header|Python}}==
<syntaxhighlight 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,401: Line 1,387:
spaces => spaces
spaces => spaces
</pre>
</pre>

=={{header|Quackery}}==
=={{header|Quackery}}==


<syntaxhighlight 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,465: Line 1,450:


Stack empty.</pre>
Stack empty.</pre>

=={{header|Raku}}==
=={{header|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...) ''
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...) ''


<syntaxhighlight lang=raku line>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,532: Line 1,516:
c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc
c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc
spaces ==> spaces</pre>
spaces ==> spaces</pre>

=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|go}}
{{trans|go}}
<syntaxhighlight 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,585: Line 1,568:
{{out}}
{{out}}
<pre>Same as Go entry</pre>
<pre>Same as Go entry</pre>

=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-str}}
{{libheader|Wren-str}}
Line 1,594: Line 1,576:


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.
<syntaxhighlight lang=ecmascript>import "./str" for Char
<syntaxhighlight lang="ecmascript">import "./str" for Char
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 1,672: Line 1,654:
spaces -> spaces
spaces -> spaces
</pre>
</pre>

=={{header|XPL0}}==
=={{header|XPL0}}==
<syntaxhighlight 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)