Camel case and snake case: Difference between revisions
Content added Content deleted
(Python example) |
|||
Line 229: | Line 229: | ||
" internal space " >camel " internalSpace " |
" internal space " >camel " internalSpace " |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
|||
'''Adapted from [[#Wren|Wren]]''' |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
'''Preliminaries''' |
|||
<lang jq>def isUpper: explode[0] | 65 <= . and . <= 90; |
|||
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." |
|||
def trim: sub("^\\s+";"") | sub("\\s+$";"");</lang> |
|||
'''Camel and Snake''' |
|||
<lang jq>def toCamel: |
|||
trim as $snake |
|||
| { camel: "", underscore : false} |
|||
| reduce ($snake|explode[]|[.]|implode) as $c (.; |
|||
if ["_", "-", " "]|index($c) |
|||
then .underscore = true |
|||
elif .underscore |
|||
then .camel += ($c|ascii_upcase) |
|||
| .underscore = false |
|||
else .camel += $c |
|||
end) |
|||
| .camel; |
|||
def toSnake: |
|||
(trim | gsub("\\s"; "_")) as $camel |
|||
| reduce ($camel|explode[1:][]|[.]|implode) as $c ( |
|||
$camel[0:1]; |
|||
if $c|isUpper |
|||
then ($c|ascii_downcase) as $lc |
|||
| if (.[-1:] | (. == "_" or . == "-")) |
|||
then . + $lc |
|||
else . + "_" + $lc |
|||
end |
|||
else . + $c |
|||
end ); |
|||
def tests: [ |
|||
"snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis", |
|||
"hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces " |
|||
]; |
|||
" === to_snake_case ===", |
|||
(tests[] | "\(lpad(33)) -> \(toSnake)"), |
|||
"", |
|||
" === toCamelCase ===", |
|||
(tests[] | "\(lpad(33)) -> \(toCamel)")</lang> |
|||
{{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 |
|||
=== 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 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 482: | Line 555: | ||
spaces => spaces |
spaces => spaces |
||
</pre> |
</pre> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |