camel-snake-kebab icon indicating copy to clipboard operation
camel-snake-kebab copied to clipboard

(->snake_case :s3-key) ; => :s_3_key

Open rsslldnphy opened this issue 10 years ago • 10 comments

Is there any way to avoid numbers creating separators? Had a quick scan of the code but couldn't see an obvious workaround. Thanks.

rsslldnphy avatar Nov 19 '14 11:11 rsslldnphy

This is quite tricky to solve correctly in the general case without first classifying the case of the input string. I've thought about it before but will look into it again this weekend. If you want to take a stab at it yourself, take a look at this expression: https://github.com/qerub/camel-snake-kebab/blob/master/src/camel_snake_kebab/internals/misc.cljx#L29

qerub avatar Nov 19 '14 21:11 qerub

Yes, it does seem like quite a tricky problem. The only thing I could think of was never adding split points when going from snake case to kebab case and vice versa, but this would mean conversions to and from camelcase would no longer be reversible, which doesn't sound ideal. Will have a ponder too.

rsslldnphy avatar Nov 20 '14 09:11 rsslldnphy

The way I solved this in lettercase was to optionally allow the user to pass in their own separator regex. In your case, it would be (lower-underscore :s3-key #"-").

camel-snake-kebab has diverged a bunch from when we were similar in order to support CLJS, so enabling that is a bit tricky. I think one way you could do this is to enable you to pass the separators to classify-char and pass it all the way down from the top-level functions with varargs. Something like (->snake_case :s3-key :number []), which would prevent the split between s and 3.

Since you can't use a regex, perhaps better would be to name the split rules (ex. :whitespace or :number-after-letter) and then allow the user to omit rules on call.

ToBeReplaced avatar Nov 21 '14 18:11 ToBeReplaced

The way I solved this in lettercase was to optionally allow the user to pass in their own separator regex. […]

Yeah, this makes sense and can be viewed as a sort of manual classification of the input case. I'll give this approach a try in this shape:

(->snake_case ident :splitter \-)

(->CamelCase ident :splitter #"\s+")

An alternative I can think of is automatic classification based on the presence of certain chars (i.e. presence of \- will make split split on \-), but that gets very messy as soon as you put strings with multiple separator chars into the picture.

Another alternative would be to let the user state the expected input type (e.g. (->kebab-case ident :from snake_case) or (->kebab-case (<-snake_case ident)) which would also allow good input validation…

Since you can't use a regex […]

I can use regexes for simple char splits, just not for case shifts.

qerub avatar Nov 22 '14 15:11 qerub

I've created pull request #23 implementing the previously mentioned approach. Feedback is more than welcome.

qerub avatar Jan 10 '15 17:01 qerub

The just released version 0.3.0 allows you to do (->snake_case :s3-key :separator \-).

I'm leaving this issue open because I want a better solution.

qerub avatar Jan 17 '15 19:01 qerub

There's a similar annoying problem with kebab-case"

  (csk/->kebab-case :s3_bucket_name)
;; >  :s-3-bucket-name

kenrestivo-stem avatar Aug 21 '17 20:08 kenrestivo-stem

Is there any update on this issue? I mean it's been hanging here since 2014 😱

agzam avatar Jul 08 '19 21:07 agzam

Yeah, I'd also very much like a way to disable letters followed by numbers being separated.

vincent-dm avatar Mar 07 '22 00:03 vincent-dm

A reminder that snake -> kebabs transforms containing non-leading numbers are not reversible:

(-> "a1_example"
    ->kebab-case-keyword
    ->snake_case_string)
"a_1_example"

(-> "a2a_example"
    ->kebab-case-keyword
    ->snake_case_string)
"a_2a_example"

waffletower avatar Jun 28 '23 21:06 waffletower