Camel case and snake case: Difference between revisions
Content added Content deleted
(Added Lua.) |
Drkameleon (talk | contribs) (Added Arturo implementation) |
||
Line 414: | Line 414: | ||
spaces -> spaces |
spaces -> spaces |
||
</pre> |
</pre> |
||
=={{header|Arturo}}== |
|||
<lang rebol>camelToSnake: function [str][ |
|||
i: new 0 |
|||
result: new "" |
|||
while [i < size str][ |
|||
ch: str\[i] |
|||
if? upper? ch [ |
|||
'result ++ `_` ++ lower str\[i] |
|||
] |
|||
else [ |
|||
'result ++ str\[i] |
|||
] |
|||
inc 'i |
|||
] |
|||
return result |
|||
] |
|||
snakeToCamel: function [str][ |
|||
i: new 0 |
|||
result: new "" |
|||
while [i < size str][ |
|||
ch: str\[i] |
|||
if? and? [ch=`_`][(i+1) < size str] [ |
|||
'result ++ upper str\[i+1] |
|||
inc 'i |
|||
] |
|||
else [ |
|||
'result ++ str\[i] |
|||
] |
|||
inc 'i |
|||
] |
|||
return result |
|||
] |
|||
tests: ["snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis", |
|||
"hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces "] |
|||
loop tests 'test [ |
|||
print [pad test 35 "=> camel:" snakeToCamel test "snake:" camelToSnake test] |
|||
]</lang> |
|||
{{out}} |
|||
<pre> snakeCase => camel: snakeCase snake: snake_case |
|||
snake_case => camel: snakeCase snake: snake_case |
|||
variable_10_case => camel: variable10Case snake: variable_10_case |
|||
variable10Case => camel: variable10Case snake: variable10_case |
|||
ɛrgo rE tHis => camel: ɛrgo rE tHis snake: ɛrgo r_e t_his |
|||
hurry-up-joe! => camel: hurry-up-joe! snake: hurry-up-joe! |
|||
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}}== |
=={{header|Factor}}== |