Jump to content

Camel case and snake case: Difference between revisions

→‎{{header|Raku}}: Add a Raku example
mNo edit summary
(→‎{{header|Raku}}: Add a Raku example)
Line 22:
 
*   [[Naming_conventions]]
 
 
 
=={{header|Raku}}==
The specs are a little vague, but taking a wild stab at it...
 
<lang perl6>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 "
>;
 
 
sub to_snake_case (Str $s is copy) {
$s.=trim;
return $s if $s.contains: / \s | '/' /;
$s.=subst: / <after <:Ll>> (<:Lu>|<:digit>+) /, {'_' ~ $0.lc}, :g;
$s.=subst: / <after <:digit>> (<:Lu>) /, {'_' ~ $0.lc}, :g;
}
 
sub toCamelCase (Str $s is copy) {
$s.=trim;
return $s if $s.contains: / \s | '/' /;
$s.=subst: / ('_') (\w) /, {$1.uc}, :g;
}
 
sub to-kebab-case (Str $s is copy) {
$s.=trim;
return $s if $s.contains: / \s | '/' /;
$s.=subst: / ('_') (\w) /, {'-' ~ $1.lc}, :g;
$s.=subst: / <after <:Ll>> (<:Lu>|<:digit>+) /, {'-' ~ $0.lc}, :g;
$s.=subst: / <after <:digit>> (<:Lu>) /, {'-' ~ $0.lc}, :g;
}
 
say "{' ' x 32}to_snake_case";
printf "%35s ==> %-35s\n", $_, .&to_snake_case for @tests;
say "\n{' ' x 32}toCamelCase";
printf "%35s ==> %-35s\n", $_, .&toCamelCase for @tests;
say "\n{' ' x 32}to-kabab-case";
printf "%35s ==> %-35s\n", $_, .&to-kebab-case for @tests;</lang>
{{out}}
<pre> to_snake_case
snakeCase ==> snake_case
snake_case ==> snake_case
variable_10_case ==> variable_10_case
variable10Case ==> variable_10_case
ɛrgo rE tHis ==> ɛrgo rE tHis
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 ==> ɛrgo rE tHis
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-kabab-case
snakeCase ==> snake-case
snake_case ==> snake-case
variable_10_case ==> variable-10-case
variable10Case ==> variable-10-case
ɛrgo rE tHis ==> ɛrgo rE tHis
hurry-up-joe! ==> hurry-up-joe!
c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc
spaces ==> spaces</pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.