-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selection of boundaries for the default implementation of is_case
#21
Comments
This commit fixes an issue with `convert_case` crate reported in [1]. The issue is that the default implementation of `is_case` provided by that crate initializes the converter with almost all available boundaries. As a result the following conversion takes place: "ERR1".to_case(Case::UpperSnake) -> "ERR_1" Furthermore, the following check fails, while it seems like it shouldn't because the string is in capital letters. "ERR1".is_case(Case::UpperSnake) This commit introduces a new primitive `convert_case` which receives two case parameters, one for the source case and one of the target case. This way the converter is initialized with the correct source case boundaries. And it allows implementation of a new `is_case` primitive that builds on `convert_case` to select the source case boundaries correctly. [1] rutrum/convert-case#21
This commit fixes an issue with `convert_case` crate reported in [1]. The issue is that the default implementation of `is_case` provided by that crate initializes the converter with almost all available boundaries. As a result the following conversion takes place: "ERR1".to_case(Case::UpperSnake) -> "ERR_1" Furthermore, the following check fails, while it seems like it shouldn't because the string is in capital letters. "ERR1".is_case(Case::UpperSnake) This commit introduces a new primitive `convert_case` which receives two case parameters, one for the source case and one of the target case. This way the converter is initialized with the correct source case boundaries. And it allows implementation of a new `is_case` primitive that builds on `convert_case` to select the source case boundaries correctly. [1] rutrum/convert-case#21
Hi @r-bk . Thanks for reaching out. I'm glad you like the crate. I agree with you, I think this is a non-intuitive result. I thought your suggestion made sense, but it failed some tests. Take the following:
Only splitting based on underscore in this case would be counter to the intent. We try generously to split on all boundaries, so we return false when we join back with the case-specific delimiter. I think the unexpected part of this code is that UpperDigit boundary is being used by default. Digits are still an ongoing problem I'm trying to formalize (see http://stringcase.org, site WIP). Depending on context, they are sometimes boundaries, sometimes not. An example brought to my attention from another use was "2d_transformation". This is case of digit first, then letter. In this case, I wouldn't want to split here. But for "Transformation2D" I might split on "n2". But then, the unlikely scenario of "TRANSFORMATION2D"...this doesn't feel common, and I don't even think it's obvious that you'd want to split on UpperDigit all the time. I think I'll remove UpperDigit from the list of defaults. Do you think that makes sense? Should another digit boundary be removed from the defaults? |
Hi @rutrum ! There are no easy choices :) So I am thinking loud here. Intuitively removing Specifically to fix the |
I think you are correct, this isn't an issue of default boundaries (that might be a separate issue, however.) It's really that the mechanism of performing the transformation isn't good enough. This function would actually require manual checks. I agree with your outline. The As I get back into this project, I will put this item on the todo list of improvements to make. I don't know if it will make it to 0.7, but this is clearly something that should be fixed. Thanks for bringing it to my attention. |
@rutrum Hi!
Firstly, thanks for such a useful crate!
I have found the following example which IMO has a not intuitive result.
This test fails, even though
UpperSnake
case has only a single boundary_
and the string is in capital letters.This happens because the implementation of
is_case
initializes theStateConverter
withBoundary::defaults()
which includesUpperDigit
boundary.Is this by design, or a bug?
If a bug, would the correct implementation of
is_case
be something like this?The text was updated successfully, but these errors were encountered: