Skip to content

Commit

Permalink
when reversing words before doing the Schlinkert prune, use graphemes…
Browse files Browse the repository at this point in the history
… rather than chracters to better attempt to handle accent characters and emoji
  • Loading branch information
sts10 committed Apr 24, 2023
1 parent 73d8f1f commit be38459
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/list_manipulations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ pub fn schlinkert_prune(list: &[String]) -> Vec<String> {
new_list
}

/// Reverse all words on given list
/// ["hotdog", "hamburger", "alligator"] becomes
/// ["godtoh", "regrubmah", "rotagilla"]
/// Probably doesn't work well on complex graphmemes...
fn reverse_all_words(list: &[String]) -> Vec<String> {
/// Reverse all words on given list. For example,
/// `["hotdog", "hamburger", "alligator"]` becomes
/// `["godtoh", "regrubmah", "rotagilla"]`
/// Uses graphemes to ensure it handles accented characters correctly.
pub fn reverse_all_words(list: &[String]) -> Vec<String> {
let mut reversed_list = vec![];
for word in list {
reversed_list.push(word.chars().rev().collect::<String>());
reversed_list.push(word.graphemes(true).rev().collect::<String>());
}
reversed_list
}
Expand Down
13 changes: 13 additions & 0 deletions tests/list_manipulation_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod list_manipulation_tests {
use tidy::dice::print_as_dice; // not exactly sure why I need this here...
use tidy::list_manipulations::reverse_all_words;
use tidy::*;

fn make_lists() -> (Vec<String>, Vec<String>, Vec<String>, Vec<String>) {
Expand Down Expand Up @@ -660,6 +661,18 @@ mod list_manipulation_tests {
);
}

#[test]
fn can_reverse_list() {
let list = vec![
"hotdog".to_string(),
"hamburger".to_string(),
"alligator".to_string(),
"😀😁😆".to_string(),
];
let rev_list = reverse_all_words(&list);
assert_eq!(rev_list, ["godtoh", "regrubmah", "rotagilla", "😆😁😀"]);
}

#[test]
fn can_print_dice_rolls_of_base_6() {
assert_eq!(print_as_dice(0, 6, 7776, false), "11111".to_string());
Expand Down

0 comments on commit be38459

Please sign in to comment.