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 all 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
31 changes: 31 additions & 0 deletions src/random/randomParagraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
22 changes: 19 additions & 3 deletions src/random/randomParagraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) + "."
);
};
Loading