Skip to content

Commit

Permalink
Merge pull request #4 from fenhl/join
Browse files Browse the repository at this point in the history
Add `join` convenience function
  • Loading branch information
fenhl committed Feb 3, 2021
2 parents 666349c + 1134499 commit d2b2c1f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ pub fn quote(in_str: &str) -> Cow<str> {
}
}

/// Convenience function that consumes an iterable of words and turns it into a single string,
/// quoting words when necessary. Consecutive words will be separated by a single space.
pub fn join<'a, I: IntoIterator<Item = &'a str>>(words: I) -> String {
words.into_iter()
.map(quote)
.collect::<Vec<_>>()
.join(" ")
}

#[cfg(test)]
static SPLIT_TEST_ITEMS: &'static [(&'static str, Option<&'static [&'static str]>)] = &[
("foo$baz", Some(&["foo$baz"])),
Expand Down Expand Up @@ -227,3 +236,11 @@ fn test_quote() {
assert_eq!(quote("\""), "\"\\\"\"");
assert_eq!(quote(""), "\"\"");
}

#[test]
fn test_join() {
assert_eq!(join(vec![]), "");
assert_eq!(join(vec![""]), "\"\"");
assert_eq!(join(vec!["a", "b"]), "a b");
assert_eq!(join(vec!["foo bar", "baz"]), "\"foo bar\" baz");
}

0 comments on commit d2b2c1f

Please sign in to comment.