Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
//!
//! ## Example
//!
//! ```
//! use titlecase::titlecase;
//! ```rust
//! use titlecase::{titlecase, Titlecase};
//!
//! let text = "a sample title to capitalize: an example";
//! assert_eq!(text.titlecase(), "A Sample Title to Capitalize: An Example");
//! assert_eq!(titlecase(text), "A Sample Title to Capitalize: An Example");
//! ```
//!
Expand Down Expand Up @@ -76,6 +77,26 @@ fn words_regex() -> &'static Regex {
})
}

/// A trait describing an item that can be converted to title case.
///
/// # Examples
///
/// ```rust
/// use titlecase::Titlecase;
///
/// assert_eq!("hello world!".titlecase(), "Hello World!");
/// ```
pub trait Titlecase {
/// Convert `self` to title case.
fn titlecase(&self) -> String;
}

impl<T: AsRef<str>> Titlecase for T {
fn titlecase(&self) -> String {
titlecase(self.as_ref())
}
}

/// Returns `input` in title case.
///
/// ### Example
Expand Down Expand Up @@ -235,13 +256,14 @@ fn fix_small_word_at_end(text: &str) -> Cow<'_, str> {

#[cfg(test)]
mod tests {
use super::titlecase;
use super::{titlecase, Titlecase};

macro_rules! testcase {
($name:ident, $input:expr, $expected:expr) => {
#[test]
fn $name() {
assert_eq!(titlecase($input), $expected);
assert_eq!($input.titlecase(), $expected);
}
};
}
Expand Down