Skip to content

Commit

Permalink
Merge pull request #49 from mgeisler/lipsum-words
Browse files Browse the repository at this point in the history
lipsum_words: new function for random lorem ipsum text
  • Loading branch information
mgeisler authored Dec 9, 2018
2 parents cd53a74 + fe836bf commit e2f9088
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ This is a changelog with the most important changes in each release.

### Unreleased

The new `lipsum_words` function can be used to generate random lorem
ipsum text that doesn't always start with "Lorem ipsum".

Dependencies were updated and the oldest supported version of Rust is
now 1.22.

Expand Down
47 changes: 41 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,12 @@ thread_local! {
}
}

/// Generate `n` words of lorem ipsum text. The output starts with
/// "Lorem ipsum" and continues with the standard lorem ipsum text
/// from [`LOREM_IPSUM`]. The text will become random if sufficiently
/// long output is requested.
/// Generate `n` words of lorem ipsum text. The output will always
/// start with "Lorem ipsum".
///
/// The text continues with the standard lorem ipsum text from
/// [`LOREM_IPSUM`] and becomes random if more than 18 words is
/// requested. See [`lipsum_words`] if fully random text is needed.
///
/// # Examples
///
Expand All @@ -409,22 +411,47 @@ thread_local! {
/// ```
///
/// [`LOREM_IPSUM`]: constant.LOREM_IPSUM.html
/// [`lipsum_words`]: fn.lipsum_words.html
pub fn lipsum(n: usize) -> String {
LOREM_IPSUM_CHAIN.with(|cell| {
let mut chain = cell.borrow_mut();
chain.generate_from(n, ("Lorem", "ipsum"))
})
}

/// Generate `n` words of random lorem ipsum text.
///
/// The text starts with a random word from [`LOREM_IPSUM`]. Multiple
/// sentences may be generated, depending on the punctuation of the
/// words being random selected.
///
/// # Examples
///
/// ```
/// use lipsum::lipsum_words;
///
/// println!("{}", lipsum_words(6));
/// // -> "Propter soliditatem, censet in infinito inani."
/// ```
///
/// [`LOREM_IPSUM`]: constant.LOREM_IPSUM.html
pub fn lipsum_words(n: usize) -> String {
LOREM_IPSUM_CHAIN.with(|cell| {
let mut chain = cell.borrow_mut();
chain.generate(n)
})
}

/// Minimum number of words to include in a title.
const TITLE_MIN_WORDS: usize = 3;
/// Maximum number of words to include in a title.
const TITLE_MAX_WORDS: usize = 8;
/// Words shorter than this size are not capitalized.
const TITLE_SMALL_WORD: usize = 3;

/// Generate a short lorem ipsum string where the words are
/// capitalized and stripped for punctuation characters.
/// Generate a short lorem ipsum text with words in title case.
///
/// The words are capitalized and stripped for punctuation characters.
///
/// # Examples
///
Expand Down Expand Up @@ -496,6 +523,14 @@ mod tests {
assert_eq!(lipsum(2).split_whitespace().count(), 2);
}

#[test]
fn starts_differently() {
// Check that calls to lipsum_words don't always start with
// "Lorem ipsum".
let idx = "Lorem ipsum".len();
assert_ne!(&lipsum_words(5)[..idx], &lipsum_words(5)[..idx]);
}

#[test]
fn generate_title() {
for word in lipsum_title().split_whitespace() {
Expand Down

0 comments on commit e2f9088

Please sign in to comment.