Skip to content

Commit 8bbe6c5

Browse files
authored
Merge pull request #2 from whitequark/master
Various improvements
2 parents 9ed521c + 9e524b5 commit 8bbe6c5

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

src/case.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use std::char::ToUppercase;
1919
use std::iter::Peekable;
2020

21-
/// Converts any case into lower case ignoring seperators.
21+
/// Converts any case into lower case ignoring separators.
2222
///
2323
/// # Example
2424
/// ```rust
@@ -59,7 +59,7 @@ pub fn is_lower_case(string: &str) -> bool {
5959
string == to_lower_case(string)
6060
}
6161

62-
/// Converts any case into UPPER CASE ignoring seperators.
62+
/// Converts any case into UPPER CASE ignoring separators.
6363
///
6464
/// # Example
6565
/// ```rust
@@ -120,7 +120,7 @@ pub fn is_upper_case(string: &str) -> bool {
120120
pub fn to_sentence_case(string: &str) -> String {
121121
string
122122
.chars()
123-
.map(|c| swap_seperator(c, ' '))
123+
.map(|c| swap_separator(c, ' '))
124124
.break_camel(' ')
125125
.flat_map(char::to_lowercase)
126126
.collect()
@@ -163,7 +163,7 @@ pub fn is_sentence_case(string: &str) -> bool {
163163
pub fn to_title_case(string: &str) -> String {
164164
string
165165
.chars()
166-
.map(|c| swap_seperator(c, ' '))
166+
.map(|c| swap_separator(c, ' '))
167167
.break_camel(' ')
168168
.flat_map(char::to_lowercase)
169169
.capitalize_words()
@@ -289,7 +289,7 @@ pub fn is_pascal_case(string: &str) -> bool {
289289
pub fn to_kebab_case(string: &str) -> String {
290290
string
291291
.chars()
292-
.map(|c| swap_seperator(c, '-'))
292+
.map(|c| swap_separator(c, '-'))
293293
.break_camel('-')
294294
.flat_map(char::to_lowercase)
295295
.collect()
@@ -332,7 +332,7 @@ pub fn is_kebab_case(string: &str) -> bool {
332332
pub fn to_train_case(string: &str) -> String {
333333
string
334334
.chars()
335-
.map(|c| swap_seperator(c, '-'))
335+
.map(|c| swap_separator(c, '-'))
336336
.break_camel('-')
337337
.flat_map(char::to_lowercase)
338338
.capitalize_words()
@@ -376,7 +376,7 @@ pub fn is_train_case(string: &str) -> bool {
376376
pub fn to_snake_case(string: &str) -> String {
377377
string
378378
.chars()
379-
.map(|c| swap_seperator(c, '_'))
379+
.map(|c| swap_separator(c, '_'))
380380
.break_camel('_')
381381
.flat_map(char::to_lowercase)
382382
.collect()
@@ -419,7 +419,7 @@ pub fn is_snake_case(string: &str) -> bool {
419419
pub fn to_constant_case(string: &str) -> String {
420420
string
421421
.chars()
422-
.map(|c| swap_seperator(c, '_'))
422+
.map(|c| swap_separator(c, '_'))
423423
.break_camel('_')
424424
.flat_map(char::to_uppercase)
425425
.collect()
@@ -443,18 +443,18 @@ pub fn is_constant_case(string: &str) -> bool {
443443
string == to_constant_case(string)
444444
}
445445

446-
/// Checks if a character is a seperator.
446+
/// Checks if a character is a separator.
447447
#[inline]
448-
fn is_seperator(c: char) -> bool {
448+
fn is_separator(c: char) -> bool {
449449
c == ' ' || c == '-' || c == '_'
450450
}
451451

452-
/// Swaps the current character (which may be a seperator), with the seperator
453-
/// of choice. Currently ' ', '-', and '_' are considered seperators which will
452+
/// Swaps the current character (which may be a separator), with the separator
453+
/// of choice. Currently ' ', '-', and '_' are considered separators which will
454454
/// be swapped.
455455
#[inline]
456-
fn swap_seperator(c: char, sep: char) -> char {
457-
if is_seperator(c) {
456+
fn swap_separator(c: char, sep: char) -> char {
457+
if is_separator(c) {
458458
sep
459459
} else {
460460
c
@@ -477,9 +477,9 @@ fn scan_to_camel(state: &mut (bool, Option<char>), curr: char) -> Option<String>
477477
// capitalize it and mark the state as finished.
478478
state.0 = false;
479479
Some(curr.to_uppercase().collect())
480-
} else if is_seperator(curr) {
481-
// If the current character is a seperator, mark the state to capitalize
482-
// the next character and remove the seperator.
480+
} else if is_separator(curr) {
481+
// If the current character is a separator, mark the state to capitalize
482+
// the next character and remove the separator.
483483
state.0 = true;
484484
Some("".to_owned())
485485
} else if !last.map_or(false, char::is_lowercase) {
@@ -510,12 +510,12 @@ trait Extras: Iterator<Item=char> {
510510
}
511511

512512
/// Uses the `CapitalizeWords` type to capitilize individual words seperated
513-
/// by a seperator (as defined by `is_seperator`).
513+
/// by a separator (as defined by `is_separator`).
514514
#[inline]
515515
fn capitalize_words(self) -> CapitalizeWords<Self> where Self: Sized {
516516
let mut iter = self.peekable();
517-
// If the first character is not a seperator, we want to capitilize it.
518-
let cap = !iter.peek().cloned().map_or(false, is_seperator);
517+
// If the first character is not a separator, we want to capitilize it.
518+
let cap = !iter.peek().cloned().map_or(false, is_separator);
519519
CapitalizeWords {
520520
iter: iter,
521521
cap: cap,
@@ -542,7 +542,7 @@ impl<I> Iterator for BreakCamel<I> where I: Iterator<Item=char> {
542542

543543
#[inline]
544544
fn next(&mut self) -> Option<Self::Item> {
545-
// If we have been signaled to break, the next item is a seperator and we
545+
// If we have been signaled to break, the next item is a separator and we
546546
// should disable break mode.
547547
if self.br {
548548
self.br = false;
@@ -605,10 +605,10 @@ impl<I> Iterator for CapitalizeWords<I> where I: Iterator<Item=char> {
605605
self.upper = Some(c.to_uppercase());
606606
// We want it to loop back here…
607607
} else {
608-
// Otherwise return the character, but if it is a seperator, and the
609-
// next character is not a seperator—signal the next character should
608+
// Otherwise return the character, but if it is a separator, and the
609+
// next character is not a separator—signal the next character should
610610
// be capitalized.
611-
if is_seperator(c) && !self.iter.peek().cloned().map_or(false, is_seperator) {
611+
if is_separator(c) && !self.iter.peek().cloned().map_or(false, is_separator) {
612612
self.cap = true;
613613
}
614614
return Some(c);

src/lib.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub mod case;
2727
/// assert_eq!("Hello World".to_camel_case(), "helloWorld".to_owned());
2828
/// ```
2929
pub trait Inflect {
30+
fn to_upper_case(&self) -> String;
31+
fn is_upper_case(&self) -> bool;
32+
fn to_lower_case(&self) -> String;
33+
fn is_lower_case(&self) -> bool;
3034
fn to_sentence_case(&self) -> String;
3135
fn is_sentence_case(&self) -> bool;
3236
fn to_title_case(&self) -> String;
@@ -45,7 +49,11 @@ pub trait Inflect {
4549
fn is_constant_case(&self) -> bool;
4650
}
4751

48-
impl<'a> Inflect for &'a str {
52+
impl Inflect for str {
53+
#[inline] fn to_upper_case(&self) -> String { case::to_upper_case(self) }
54+
#[inline] fn is_upper_case(&self) -> bool { case::is_upper_case(self) }
55+
#[inline] fn to_lower_case(&self) -> String { case::to_lower_case(self) }
56+
#[inline] fn is_lower_case(&self) -> bool { case::is_lower_case(self) }
4957
#[inline] fn to_sentence_case(&self) -> String { case::to_sentence_case(self) }
5058
#[inline] fn is_sentence_case(&self) -> bool { case::is_sentence_case(self) }
5159
#[inline] fn to_title_case(&self) -> String { case::to_title_case(self) }
@@ -64,21 +72,17 @@ impl<'a> Inflect for &'a str {
6472
#[inline] fn is_constant_case(&self) -> bool { case::is_constant_case(self) }
6573
}
6674

67-
impl Inflect for String {
68-
#[inline] fn to_sentence_case(&self) -> String { case::to_sentence_case(&self) }
69-
#[inline] fn is_sentence_case(&self) -> bool { case::is_sentence_case(&self) }
70-
#[inline] fn to_title_case(&self) -> String { case::to_title_case(&self) }
71-
#[inline] fn is_title_case(&self) -> bool { case::is_title_case(&self) }
72-
#[inline] fn to_camel_case(&self) -> String { case::to_camel_case(&self) }
73-
#[inline] fn is_camel_case(&self) -> bool { case::is_camel_case(&self) }
74-
#[inline] fn to_pascal_case(&self) -> String { case::to_pascal_case(&self) }
75-
#[inline] fn is_pascal_case(&self) -> bool { case::is_pascal_case(&self) }
76-
#[inline] fn to_kebab_case(&self) -> String { case::to_kebab_case(&self) }
77-
#[inline] fn is_kebab_case(&self) -> bool { case::is_kebab_case(&self) }
78-
#[inline] fn to_train_case(&self) -> String { case::to_train_case(&self) }
79-
#[inline] fn is_train_case(&self) -> bool { case::is_train_case(&self) }
80-
#[inline] fn to_snake_case(&self) -> String { case::to_snake_case(&self) }
81-
#[inline] fn is_snake_case(&self) -> bool { case::is_snake_case(&self) }
82-
#[inline] fn to_constant_case(&self) -> String { case::to_constant_case(&self) }
83-
#[inline] fn is_constant_case(&self) -> bool { case::is_constant_case(&self) }
75+
#[cfg(test)]
76+
mod test {
77+
use super::Inflect;
78+
79+
#[test]
80+
fn test_str() {
81+
assert_eq!("foo".to_title_case(), "Foo".to_owned());
82+
}
83+
84+
#[test]
85+
fn test_string() {
86+
assert_eq!("foo".to_owned().to_title_case(), "Foo".to_owned());
87+
}
8488
}

0 commit comments

Comments
 (0)