Camel case and snake case: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 14:
:*Show the results on changing to both snake case and camel case for each of the following strings:
 
<syntaxhighlight lang="java">
"snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis",
"hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces "
Line 22:
 
* &nbsp; [[Naming_conventions]]
 
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F snakeToCamelCase(nam, sep = ‘[_]+’, lcmiddle = 0B)
‘ convert snake '_' separator case to camel case ’
I nam == ‘’
Line 267 ⟶ 264:
 
</pre>
 
=={{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...
<syntaxhighlight lang="algol68">BEGIN # convert camel case to and from snake case #
# returns c converted to uppercase if it is lowercase, c otherwise #
OP TOUPPER = ( CHAR c )STRING:
Line 414 ⟶ 410:
spaces -> spaces
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">camelToSnake: function [str][
i: new 0
result: new ""
Line 469 ⟶ 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
spaces => camel: spaces snake: spaces</pre>
 
=={{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.
{{works with|Factor|0.99 2021-06-02}}
<syntaxhighlight lang="factor">USING: formatting kernel math regexp sequences splitting
splitting.extras unicode ;
 
Line 527 ⟶ 521:
" internal space " >camel " internalSpace "
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
Line 534 ⟶ 527:
 
'''Preliminaries'''
<syntaxhighlight lang="jq">def isUpper: explode[0] | 65 <= . and . <= 90;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
Line 541 ⟶ 534:
def trim: sub("^\\s+";"") | sub("\\s+$";"");</syntaxhighlight>
'''Camel and Snake'''
<syntaxhighlight lang="jq">def toCamel:
trim as $snake
| { camel: "", underscore : false}
Line 599 ⟶ 592:
spaces -> spaces
</pre>
 
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">#=
Regex based variable name convention change string functions.
`sep` is the separator targeted for change from (to camel case) or to (to snake case)
Line 853 ⟶ 844:
spaces => spaces
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang=Scheme"scheme">
{def snake2camel
{lambda {:w}
Line 886 ⟶ 876:
-> snake_case my_brave_new_world ... and_so_on
</syntaxhighlight>
 
=={{header|Lua}}==
 
Line 895 ⟶ 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.
 
<syntaxhighlight lang=Lua"lua">local function escapeForPattern(str)
return (str:gsub("[-+*^?$.%%()[%]]", "%%%0"))
end
Line 924 ⟶ 913:
'''Tests:'''
 
<syntaxhighlight lang=Lua"lua">local function utf8Length(str)
local len = 0
for char in str:gmatch"[\1-\127\194-\244][\128-\191]*" do
Line 1,008 ⟶ 997:
* 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).
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Camel_case_and_snake_case
Line 1,065 ⟶ 1,053:
spaces -> spaces
</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<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>
Line 1,122 ⟶ 1,109:
spaces ===> spaces
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">""" https://rosettacode.org/wiki/Camel_case_and_snake_case """
 
import re
Line 1,401 ⟶ 1,387:
spaces => spaces
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang=Quackery"quackery"> [ $ "_ -" find 3 < ] is separator ( c --> b )
 
[ dup lower != ] is capital ( c --> b )
Line 1,465 ⟶ 1,450:
 
Stack empty.</pre>
 
=={{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...) ''
 
<syntaxhighlight lang="raku" line>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 "
Line 1,532 ⟶ 1,516:
c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc
spaces ==> spaces</pre>
 
=={{header|Vlang}}==
{{trans|go}}
<syntaxhighlight lang="vlang">fn to_camel(snake string) string {
mut camel := ''
mut underscore := false
Line 1,585 ⟶ 1,568:
{{out}}
<pre>Same as Go entry</pre>
 
=={{header|Wren}}==
{{libheader|Wren-str}}
Line 1,594 ⟶ 1,576:
 
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
import "/fmt" for Fmt
 
Line 1,672 ⟶ 1,654:
spaces -> spaces
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">string 0; \use zero-terminated strings
char Out(100); \output string (made global for safety)
 
10,327

edits