Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max characters to random paragraph #8

Merged
merged 6 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
22 changes: 22 additions & 0 deletions src/random/randomParagraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
20 changes: 17 additions & 3 deletions src/random/randomParagraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@ 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,
words = 8,
}: {
maxCharacters?: number;
words?: number;
} = {}) => {
return capitalize(
array(randomInt(words, 16), () => randomWord())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be called minWords as words sounds like exact number?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right.

.join(" ")
.slice(0, maxCharacters - 1) + "."
);
};
Loading