Skip to content

Commit 345b5d9

Browse files
authored
feat: add minCharacters option to randomParagraph (#39)
* feat: add minCharacters option to randomParagraph Adds an optional minCharacters parameter that ensures the generated paragraph meets a minimum character length by adding words beyond maxWords. The result is still capped by maxCharacters (truncation wins). * chore: version bump for minCharacters option
1 parent c220183 commit 345b5d9

4 files changed

Lines changed: 62 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# deverything
22

3+
## 5.1.0
4+
5+
### Minor Changes
6+
7+
- Add `minCharacters` option to `randomParagraph` — ensures the generated paragraph meets a minimum character length by adding words beyond `maxWords`, while still respecting `maxCharacters` as an upper bound.
8+
39
## 5.0.0
410

511
### Major Changes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deverything",
3-
"version": "5.0.0",
3+
"version": "5.1.0",
44
"description": "Everything you need for Dev",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

src/random/randomParagraph.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,41 @@ describe(`randomParagraph`, () => {
3838
const result = randomParagraph();
3939
expect(result.endsWith(".")).toBeTruthy();
4040
});
41+
42+
it(`respects minCharacters`, () => {
43+
for (let i = 0; i < 20; i++) {
44+
const result = randomParagraph({ minCharacters: 100, maxCharacters: 500 });
45+
expect(result.length).toBeGreaterThanOrEqual(100);
46+
}
47+
});
48+
49+
it(`respects minCharacters and maxCharacters together`, () => {
50+
for (let i = 0; i < 20; i++) {
51+
const result = randomParagraph({
52+
minCharacters: 150,
53+
maxCharacters: 200,
54+
});
55+
expect(result.length).toBeGreaterThanOrEqual(150);
56+
expect(result.length).toBeLessThanOrEqual(200);
57+
}
58+
});
59+
60+
it(`caps at maxCharacters when minCharacters exceeds it`, () => {
61+
for (let i = 0; i < 20; i++) {
62+
const result = randomParagraph({
63+
minCharacters: 300,
64+
maxCharacters: 200,
65+
});
66+
expect(result.length).toBeLessThanOrEqual(200);
67+
}
68+
});
69+
70+
it(`default behavior is unchanged without minCharacters`, () => {
71+
for (let i = 0; i < 20; i++) {
72+
const result = randomParagraph();
73+
expect(result.length).toBeLessThanOrEqual(200);
74+
expect(result.endsWith(".")).toBeTruthy();
75+
expect(result.length).toBeGreaterThan(0);
76+
}
77+
});
4178
});

src/random/randomParagraph.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,33 @@ import { randomWord } from "./randomWord";
77
/**
88
* Generates a random paragraph of text.
99
* @param maxCharacters The maximum number of characters. The paragraph will be truncated to this length if it exceeds it. Default is 200.
10-
* @param words The number of words. Default is 8.
10+
* @param minCharacters The minimum number of characters. Words will be added beyond maxWords until this threshold is met. Default is undefined (no minimum).
11+
* @param minWords The minimum number of words. Default is 8.
12+
* @param maxWords The maximum number of words. Default is 16.
1113
* @returns A random paragraph of text.
1214
*/
1315
export const randomParagraph = ({
1416
maxCharacters = 200,
17+
minCharacters,
1518
minWords = 8,
1619
maxWords = 16,
1720
}: {
1821
maxCharacters?: number;
22+
minCharacters?: number;
1923
minWords?: number;
2024
maxWords?: number;
2125
} = {}) => {
22-
return capitalize(
23-
array(randomInt({ min: minWords, max: maxWords }), () => randomWord())
24-
.join(" ")
25-
.slice(0, maxCharacters - 1) + "."
26-
);
26+
const words: string[] = [];
27+
const targetWordCount = randomInt({ min: minWords, max: maxWords });
28+
29+
for (
30+
let i = 0;
31+
i < targetWordCount ||
32+
(minCharacters !== undefined && words.join(" ").length < minCharacters);
33+
i++
34+
) {
35+
words.push(randomWord());
36+
}
37+
38+
return capitalize(words.join(" ").slice(0, maxCharacters - 1) + ".");
2739
};

0 commit comments

Comments
 (0)