Skip to content

trananhtung/camelcase

Repository files navigation

camelcase

All Contributors

crates.io docs.rs CI license

Convert a string to camelCase.

foo-barfooBar, foo_bar_bazfooBarBaz, XML-httpxmlHttp. Handles dash, dot, underscore, and space separators, as well as existing camelCase/PascalCase and acronyms. A faithful Rust port of the widely-used camelcase npm package.

It is the inverse of decamelize.

  • PascalCase, preserve_consecutive_uppercase, and capitalize_after_number options
  • Differential-tested against the reference camelcase implementation

Install

[dependencies]
camelcase = "0.1"

Usage

use camelcase::{camel_case, camel_case_with, camel_case_slice, Options};

assert_eq!(camel_case("foo-bar"), "fooBar");
assert_eq!(camel_case("foo_bar_baz"), "fooBarBaz");
assert_eq!(camel_case("XML-http"), "xmlHttp");
assert_eq!(camel_case("p2p network"), "p2pNetwork");
assert_eq!(camel_case("foo123bar"), "foo123Bar");
assert_eq!(camel_case("__foo__bar__"), "__fooBar"); // leading _/$ preserved

// PascalCase:
assert_eq!(camel_case_with("foo-bar", Options::new().pascal_case(true)), "FooBar");

// Preserve consecutive uppercase runs:
assert_eq!(
    camel_case_with("XML-http", Options::new().preserve_consecutive_uppercase(true)),
    "XMLHttp"
);

// From a list of fragments:
assert_eq!(camel_case_slice(&["foo", "bar"], Options::default()), "fooBar");

Options

Option Default Effect
pascal_case false Uppercase the first character (FooBar).
preserve_consecutive_uppercase false Keep runs of uppercase letters (XMLHttp instead of xmlHttp).
capitalize_after_number true Treat a digit run as a word boundary (foo123Bar).

Contributors ✨

This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

Tung Tran
Tung Tran

💻 🚧

License

Licensed under either of MIT or Apache-2.0 at your option.

About

Convert a string to camelCase (foo-bar -> fooBar). A faithful Rust port of the camelcase npm package; the inverse of decamelize.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages