Code Monkey home page Code Monkey logo

inflector's Introduction

Rust Inflector

Build Status Crates.ioCrate downloads

Adds String based inflections for Rust. Snake, kebab, train, camel, sentence, class, and title cases as well as ordinalize, deordinalize, demodulize, deconstantize, foreign key, table case, and pluralize/singularize are supported as both traits and pure functions acting on &str and String types.


Documentation:

Documentation can be found here at the README or via rust docs below.

Rust docs with examples


Installation:

As a crate

[dependencies]
Inflector = "*"

Compile yourself:

  1. Install Rust and cargo
  2. git clone https://github.com/whatisinternet/Inflector
  3. Library: cd inflector && cargo build --release --lib
  4. You can find the library in target/release

Usage / Example:

...
// to use methods like String.to_lower_case();
extern crate inflector;
use inflector::Inflector;
...
fn main() {
...
  let camel_case_string: String = "some_string".to_camel_case();
...
}

Or

...
// to use methods like to_snake_case(&str);
extern crate inflector;

// use inflector::cases::classcase::to_class_case;
// use inflector::cases::classcase::is_class_case;

// use inflector::cases::camelcase::to_camel_case;
// use inflector::cases::camelcase::is_camel_case;

// use inflector::cases::pascalcase::to_pascal_case;
// use inflector::cases::pascalcase::is_pascal_case;

// use inflector::cases::screamingsnakecase::to_screamingsnake_case;
// use inflector::cases::screamingsnakecase::is_screamingsnake_case;

// use inflector::cases::snakecase::to_snake_case;
// use inflector::cases::snakecase::is_snake_case;

// use inflector::cases::kebabcase::to_kebab_case;
// use inflector::cases::kebabcase::is_kebab_case;

// use inflector::cases::traincase::to_train_case;
// use inflector::cases::traincase::is_train_case;

// use inflector::cases::sentencecase::to_sentence_case;
// use inflector::cases::sentencecase::is_sentence_case;

// use inflector::cases::titlecase::to_title_case;
// use inflector::cases::titlecase::is_title_case;

// use inflector::cases::tablecase::to_table_case;
// use inflector::cases::tablecase::is_table_case;

// use inflector::numbers::ordinalize::ordinalize;
// use inflector::numbers::deordinalize::deordinalize;

// use inflector::suffix::foreignkey::to_foreign_key;
// use inflector::suffix::foreignkey::is_foreign_key;

// use inflector::string::demodulize::demodulize;
// use inflector::string::deconstantize::deconstantize;

// use inflector::string::pluralize::to_plural;
// use inflector::string::singularize::to_singular;
...
fn main() {
...
  let camel_case_string: String = to_camel_case("some_string");
...
}

Advanced installation and usage:

If the project doesn't require singularize, pluralize, class, table, demodulize, deconstantize. Then in your cargo.toml you may wish to specify:

[dependencies.Inflector]
version = "*"
default-features = false

Or

Inflector = {version="*", default-features=false}

To test this crate locally with features off try:

cargo test --no-default-features

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

inflector's People

Contributors

cmsd2 avatar eijebong avatar kanerogers avatar mthjones avatar nbaksalyar avatar stpettersens avatar weiznich avatar whatisinternet avatar yoric 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  avatar

inflector's Issues

Wrong converting to class case

This code:

assert_eq!(
    inflector::cases::classcase::to_class_case("editMessageMedia"), 
    "EditMessageMedia"
);

Fails with this error:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `"EditMessageMedum"`,
 right: `"EditMessageMedia"`', src/main.rs:228:5

ClassCase singularization

The documentation should make clear that to_class_case singularizes the string.

Also, it would be nice if there was a variant that does not singularize. I'll be happy to work on a PR if someone finds a good name for the function/module :)

Performance is too low

In contrast to a similar project camel case and snake case are up to 4 times slower. This will require a large refactor of the entire project, but can be broken up and completed one method at a time.

Use a lowercase crate name

Inflector is different from most crates in using a capital letter to start it's crate name. It would be nice if it was consistent with the rest of the ecosystem.

Guide for benchmarking this crate

The benchmarks are all in place. Just needs docs on how to do it.

Steps might include:

  1. rustup
  2. changing to nightly rust
  3. commands for running benchmarks cargo bench --features=unstable
  4. posting as part of PR or issue

Register other cases with crates.io

I've never submitted a crate, so I don't know that it's possible, but after spending a few minutes wondering why on Earth cargo wasn't finding inflector = x - for x as anything I could think to try - I think it would be a great idea to register 'inflector' as an alias for the crate 'Inflector', if that's possible?

If it's not possible, then perhaps consider changing the repo name to 'Inflector', so they're at least consistent?

Clippy

Currently failing clippy checks. Should be fairly easy to get back to green.

Incorrect handling of accentuated chars?

With Inflector 1.11.4 & Rust 1.51.0:

assert_eq!("CARRÉ".to_lowercase(), "carré");
assert_eq!("CARRÉ".to_title_case(), "Carré");
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `"CarrÉ"`,
 right: `"Carré"`'

Performance

Iterating over every char is probably not the best approach. Should probably come up with something better.

Camelcase with single letter starting word

Take the string "FBar". Turning it into camel case (from what I can tell, please correct me if I'm wrong) should yield "fBar" but it doesn't, it yields "fbar" which doesn't really make any sense from what I can tell.

Benchmarks

This is to track benchmark results for case conversions performance issues seen in #18.

Obviously there is a huge issue with class case. That said several other areas are quite slow too.

Tests were run on: mid-2014 MBP i5 2.6

Current state of master

test cases::camelcase::tests::bench_camel                                         ... bench:       1,825 ns/iter (+/- 346)
test cases::camelcase::tests::bench_camel_from_sname                              ... bench:       1,223 ns/iter (+/- 289)
test cases::camelcase::tests::bench_is_camel                                      ... bench:      49,416 ns/iter (+/- 593)
test cases::classcase::tests::bench_class_case                                    ... bench: 160,985,376 ns/iter (+/- 5,173,751)
test cases::classcase::tests::bench_class_from_snake                              ... bench: 161,533,425 ns/iter (+/- 4,167,305)
test cases::classcase::tests::bench_is_class                                      ... bench: 161,352,118 ns/iter (+/- 3,793,478)
test cases::kebabcase::tests::bench_is_kebab                                      ... bench:         793 ns/iter (+/- 400)
test cases::kebabcase::tests::bench_kebab                                         ... bench:         752 ns/iter (+/- 310)
test cases::kebabcase::tests::bench_kebab_from_snake                              ... bench:         210 ns/iter (+/- 32)
test cases::lowercase::tests::bench_is_lower                                      ... bench:         340 ns/iter (+/- 86)
test cases::lowercase::tests::bench_lower                                         ... bench:         306 ns/iter (+/- 173)
test cases::screamingsnakecase::tests::bench_is_screaming_snake                   ... bench:         635 ns/iter (+/- 210)
test cases::screamingsnakecase::tests::bench_screaming_snake                      ... bench:         610 ns/iter (+/- 87)
test cases::screamingsnakecase::tests::bench_screaming_snake_from_camel           ... bench:         961 ns/iter (+/- 579)
test cases::screamingsnakecase::tests::bench_screaming_snake_from_class           ... bench:         894 ns/iter (+/- 352)
test cases::screamingsnakecase::tests::bench_screaming_snake_from_kebab           ... bench:         877 ns/iter (+/- 571)
test cases::screamingsnakecase::tests::bench_screaming_snake_from_screaming_snake ... bench:         584 ns/iter (+/- 304)
test cases::screamingsnakecase::tests::bench_screaming_snake_from_sentence        ... bench:       1,123 ns/iter (+/- 630)
test cases::screamingsnakecase::tests::bench_screaming_snake_from_upper_kebab     ... bench:         914 ns/iter (+/- 435)
test cases::sentencecase::tests::bench_is_sentence                                ... bench:       2,714 ns/iter (+/- 796)
test cases::sentencecase::tests::bench_sentence                                   ... bench:       2,678 ns/iter (+/- 1,357)
test cases::sentencecase::tests::bench_sentence_from_snake                        ... bench:       2,100 ns/iter (+/- 1,046)
test cases::snakecase::tests::bench_is_snake                                      ... bench:         626 ns/iter (+/- 191)
test cases::snakecase::tests::bench_snake                                         ... bench:         581 ns/iter (+/- 298)
test cases::snakecase::tests::bench_snake_from_camel                              ... bench:         882 ns/iter (+/- 328)
test cases::snakecase::tests::bench_snake_from_class                              ... bench:         883 ns/iter (+/- 193)
test cases::snakecase::tests::bench_snake_from_kebab                              ... bench:         922 ns/iter (+/- 360)
test cases::snakecase::tests::bench_snake_from_sentence                           ... bench:       1,209 ns/iter (+/- 539)
test cases::snakecase::tests::bench_snake_from_snake                              ... bench:         637 ns/iter (+/- 386)
test cases::snakecase::tests::bench_snake_from_upper_kebab                        ... bench:         876 ns/iter (+/- 488)
test cases::tablecase::tests::bench_is_table_case                                 ... bench:       5,784 ns/iter (+/- 1,129)
test cases::tablecase::tests::bench_table_case                                    ... bench:       5,754 ns/iter (+/- 1,460)
test cases::titlecase::tests::bench_is_title                                      ... bench:       2,847 ns/iter (+/- 1,553)
test cases::titlecase::tests::bench_title                                         ... bench:       2,799 ns/iter (+/- 1,309)
test cases::titlecase::tests::bench_title_from_snake                              ... bench:       2,202 ns/iter (+/- 697)
test cases::uppercase::tests::bench_is_upper                                      ... bench:         357 ns/iter (+/- 55)
test cases::uppercase::tests::bench_upper                                         ... bench:         311 ns/iter (+/- 135)

Is this project alive?

There are about 6 million downloads on the crates.io website.
And the last release was 3 years ago.
No public activity from the main developer on the last 3 years on GitHub!
Fortunately, he is alive, I can see some activity on Twitter and LinkedIn (based on the links in his website).

So my question is that, what's the state of such an important project with so many defendants?

UTF8 support

Hello, I'm fairly new to Rust and wanted to build something on the case conversion of this crate.
However, it does not work on UTF8. I completely understand that almost everything you'd want to case convert is ASCII.
Was this chosen for performance reasons, or was it an oversight / not a conscious decision?

Thank you

Thank you for this package. It accomplishes everything I was looking for in an inflection package and more. It even does pluralization. Just wanted to open an issue to say thank you.

Unspecified license.

Hi! Cargo.toml advertises BSD 2-clause license but this isn't specified anywhere else.

Title case performance

It's actually pretty good but much slower than it needs to be:

test cases::titlecase::tests::bench_is_title                    ... bench:         888 ns/iter (+/- 191)
test cases::titlecase::tests::bench_title                       ... bench:         804 ns/iter (+/- 188)
test cases::titlecase::tests::bench_title_from_snake            ... bench:         825 ns/iter (+/- 419)

A general approach might be to look at what PascalCase is doing and adapt it for Title Case since they're so similar.

Upload license file to crates.io

The license text LICENSE.md is not in the sources uploaded to crates.io.

$ wget https://crates.io/api/v1/crates/Inflector/0.11.4/download
$ tar -xvf download
$ ls Inflector-0.11.4
Cargo.toml  Cargo.toml.orig  README.md  src  tests

Can you please upload it to crates.io? It would make it easier for us to comply with the Redistributions of source code must retain the above copyright notice part, for automated tooling that downloads from crates.io to redistribute the notice more easily.

deordinalize fails on non-numbers

Just glancing at the code, deordinalizing "last" would give "la". Similarly, "first" would give "fir", while I would have expected it to give "1".

To me this definitely qualifies as a bug. Not sure what the correct behavior is, but at a minimum this should be documented. Ideally it wouldn't affect words that are not actually ordinals.

Improvement on guide to contributing

The current guide is OK but doesn't really onboard people. It should probably include getting started with the crate as well as running benchmarks and tests.

Conversions should not consume String

Looks like most (all?) conversions create a new String, which means it should be sufficient for them to accept &str and not a String.

This way it would be possible to convert borrowed strings without cloning them (which is expensive due to extra temporary heap allocation and copying data).

Snake casing not idempotent for strings with a numeric character following a capitalized character

Current behavior

to_snake_case("convertedAcquisitionV2Counter") => "converted_acquisition_v2_counter"
to_snake_case("converted_acquisition_v2_counter") => "converted_acquisition_v_2_counter"

Which also leads to:

is_snake_case(to_snake_case("convertedAcquisitionV2Counter")) => false

Expected behavior

to_snake_case("convertedAcquisitionV2Counter") => "converted_acquisition_v2_counter"
to_snake_case("converted_acquisition_v2_counter") => "converted_acquisition_v2_counter"
is_snake_case(to_snake_case("convertedAcquisitionV2Counter")) => true

Open up the internal API for case casting

Not sure if anyone would find it useful. Basically I'm looking to cut down on the number of supported cases this crate has. This is probably something that needs feedback overall but I think it could be a good idea for a 2.0.0 (obviously very much so future thinking since we're not even at a 1.0.0).

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.