Camel case and snake case: Difference between revisions

Content added Content deleted
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Added Racket solution)
Line 1,450: Line 1,450:


Stack empty.</pre>
Stack empty.</pre>

=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket

(define input '("snakeCase" "snake_case" "variable_10_case" "variable10Case" "ɛrgo rE tHis" "hurry-up-joe!" "c://my-docs/happy_Flag-Day/12.doc" " spaces "))

;; make '-' the canonical separator by replacing '_' and ' ' with '-'
(define (dashify s)
(regexp-replace* #px"[_ ]" s "-"))

;; replace -X with -x for any upper-case X
(define (dash-upper->dash-lower s)
(regexp-replace* #px"-[[:upper:]]" s string-downcase))

;; replace X with -x for any upper-case X
(define (upper->dash-lower s)
(regexp-replace* #px"[[:upper:]]"
s
(λ (s) (string-append "-" (string-downcase s)))))

(define (string-kebabcase s)
(upper->dash-lower (dash-upper->dash-lower (dashify s))))

(define (string-snakecase s)
;; once we have kebabcase, snakecase is easy, just change '-' to '_'
(regexp-replace* #px"-" (string-kebabcase s) "_"))

(define (string-camelcase s)
;; camel is pretty easy, too - replace dash-anything with uppercase-anything
;; note: this will change non-letters as well, so -10 becomes just 10
(regexp-replace* #px"-." (string-kebabcase s) (λ (s) (string-upcase (substring s 1 2)))))

(define (convert-case to-case case-name namelist)
(printf "Conversions to ~a:~n" case-name)
(for ([name namelist])
(printf "'~a' --> '~a'~n" name (to-case (string-trim name))))
(printf "~n"))

(convert-case string-kebabcase "kebab-case" input)
(convert-case string-snakecase "snake_case" input)
(convert-case string-camelcase "camelCase" input)
</syntaxhighlight>
{{out}}
<pre>
Conversions to kebab-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'

Conversions 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'

Conversions 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|Raku}}==
=={{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...) ''
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...) ''