1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/// Truncates a string and appends an ellipsis (`"..."`) if it exceeds the specified length.
///
/// This function trims the input string of leading and trailing whitespace. If the trimmed
/// string's length exceeds the specified `length`, it truncates the string to `length - 3`
/// characters and appends an ellipsis. If the trimmed string is shorter than or equal to
/// `length`, it returns the trimmed string as is. If either the trimmed string or the
/// specified `length` is less than 3, it returns `"..."`.
///
/// # Arguments
///
/// * `s` - The input string to potentially truncate.
/// * `length` - The maximum allowed length of the returned string.
///
/// # Returns
///
/// * `String` - The possibly truncated string with an ellipsis appended.
///
/// # Examples
///
/// ```rust
/// use lowdash::ellipsis;
///
/// let result = ellipsis("Hello, World!", 10);
/// assert_eq!(result, "Hello, ...");
///
/// let result = ellipsis("Short", 10);
/// assert_eq!(result, "Short");
///
/// let result = ellipsis("ExactLength", 11);
/// assert_eq!(result, "ExactLength");
///
/// let result = ellipsis(" Trimmed ", 6);
/// assert_eq!(result, "Tri...");
///
/// let result = ellipsis("Hi", 2);
/// assert_eq!(result, "Hi");
/// ```
pub fn ellipsis(s: &str, length: usize) -> String {
let trimmed = s.trim();
let trimmed_len = trimmed.chars().count();
if trimmed_len > length {
if trimmed_len < 3 || length < 3 {
return "...".to_string();
}
let trunc_length = length.saturating_sub(3);
let truncated: String = trimmed.chars().take(trunc_length).collect();
truncated + "..."
} else {
trimmed.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ellipsis_truncate() {
let result = ellipsis("Hello, World!", 10);
assert_eq!(result, "Hello, ...");
}
#[test]
fn test_ellipsis_no_truncate() {
let result = ellipsis("Short", 10);
assert_eq!(result, "Short");
}
#[test]
fn test_ellipsis_exact_length() {
let result = ellipsis("ExactLength", 11);
assert_eq!(result, "ExactLength");
}
#[test]
fn test_ellipsis_with_whitespace() {
let result = ellipsis(" Trimmed ", 6);
assert_eq!(result, "Tri...");
}
#[test]
fn test_ellipsis_short_length() {
let result = ellipsis("Hi", 2);
assert_eq!(result, "Hi");
}
#[test]
fn test_ellipsis_length_less_than_three() {
let result = ellipsis("Hello", 2);
assert_eq!(result, "...");
}
#[test]
fn test_ellipsis_empty_string() {
let result = ellipsis(" ", 5);
assert_eq!(result, "");
}
#[test]
fn test_ellipsis_length_zero() {
let result = ellipsis("Hello", 0);
assert_eq!(result, "...");
}
#[test]
fn test_ellipsis_unicode_characters() {
let result = ellipsis("こんにちは世界", 5);
assert_eq!(result, "こん...");
}
#[test]
fn test_ellipsis_multibyte_characters() {
let result = ellipsis("😀😃😄😁😆", 4);
assert_eq!(result, "😀...");
}
}