rutrum / convert-case Goto Github PK
View Code? Open in Web Editor NEWConverts to and from various cases.
License: MIT License
Converts to and from various cases.
License: MIT License
I compared this crate with ChatGPT's code. And ChatGPT's code was faster than this one.
Maybe we need to optimize the algorithm for converting a case.
Hi there, thanks for the crate!
Quick feature request: sentence case. Here, every character is lowercase except the first character which is uppercase.
use convert_case::{Case, Casing};
assert_eq!("My variable name", "My variable NAME".to_case(Case::Sentence));
This could easily be implemented by reusing the lower case functionality.
Since this is easy to create, this is a low priority feature request. Feel free to close this request if you think it's not worth adding it. Still I think it's more ergonomic to include it directly in the library.
ok, I hardly know rust at all
#[derive(Clone, Copy)]
pub struct ToJavaPackagePathHelper;
impl HelperDef for ToJavaPackagePathHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper,
_: &Handlebars,
_: &Context,
_rc: &mut RenderContext,
out: &mut dyn Output,
) -> HelperResult {
let param = h.param(0).ok_or(RenderError::new(
"this function requires an argument to process",
))?;
let rendered = param.value().render();
let conv = Converter::new()
.set_pattern(Pattern::Camel)
.remove_boundaries(&[
Boundary::UpperDigit,
Boundary::LowerDigit,
Boundary::DigitUpper,
Boundary::DigitLower,
])
.set_delim("/");
let path = &conv.convert(rendered).to_lowercase();
debug!("path: '{}'", path );
out.write(path)?;
Ok(())
}
}
the goal is (hopefully) to obviously convert a java package to a path. java packages (by convention) are all prefixed with a reverse domain. So my domain com.xenoterracide
input, should end up as com/xenoterracide
which will then be passed to mkdir
, this sadly doesn't seem to work. If I don't input a dot it usually seems to work as expected. Probably just going to do a .
replace, but seems like maybe a good feature (PathCase), but in general not the behavior I would expect from Pattern::Camel.
https://github.com/xenoterracide/brix/blob/main/crates/brix_processor/src/helpers/basic.rs#L119
I wanted to use this crate to convert some strings to snake_case, but the strings I wanted to convert need 2d or 3d to be together.
For example:
CollisionShape2D
should convert to collision_shape_2d
let persp = "ПЕРСПЕКТИВА24".to_case(Case::Title);
thread 'main' panicked at 'byte index 11 is not a char boundary; it is inside 'Е' (bytes 10..12) of ПЕРСПЕКТИВА24
', /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/core/src/str/mod.rs:576:13
The same for:
let persp = "тЦ".to_case(Case::Title);
thread 'main' panicked at 'byte index 3 is not a char boundary; it is inside 'Ц' (bytes 2..4) of тЦ
', /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/core/src/str/mod.rs:576:13
Hi there,
I really like you crate. However I came across a small issue. If the second char is uppercase in a camel case string, there should not be boundary inserted.
Here are two test cases:
failes with "foo_bar" != "f_oo_bar"
#[test]
fn test_camel_case() {
assert_eq!("foo_bar", "fOOBar".from_case(Case::Camel).to_case(Case::Snake))
}
succeeds
#[test]
fn test_pascal_case() {
assert_eq!("foo_bar", "FOOBar".from_case(Case::Camel).to_case(Case::Snake))
}
It's probably a quite niche bug and also easy to work around by just making the first letter always uppercase and then converting from pascal case instead.
to_case() fail with words With accent mark. Example: música moderna.
I have some strings like uart0
and need to convert to UpperSnake
in some cases. So I search and find this wonderful crate. But when using it, the result I get is like UART_0
.
I wonder is there a way to avoid this? Maybe a flag or some methods in this crate?
0.4 to 0.5 had some breaking changes, which are not documented anywhere.
For example, from_case
no longer works on str
.
@rutrum Hi!
Firstly, thanks for such a useful crate!
I have found the following example which IMO has a not intuitive result.
#[test]
fn test_upper_snake() {
use convert_case::{Case, Casing};
assert!("UPPER_CASE_WITH_DIGIT1".is_case(Case::UpperSnake));
}
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 the StateConverter
with Boundary::defaults()
which includes UpperDigit
boundary.
Is this by design, or a bug?
If a bug, would the correct implementation of is_case
be something like this?
fn is_case(&self, case: Case) -> bool {
&self.from_case(case).to_case(case) == self
}
It would be great to expose a write!
compatible form of convert case in case an existing buffer is already provided.
I'm trying to use Case::Title
on some text that contains a newline (ex: The big\nblue house
), but the newline isn't counted as a boundary, so I get The Big\nblue House
. Is there a way to specify newlines as boundarys so I can get The Big\nBlue House
? Thanks.
I tried doing something like:
let mut boundaries = Boundary::list_from("\n");
boundaries.push(Boundary::Space);
let converter = Converter::new()
.to_case(Case::Title)
.set_boundaries(&boundaries);
But I didn't have any luck with this.
Arc<str>
type cannot call to_case
, you need to convert .as_ref()
to &str
first.
error[E0599]: the method `to_case` exists for struct `Arc<str>`, but its trait bounds were not satisfied
--> projects\src\variants\codegen.rs:37:31
|
37 | let wasi_alias = name.to_case(Case::Kebab);
| ^^^^^^^
|
365 | pub struct String {
| ----------------- doesn't satisfy `_: PartialEq<Arc<str>>`
|
248 | / pub struct Arc<
249 | | T: ?Sized,
250 | | #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
251 | | > {
| |_- doesn't satisfy `std::sync::Arc<str>: Casing<std::sync::Arc<str>>`
|
= note: the following trait bounds were not satisfied:
`std::string::String: PartialEq<std::sync::Arc<str>>`
which is required by `std::sync::Arc<str>: Casing<std::sync::Arc<str>>`
`str: Sized`
which is required by `str: Casing<str>`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.