Camel case and snake case: Difference between revisions

Added Easylang
m (→‎{{header|Wren}}: Minor tidy)
(Added Easylang)
 
(3 intermediate revisions by 2 users not shown)
Line 680:
 
 
 
=={{header|EasyLang}}==
{{trans|Nim}}
<syntaxhighlight>
func$ strip s$ .
a = 1
while substr s$ a 1 = " "
a += 1
.
b = len s$
while substr s$ b 1 = " "
b -= 1
.
return substr s$ a (b - a + 1)
.
func$ toupper c$ .
c = strcode c$
if c >= 97 and c <= 122
c$ = strchar (c - 32)
.
return c$
.
func$ tolower c$ .
c = strcode c$
if c >= 65 and c <= 90
c$ = strchar (c + 32)
.
return c$
.
func isupper c$ .
c = strcode c$
if c >= 65 and c <= 90
return 1
.
.
delim$ = "_- "
func$ snakecase s$ .
s$ = strip s$
for c$ in strchars s$
if isupper c$ = 1 and prev$ <> ""
if strpos delim$ prev$ = 0
r$ &= "_"
.
r$ &= tolower c$
else
r$ &= c$
.
prev$ = c$
.
return r$
.
func$ camelcase s$ .
s$ = strip s$
prev$ = "x"
for c$ in strchars s$
if strpos delim$ prev$ <> 0
r$ &= toupper c$
elif strpos delim$ c$ = 0
r$ &= c$
.
prev$ = c$
.
return r$
.
test$[] = [ "snakeCase" "snake_case" "variable_10_case" "variable10Case" "ɛrgo rE tHis" "hurry-up-joe!" "c://my-docs/happy_Flag-Day/12.doc" " spaces " ]
print "=== To snake_case ==="
for s$ in test$[]
print s$ & " -> " & snakecase s$
.
print "\n=== To camelCase ==="
for s$ in test$[]
print s$ & " -> " & camelcase s$
.
</syntaxhighlight>
{{out}}
<pre>
=== 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
 
=== To camelCase ===
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
</pre>
 
=={{header|Factor}}==
Line 1,284 ⟶ 1,380:
spaces => spaces
</pre>
 
=={{header|Kotlin}}==
{{trans|Scala}}
<syntaxhighlight lang="Kotlin">
fun main() {
val variableNames = listOf("snakeCase", "snake_case", "variable_10_case", "variable10Case",
"ergo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces ")
 
println(" ".repeat(26) + "=== To snake_case ===")
variableNames.forEach { text ->
println("${text.padStart(34)} --> ${toSnakeCase(text)}")
}
 
println("\n" + " ".repeat(26) + "=== To camelCase ===")
variableNames.forEach { text ->
println("${text.padStart(34)} --> ${toCamelCase(text)}")
}
}
 
fun toSnakeCase(camel: String): String {
val snake = StringBuilder()
camel.trim().replace(" ", "_").replace("-", "_").forEach { ch ->
if (snake.isEmpty() || snake.last() != '_' || ch != '_') {
if (ch.isUpperCase() && snake.isNotEmpty() && snake.last() != '_') snake.append('_')
snake.append(ch.toLowerCase())
}
}
return snake.toString()
}
 
fun toCamelCase(snake: String): String {
val camel = StringBuilder()
var underscore = false
snake.trim().replace(" ", "_").replace("-", "_").forEach { ch ->
if (ch == '_') {
underscore = true
} else if (underscore) {
camel.append(ch.toUpperCase())
underscore = false
} else {
camel.append(ch)
}
}
return camel.toString()
}
</syntaxhighlight>
{{out}}
<pre>
=== To snake_case ===
snakeCase --> snake_case
snake_case --> snake_case
variable_10_case --> variable_10_case
variable10Case --> variable10_case
ergo rE tHis --> ergo_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
 
=== To camelCase ===
snakeCase --> snakeCase
snake_case --> snakeCase
variable_10_case --> variable10Case
variable10Case --> variable10Case
ergo rE tHis --> ergoRETHis
hurry-up-joe! --> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc --> c://myDocs/happyFlagDay/12.doc
spaces --> spaces
 
</pre>
 
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
Line 2,099 ⟶ 2,266:
c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc
spaces ==> spaces</pre>
 
 
=={{header|RPL}}==
« 1
'''WHILE''' DUP2 DUP SUB " " == '''REPEAT''' 1 + '''END'''
OVER DUP SIZE
'''WHILE''' DUP2 DUP SUB " " == '''REPEAT''' 1 - '''END'''
SWAP DROP SUB
» '<span style="color:blue">TRIM</span>' STO
« <span style="color:blue">TRIM</span> → s
« ""
1 s SIZE '''FOR''' j
s j DUP SUB
'''CASE'''
"- " OVER POS '''THEN'''
DROP "_" '''END'''
DUP "A" ≥ OVER "Z" ≤ AND '''THEN'''
NUM 32 + CHR
'''IF''' OVER DUP SIZE DUP SUB "_" ≠ '''THEN''' "_" SWAP + '''END'''
'''END'''
'''END'''
+
'''NEXT'''
» » '<span style="color:blue">→SNAKE</span>' STO
« <span style="color:blue">TRIM</span> → s
« "" 1 CF
1 s SIZE '''FOR''' j
s j DUP SUB
'''CASE'''
"-_ " OVER POS '''THEN'''
DROP "" 1 SF '''END'''
DUP "a" ≥ OVER "z" ≤ AND 1 FS?C AND '''THEN'''
NUM 32 - CHR '''END'''
'''END'''
+
'''NEXT'''
» » '<span style="color:blue">→CAMEL</span>' STO
« { "snakeCase" "snake_case" "variable_10_case" "variable10Case"
"εrgo rE tHis" "hurry-up-joe!" "c://my-docs/happy_Flag-Day/12.doc" " spaces " }
DUP 1 « <span style="color:blue">→SNAKE</span> » DOLIST
SWAP 1 « <span style="color:blue">→CAMEL</span> » DOLIST
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: { "snake_case" "snake_case" "variable_10_case" "variable10_case" "εrgo_r_e_t_his" "hurry_up_joe!" "c://my_docs/happy_flag_day/12.doc" "spaces" }
1: { "snakeCase" "snakeCase" "variable10Case" "variable10Case" "εrgoRETHis" "hurryUpJoe!" "c://myDocs/happyFlagDay/12.doc" "spaces" }
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object CamelCaseAndSnakeCase extends App {
 
val variableNames = List("snakeCase", "snake_case", "variable_10_case", "variable10Case",
"ergo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces ")
 
println(" " * 26 + "=== To snake_case ===")
variableNames.foreach { text =>
println(f"$text%34s --> ${toSnakeCase(text)}")
}
 
println("\n" + " " * 26 + "=== To camelCase ===")
variableNames.foreach { text =>
println(f"$text%34s --> ${toCamelCase(text)}")
}
 
def toSnakeCase(camel: String): String = {
val snake = new StringBuilder
camel.trim.replace(" ", "_").replace("-", "_").foreach { ch =>
if (snake.isEmpty || snake.last != '_' || ch != '_') {
if (ch.isUpper && snake.nonEmpty && snake.last != '_') snake.append('_')
snake.append(ch.toLower)
}
}
snake.toString
}
 
def toCamelCase(snake: String): String = {
val camel = new StringBuilder
var underscore = false
snake.trim.replace(" ", "_").replace("-", "_").foreach { ch =>
if (ch == '_') underscore = true
else if (underscore) {
camel.append(ch.toUpper)
underscore = false
} else camel.append(ch)
}
camel.toString
}
}
</syntaxhighlight>
{{out}}
<pre>
=== To snake_case ===
snakeCase --> snake_case
snake_case --> snake_case
variable_10_case --> variable_10_case
variable10Case --> variable10_case
ergo rE tHis --> ergo_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
 
=== To camelCase ===
snakeCase --> snakeCase
snake_case --> snakeCase
variable_10_case --> variable10Case
variable10Case --> variable10Case
ergo rE tHis --> ergoRETHis
hurry-up-joe! --> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc --> c://myDocs/happyFlagDay/12.doc
spaces --> spaces
 
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
1,969

edits