Giter Site home page Giter Site logo

convert-case's Introduction

Convert Case

Converts to and from various cases.

Rust Library convert_case

Convert case was written in Rust and is ready to be used inline with your rust code as a library.

use convert_case::{Case, Casing};

assert_eq!("ronnieJamesDio", "Ronnie_James_dio".to_case(Case::Camel));
assert_eq!("io_stream", "IOStream".to_case(Case::Snake));
assert_eq!(
    "2020-04-16 My Cat Cali",
    "2020-04-16_my_cat_cali".from_case(Case::Snake).to_case(Case::Title)
);

You can read the API documentation on docs.rs for a list of all features and read lots of examples.

Command Line Utility ccase

The command line utility ccase was made to expose the tools of the convert_case library to the command line.

$ ccase -t title super_mario_64
Super Mario 64

$ ccase -f snake -t title 2020-04-15_my_cat_cali
2020-04-16 My Cat Cali

$ ccase -t camel "convert to camel"
convertToCamel

Links

convert_case ccase
Repository github github
Crate crates.io crates.io
Documentation docs.rs

Cases

This is list of cases that convert_case supports. Some cases are simply aliases of others. The "Random" and "PseudoRandom" cases are provided in the convert_case library with the "random" feature, and are automatically provided in the ccase binary.

Case Example
Upper MY VARIABLE NAME
Lower my variable name
Title My Variable Name
Toggle mY vARIABLE nAME
Alternating mY vArIaBlE nAmE
Camel myVariableName
Pascal
UpperCamel
MyVariableName
Snake my_variable_name
UpperSnake
ScreamingSnake
MY_VARIABLE_NAME
Kebab my-variable-name
Cobol MY-VARIABLE-NAME
Train My-Variable-Name
Flat myvariablename
UpperFlat MYVARIABLENAME
Random MY vaRiabLe nAME
PseudoRandom mY VaRiAblE nAMe

License

Licensed under MIT License.

convert-case's People

Contributors

r3v2d0g avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

convert-case's Issues

To Path

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

Support streaming / writers

It would be great to expose a write! compatible form of convert case in case an existing buffer is already provided.

Feature request: sentence 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.

Wrong boundary detected when converting from camel case

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.

Add a license file to ccase's release tarball

I was packaging ccase on AUR when I noticed that the release tarballs at crates.io for ccase currently does not contain a LICENSE file, which makes packaging quite a bit uglier.

Can you please add a LICENSE file under /ccase directory? Much thanks!

Panicked at 'byte index

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

Add benches

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.

Convert from `Arc<str>`

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>`

Handle 2d and 3d as word separators

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

Is there a way to not insert `_` between number and character?

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?

Is there a way to specify `\n` as a boundary?

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.