Skip to content

Commit

Permalink
maxWords
Browse files Browse the repository at this point in the history
  • Loading branch information
narcis-fv committed Sep 28, 2023
1 parent f86b1f2 commit ac4930f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
45 changes: 27 additions & 18 deletions src/random/randomParagraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,35 @@ describe(`randomParagraph`, () => {
expect(randomParagraph().length).toBeTruthy();
expect(randomParagraph().endsWith(".")).toBeTruthy();
});
});

it(`respects maxCharacters`, () => {
const result = randomParagraph({ maxCharacters: 10 });
expect(result.length).toBeLessThanOrEqual(10);
});
it(`respects maxCharacters`, () => {
const result = randomParagraph({ maxCharacters: 10 });
expect(result.length).toBeLessThanOrEqual(10);
});

it(`respects words count with a large enough maxCharacters`, () => {
const result = randomParagraph({ words: 5, maxCharacters: 1000 });
const wordCount = result.split(" ").length;
// Subtracting 1 because the last word is followed by a period.
expect(wordCount).toEqual(5);
});
it(`generates a random number of words between minWords and maxWords`, () => {
const result = randomParagraph({
minWords: 5,
maxWords: 10,
maxCharacters: 1000,
});

it(`does not exceed maxCharacters even with large words count`, () => {
const result = randomParagraph({ maxCharacters: 10, words: 10 });
expect(result.length).toBeLessThanOrEqual(10);
});
const wordCount = result.split(" ").length;
expect(wordCount).toBeGreaterThanOrEqual(5);
expect(wordCount).toBeLessThanOrEqual(10);
});

it(`does not exceed maxCharacters even with large words count`, () => {
const result = randomParagraph({
maxCharacters: 10,
minWords: 10,
maxWords: 20,
});
expect(result.length).toBeLessThanOrEqual(10);
});

it(`returns a string with a period at the end`, () => {
const result = randomParagraph();
expect(result.endsWith(".")).toBeTruthy();
it(`returns a string with a period at the end`, () => {
const result = randomParagraph();
expect(result.endsWith(".")).toBeTruthy();
});
});
8 changes: 5 additions & 3 deletions src/random/randomParagraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import { randomWord } from "./randomWord";
*/
export const randomParagraph = ({
maxCharacters = 200,
words = 8,
minWords = 8,
maxWords = 16,
}: {
maxCharacters?: number;
words?: number;
minWords?: number;
maxWords?: number;
} = {}) => {
return capitalize(
array(randomInt(words, 16), () => randomWord())
array(randomInt(minWords, maxWords), () => randomWord())
.join(" ")
.slice(0, maxCharacters - 1) + "."
);
Expand Down

0 comments on commit ac4930f

Please sign in to comment.