From e40b752ec8aaf0eb8c272d278c09c29ef0a038b2 Mon Sep 17 00:00:00 2001 From: narcis-fv Date: Thu, 28 Sep 2023 12:56:27 +0100 Subject: [PATCH 1/5] fix randomParagraph --- src/random/randomParagraph.test.ts | 22 ++++++++++++++++++++++ src/random/randomParagraph.ts | 20 ++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/random/randomParagraph.test.ts b/src/random/randomParagraph.test.ts index d80478e..4acac33 100644 --- a/src/random/randomParagraph.test.ts +++ b/src/random/randomParagraph.test.ts @@ -8,3 +8,25 @@ describe(`randomParagraph`, () => { expect(randomParagraph().endsWith(".")).toBeTruthy(); }); }); + +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(`does not exceed maxCharacters even with large words count`, () => { + const result = randomParagraph({ maxCharacters: 10, words: 10 }); + 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 5b00e82..8924bf7 100644 --- a/src/random/randomParagraph.ts +++ b/src/random/randomParagraph.ts @@ -2,6 +2,22 @@ import { array } from "../helpers"; import { capitalize } from "../helpers/capitalize"; import { randomWord } from "./randomWord"; -export const randomParagraph = () => { - return capitalize(array(8, () => 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, + words = 8, +}: { + maxCharacters?: number; + words?: number; +} = {}) => { + return capitalize( + array(words, () => randomWord()) + .join(" ") + .slice(0, maxCharacters - 1) + "." + ); }; From 9c30568d52c90812a50e8b5bcbcb17303f5d1e7b Mon Sep 17 00:00:00 2001 From: narcis-fv Date: Thu, 28 Sep 2023 12:59:03 +0100 Subject: [PATCH 2/5] update readme --- README.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/README.md b/README.md index 7bb067d..adeb269 100644 --- a/README.md +++ b/README.md @@ -130,13 +130,7 @@ These functions are optimized for low entropy random data generation useful for 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. From 52f61547104200aa0934d224f4b79fb3e3e98682 Mon Sep 17 00:00:00 2001 From: narcis-fv Date: Thu, 28 Sep 2023 13:00:44 +0100 Subject: [PATCH 3/5] bump --- CHANGELOG.md | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57a7877..5b7888d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.17.0 + +### Minor Changes + +- Updated randomParagraph to have the maxCharacters and words variables + ## 0.16.0 ### Minor Changes diff --git a/package.json b/package.json index 5033150..5645d19 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.16.1", + "version": "0.17.0", "description": "Everything you need for Dev", "main": "dist/index.js", "module": "./dist/index.mjs", From f86b1f2e18893de9e5b8ef11535fdb901148e51a Mon Sep 17 00:00:00 2001 From: narcis-fv Date: Thu, 28 Sep 2023 13:17:21 +0100 Subject: [PATCH 4/5] added vars to randomParagraph --- CHANGELOG.md | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) 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/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", From ac4930ff5a59c3718331568efd2d43b3da4d2ed2 Mon Sep 17 00:00:00 2001 From: narcis-fv Date: Thu, 28 Sep 2023 13:27:32 +0100 Subject: [PATCH 5/5] 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) + "." );