Skip to content

Commit b1998d5

Browse files
authored
fix title abbreviations (#615)
1 parent 235bcbf commit b1998d5

File tree

1 file changed

+104
-1
lines changed
  • crates/livesplit-title-abbreviations/src

1 file changed

+104
-1
lines changed

crates/livesplit-title-abbreviations/src/lib.rs

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn is_all_caps_or_digits(text: &str) -> bool {
103103

104104
pub fn abbreviate(name: &str) -> Vec<Box<str>> {
105105
let name = name.trim();
106-
let mut list = vec![name.into()];
106+
let mut list = vec![];
107107
if name.is_empty() {
108108
return list;
109109
}
@@ -165,6 +165,13 @@ pub fn abbreviate(name: &str) -> Vec<Box<str>> {
165165
list.sort_unstable();
166166
list.dedup();
167167

168+
if let Some(idx) = list.iter().position(|x| name == x.as_ref()) {
169+
let last = list.len() - 1;
170+
list.swap(idx, last);
171+
} else {
172+
list.push(name.into());
173+
}
174+
168175
list
169176
}
170177

@@ -214,3 +221,99 @@ pub fn abbreviate_category(category: &str) -> Vec<Box<str>> {
214221

215222
abbrevs
216223
}
224+
225+
#[cfg(test)]
226+
mod tests {
227+
use super::abbreviate;
228+
use alloc::boxed::Box;
229+
use alloc::vec;
230+
231+
// The tests using actual game titles can be thrown out or edited if any
232+
// major changes need to be made to the abbreviation algorithm. Do not
233+
// hesitate to remove them if they are getting in the way of actual
234+
// improvements.
235+
//
236+
// They exist purely as another measure to prevent accidental breakage.
237+
#[test]
238+
fn game_test_1() {
239+
let abbreviations = abbreviate("Burnout 3: Takedown");
240+
241+
let expected = vec![
242+
Box::from("B3"),
243+
Box::from("B3: Takedown"),
244+
Box::from("Burnout 3"),
245+
Box::from("Takedown"),
246+
Box::from("Burnout 3: Takedown"),
247+
];
248+
249+
assert_eq!(abbreviations, expected);
250+
}
251+
252+
#[test]
253+
fn game_test_2() {
254+
let abbreviations = abbreviate("The Legend of Zelda: The Wind Waker");
255+
256+
let expected = vec![
257+
Box::from("Legend of Zelda: TWW"),
258+
Box::from("Legend of Zelda: The Wind Waker"),
259+
Box::from("Legend of Zelda: Wind Waker"),
260+
Box::from("TLoZ: TWW"),
261+
Box::from("TLoZ: The Wind Waker"),
262+
Box::from("TLoZ: Wind Waker"),
263+
Box::from("TWW"),
264+
Box::from("The Wind Waker"),
265+
Box::from("Wind Waker"),
266+
Box::from("The Legend of Zelda: The Wind Waker"),
267+
];
268+
269+
assert_eq!(abbreviations, expected);
270+
}
271+
272+
#[test]
273+
fn game_test_3() {
274+
let abbreviations = abbreviate("SpongeBob SquarePants: Battle for Bikini Bottom");
275+
276+
let expected = vec![
277+
Box::from("Battle for Bikini Bottom"),
278+
Box::from("BfBB"),
279+
Box::from("SS: Battle for Bikini Bottom"),
280+
Box::from("SS: BfBB"),
281+
Box::from("SpongeBob SquarePants: Battle for Bikini Bottom"),
282+
];
283+
284+
assert_eq!(abbreviations, expected);
285+
}
286+
287+
#[test]
288+
#[rustfmt::skip]
289+
fn game_test_4() {
290+
let abbreviations = abbreviate("Super Mario 64");
291+
292+
let expected = vec![
293+
Box::from("SM64"),
294+
Box::from("Super Mario 64"),
295+
];
296+
297+
assert_eq!(abbreviations, expected);
298+
}
299+
300+
#[test]
301+
fn contains_original_title() {
302+
let abbreviations = abbreviate("test title: the game");
303+
assert!(abbreviations.contains(&Box::from("test title: the game")));
304+
}
305+
306+
#[test]
307+
fn removes_parens() {
308+
let abbreviations = abbreviate("test title (the game)");
309+
assert!(abbreviations.contains(&Box::from("test title")));
310+
}
311+
312+
#[test]
313+
fn original_title_is_last() {
314+
let abbreviations = abbreviate("test title: the game");
315+
let last = abbreviations.last().unwrap();
316+
317+
assert_eq!("test title: the game", last.as_ref())
318+
}
319+
}

0 commit comments

Comments
 (0)