Skip to content
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

Add function for capitalizing strings #1375

Merged
merged 1 commit into from
Oct 25, 2022
Merged

Add function for capitalizing strings #1375

merged 1 commit into from
Oct 25, 2022

Conversation

femnad
Copy link
Contributor

@femnad femnad commented Oct 24, 2022

Hello,

This PR adds a capitalize function (name inspired by the Python string method) for converting a string to sentence case; first letter upper case, rest lower case.

I have a use case where a function like this would be useful and it's a small enough change so I'm creating this PR without a discussion issue but it's not a big loss if it doesn't get merged.

Thanks!

src/function.rs Outdated
Comment on lines 84 to 89
let mut chars = s.chars();
match chars.next() {
None => Ok(String::new()),
Some(c) => Ok(c.to_uppercase().collect::<String>() +
&chars.as_str().to_lowercase()),
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this:

Suggested change
let mut chars = s.chars();
match chars.next() {
None => Ok(String::new()),
Some(c) => Ok(c.to_uppercase().collect::<String>() +
&chars.as_str().to_lowercase()),
}
Ok(s.chars().enumerate().flat_map(|(i, c)| if i == 0 { c.upper_case() } else { c.lower_case() }).collect())

Copy link
Contributor Author

@femnad femnad Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So much more elegant! Applied with rustfmt and some modifications.

@casey
Copy link
Owner

casey commented Oct 25, 2022

Good feature, and thanks for also adding a test! Very minor comment suggesting a slightly tweaked implementation.

@casey
Copy link
Owner

casey commented Oct 25, 2022

Also, could you add a note, like this to the function documentation: <sup>master</sup>. This is to indicate that the function is on master and not in a released version of just yet. I'll change it to the version it winds up being released in.

@femnad
Copy link
Contributor Author

femnad commented Oct 25, 2022

Added master designation to the function documentation:

image

Function `capitalize` converts first character of given string to
uppercase and the rest to lowercase.
@casey casey merged commit aaef61b into casey:master Oct 25, 2022
@casey
Copy link
Owner

casey commented Oct 25, 2022

Merged! Thank you for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants