Convert a string to camelCase.
foo-bar → fooBar, foo_bar_baz → fooBarBaz, XML-http → xmlHttp. 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, andcapitalize_after_numberoptions- Differential-tested against the reference
camelcaseimplementation
[dependencies]
camelcase = "0.1"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");| 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). |
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 💻 🚧 |
Licensed under either of MIT or Apache-2.0 at your option.