diff --git a/CHANGELOG.md b/CHANGELOG.md index 46eeff7..6d92e1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.29.0 + +### Minor Changes + +- Added maxCharacters and number of words as variables for randomParagraph. Updated readme to suggest running pnpm bump. + ## 0.28.1 ### Patch Changes diff --git a/README.md b/README.md index 55085b6..4f47e36 100644 --- a/README.md +++ b/README.md @@ -156,13 +156,7 @@ Checks are functions that throw an error, if the validation fails After changes, run ``` -pnpm changeset -``` - -then - -``` -pnpm changeset version +pnpm bump ``` To bump the version. CI will take care of publishing the package when merged. diff --git a/package.json b/package.json index 4e62efc..c3ce5dd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.28.1", + "version": "0.29.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/random/randomParagraph.test.ts b/src/random/randomParagraph.test.ts index d80478e..6e63950 100644 --- a/src/random/randomParagraph.test.ts +++ b/src/random/randomParagraph.test.ts @@ -7,4 +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(`generates a random number of words between minWords and maxWords`, () => { + const result = randomParagraph({ + minWords: 5, + maxWords: 10, + maxCharacters: 1000, + }); + + 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(); + }); }); diff --git a/src/random/randomParagraph.ts b/src/random/randomParagraph.ts index 7f440b1..f449aba 100644 --- a/src/random/randomParagraph.ts +++ b/src/random/randomParagraph.ts @@ -4,8 +4,24 @@ import { randomInt } from "./randomInt"; import { randomWord } from "./randomWord"; // TODO: add a comma in the middle of the sentence -export const randomParagraph = () => { - return ( - capitalize(array(randomInt(8, 16), () => randomWord()).join(" ")) + "." +/** + * Generates a random paragraph of text. + * @param maxCharacters The maximum number of characters. The paragraph will be truncated to this length if it exceeds it. Default is 200. + * @param words The number of words. Default is 8. + * @returns A random paragraph of text. + */ +export const randomParagraph = ({ + maxCharacters = 200, + minWords = 8, + maxWords = 16, +}: { + maxCharacters?: number; + minWords?: number; + maxWords?: number; +} = {}) => { + return capitalize( + array(randomInt(minWords, maxWords), () => randomWord()) + .join(" ") + .slice(0, maxCharacters - 1) + "." ); };