rust_persian_tools/commas/
remove_commas.rs

1/// Remove all commas in string\
2/// 3,000,000 -> 3000000\
3/// Example:
4/// ```rust
5/// // function:
6/// use rust_persian_tools::commas::remove_commas::remove_commas;
7/// assert_eq!(remove_commas("30,000,000"), "30000000".to_string());
8///
9/// // method:
10/// use crate::rust_persian_tools::commas::remove_commas::RemoveCommas;
11/// assert_eq!("30,000,000".remove_commas(), "30000000".to_string());
12/// ```
13pub fn remove_commas(str: impl AsRef<str>) -> String {
14    str.as_ref().replace(',', "")
15}
16
17/// Remove all commas in string\
18/// 3,000,000 -> 3000000\
19/// Example:
20/// ```rust
21/// // function:
22/// use rust_persian_tools::commas::remove_commas::remove_commas_mut;
23/// let mut input = String::from("30,000,000");
24/// remove_commas_mut(&mut input);
25/// assert_eq!(input, "30000000".to_string());
26///
27/// ```
28pub fn remove_commas_mut(str: &mut String) {
29    str.retain(|c| c != ',')
30}
31
32use std::borrow::Cow;
33pub trait RemoveCommas {
34    fn remove_commas(&self) -> String;
35}
36
37impl RemoveCommas for String {
38    fn remove_commas(&self) -> String {
39        remove_commas(self)
40    }
41}
42
43impl RemoveCommas for str {
44    fn remove_commas(&self) -> String {
45        remove_commas(self)
46    }
47}
48
49impl RemoveCommas for Cow<'_, str> {
50    fn remove_commas(&self) -> String {
51        remove_commas(self)
52    }
53}
54
55pub trait RemoveCommasMut {
56    fn remove_commas_mut(&mut self);
57}
58
59impl RemoveCommasMut for String {
60    fn remove_commas_mut(&mut self) {
61        remove_commas_mut(self)
62    }
63}
64impl RemoveCommasMut for Cow<'_, str> {
65    fn remove_commas_mut(&mut self) {
66        remove_commas_mut(self.to_mut())
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn remove_commas_test() {
76        assert_eq!(remove_commas("30,000,000"), "30000000".to_string());
77    }
78
79    #[test]
80    fn remove_commas_mut_test() {
81        let mut input = String::from("30,000,000");
82        remove_commas_mut(&mut input);
83        assert_eq!(input, "30000000".to_string());
84    }
85}