diff options
Diffstat (limited to 'third_party/rust/Inflector/src/numbers')
-rw-r--r-- | third_party/rust/Inflector/src/numbers/deordinalize/mod.rs | 117 | ||||
-rw-r--r-- | third_party/rust/Inflector/src/numbers/mod.rs | 9 | ||||
-rw-r--r-- | third_party/rust/Inflector/src/numbers/ordinalize/mod.rs | 147 |
3 files changed, 273 insertions, 0 deletions
diff --git a/third_party/rust/Inflector/src/numbers/deordinalize/mod.rs b/third_party/rust/Inflector/src/numbers/deordinalize/mod.rs new file mode 100644 index 0000000000..f64cce1143 --- /dev/null +++ b/third_party/rust/Inflector/src/numbers/deordinalize/mod.rs @@ -0,0 +1,117 @@ +/// Deorginalizes a `&str` +/// +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "0.1"; +/// let expected_string: String = "0.1".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "-1st"; +/// let expected_string: String = "-1".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "0th"; +/// let expected_string: String = "0".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "1st"; +/// let expected_string: String = "1".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "2nd"; +/// let expected_string: String = "2".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "3rd"; +/// let expected_string: String = "3".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "9th"; +/// let expected_string: String = "9".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "12th"; +/// let expected_string: String = "12".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "12000th"; +/// let expected_string: String = "12000".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "12001th"; +/// let expected_string: String = "12001".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "12002nd"; +/// let expected_string: String = "12002".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "12003rd"; +/// let expected_string: String = "12003".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::deordinalize::deordinalize; +/// let mock_string: &str = "12004th"; +/// let expected_string: String = "12004".to_owned(); +/// let asserted_string: String = deordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +pub fn deordinalize(non_ordinalized_string: &str) -> String { + if non_ordinalized_string.contains('.') { + non_ordinalized_string.to_owned() + } else { + non_ordinalized_string.trim_end_matches("st") + .trim_end_matches("nd") + .trim_end_matches("rd") + .trim_end_matches("th") + .to_owned() + } +} diff --git a/third_party/rust/Inflector/src/numbers/mod.rs b/third_party/rust/Inflector/src/numbers/mod.rs new file mode 100644 index 0000000000..f28704f52e --- /dev/null +++ b/third_party/rust/Inflector/src/numbers/mod.rs @@ -0,0 +1,9 @@ +#![deny(warnings)] +/// Provides ordinalization of a string. +/// +/// Example string "1" becomes "1st" +pub mod ordinalize; +/// Provides deordinalization of a string. +/// +/// Example string "1st" becomes "1" +pub mod deordinalize; diff --git a/third_party/rust/Inflector/src/numbers/ordinalize/mod.rs b/third_party/rust/Inflector/src/numbers/ordinalize/mod.rs new file mode 100644 index 0000000000..82b0d2d834 --- /dev/null +++ b/third_party/rust/Inflector/src/numbers/ordinalize/mod.rs @@ -0,0 +1,147 @@ +/// Orginalizes a `&str` +/// +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "a"; +/// let expected_string: String = "a".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "0.1"; +/// let expected_string: String = "0.1".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "-1"; +/// let expected_string: String = "-1st".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "0"; +/// let expected_string: String = "0th".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "1"; +/// let expected_string: String = "1st".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "2"; +/// let expected_string: String = "2nd".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "3"; +/// let expected_string: String = "3rd".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "9"; +/// let expected_string: String = "9th".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "12"; +/// let expected_string: String = "12th".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "12000"; +/// let expected_string: String = "12000th".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "12001"; +/// let expected_string: String = "12001st".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "12002"; +/// let expected_string: String = "12002nd".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "12003"; +/// let expected_string: String = "12003rd".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +/// ``` +/// use inflector::numbers::ordinalize::ordinalize; +/// let mock_string: &str = "12004"; +/// let expected_string: String = "12004th".to_owned(); +/// let asserted_string: String = ordinalize(mock_string); +/// assert!(asserted_string == expected_string); +/// +/// ``` +pub fn ordinalize(non_ordinalized_string: &str) -> String { + let chars: Vec<char> = non_ordinalized_string.clone().chars().collect(); + let last_number: char = chars[chars.len() - 1]; + if is_ordinalizable(last_number) { + return non_ordinalized_string.to_owned(); + } + if chars.len() > 1 { + if second_last_number_is_one(chars) { + return format!("{}{}", non_ordinalized_string, "th"); + } else if string_contains_decimal(non_ordinalized_string.to_owned()) { + return non_ordinalized_string.to_owned(); + } + } + match last_number { + '1' => format!("{}{}", non_ordinalized_string, "st"), + '2' => format!("{}{}", non_ordinalized_string, "nd"), + '3' => format!("{}{}", non_ordinalized_string, "rd"), + _ => format!("{}{}", non_ordinalized_string, "th"), + } +} + +fn is_ordinalizable(last_number: char) -> bool { + !last_number.is_numeric() +} + +fn second_last_number_is_one(chars: Vec<char>) -> bool { + let second_last_number: char = chars[chars.len() - 2]; + second_last_number == '1' +} + +fn string_contains_decimal(non_ordinalized_string: String) -> bool { + non_ordinalized_string.contains('.') +} |