Skip to content

Commit

Permalink
Merge pull request #8 from codeledge/add-maxCharacters-to-random-para…
Browse files Browse the repository at this point in the history
…graph

added maxCharacters, maxWords and minWords to randomParagraph
  • Loading branch information
tokenosopher authored Sep 28, 2023
2 parents a4c48d8 + ac4930f commit 553cb47
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
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) + "."
);
};

0 comments on commit 553cb47

Please sign in to comment.