From ac4930ff5a59c3718331568efd2d43b3da4d2ed2 Mon Sep 17 00:00:00 2001 From: narcis-fv Date: Thu, 28 Sep 2023 13:27:32 +0100 Subject: [PATCH] maxWords --- src/random/randomParagraph.test.ts | 45 ++++++++++++++++++------------ src/random/randomParagraph.ts | 8 ++++-- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/random/randomParagraph.test.ts b/src/random/randomParagraph.test.ts index 4acac33..6e63950 100644 --- a/src/random/randomParagraph.test.ts +++ b/src/random/randomParagraph.test.ts @@ -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(); + }); }); diff --git a/src/random/randomParagraph.ts b/src/random/randomParagraph.ts index 6234cb6..f449aba 100644 --- a/src/random/randomParagraph.ts +++ b/src/random/randomParagraph.ts @@ -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) + "." );